if else block
if (expression1) {
/* statements to be executed
if the expression1 is evaluated true */
} else if (expression2) {
/* statements to be executed
if the expression2 is evaluated true */
} else {
/* statements to be executed
if the expressions are all false */
}
- note that variables declared inside an
if block does not retain its value outside the block
if statements can be nested, but it is not good practice
logical operators
| operator |
description |
&& |
and |
| || |
or |
! |
not |
switch statement
switch(n) {
case value1:
/* statement for case 1 */
break;
case value2:
/* statement for case 2 */
break;
default:
/* statement for all other cases */
break;
}
- note that each case block is terminated with
break, omitting which will cause fall through
- note that this cannot be used for strings as only integral type variables can be used
ternary conditional operator
maxnumber = (num1 > num2) ? num1 : num2; /* (expression) ?, output if true :output of false