Google

if - switch

the if statement enables you to execute a series of statements if a test condition is true.

Here's what the syntax of this statement looks like,note that the code to execute is between curly braces, { and } :

Format :


if (condition){
// statements;
The if statement also includes an optional else clause .
The if evaluates the expression, that is test condition, in parentheses, 
and if the expression evaluates to true, the block after the opening curly
braces is executed; otherwise the block after the else is executed.
 

Format :

 
if (condition){
// statements1;
}
else{
// statements2;
}
 
 
if/else if
 

Format :

 
if (condition) {
// statements1;
}
else if (condition) {
// statements2;
}
else if (condition) {
// statements3;
}
else{
// statements4;
 
If the first conditional expression following the if keyword is true, the  block of statements following the expression are executed and control starts after the whole if statement. 
Otherwise, if the conditional expression following the if keyword is false, control branches to the first else if and the expression following it is evaluated. If that expression is true, the  block of statements following it are executed, and if false, the next else if is tested. 

All if/else if statements are tested and if none of their expressions are true, control goes to the else statement. Although the latter else is not required, it serves as a default action if all previous conditions were false.
 
 

The switch Statement

This statement enables you to evaluate an expression and match the expression's 
value to a number of test values, and the code will be executed 
when a match is found. 
You place the expression to evaluate in parentheses after the switch 
keyword, and the test values you want to match in a set of case statements
If a match found,the code in that case statement is executed,
and a break statement ends the switch statement. 
 

Format :

 
 
switch (expression){
case label1:
// statements
break;
case label2:
// statements
break;
// ...
[default:
statements]
 
Note , If the value of the expression doesn't match any test value, the code in the default statement, which is optional, is executed. 
If you include a default statement, it should be the last statement .

No comments: