Lecture 3 and 4 - Operators and conditional statements Loops Flashcards
Break and continue
How many types of operators are there in java ?
Arithmetic operators (+, -, *, /, %) double a = 7/2; //equals to 3.0, instead of 3.5
Assignment operators (+=, -=, *=, /=, %=, <<=, >>=, &=,...) x += 2; //equivalent to: x = x + 2;
Relational operators (==, !=, >=, <=, >,
Types of incremental and decremental operators
Postfix (x++, x–): The result is the value of x before the operators
Prefix (++x, –x): The result is the value of x after the operators
Bitwise and shift operators:
∼x \_\_\_\_Bitwise compliment of x x & y \_\_ Bitwise AND of x and y x | y \_\_\_Bitwise OR of x and y x ∧ y \_\_Bitwise XOR of x and y x << k _Left shift x >> k _Right shift x >>> k Unsigned right shift
Ternary operator:
The ternary operator can replace a if-else statement
Syntax:
condition ? exprTrue : exprFalse;
ex. min = (a<b></b>
What is a for loop used for ?
A for loop can be used to execute a block of statements repeatedly when the repeating time is known ex.
for(initialization; condition; incr/decr){
}
Example of for each in java ?
int []numArray = {7,2,6,0};
for(int i:numArray){
System.out.println(“number is: “ + i);
}
Do while and while loop difference :
while loop might not ever run. do-while loop runs at least once.
What does continue do ?
The continue statement skips the current iteration of a loop.
Only skips the innermost enclosing loop of the statement.