Lesson 03 - Decision and Looping
Lesson 03 - Decision and Looping
a. partial if statement
if(boolean expression){
statement(s);
}
b. full if statement
if(boolean expression){
statement(s);
}
else if(boolean expression){
statement(s);
}
else{
statement(s);
}
The switch…case construct
This decision construct is used for making a choice among
multiple execution paths, and the choice can be based upon an
int value. This statement successively test the value of an
expression or value against a list of integer or character
constraints. When a match is found, the statements associated
with the case constant are executed.
switch (variableName) {
case const1:
statement(s);
break;
case const2:
statement(s);
break;
case const3:
statement(s);
break;
default:
statement(s);
}
Some keywords…
The break statement – causes the program flow to exit from the body of the switch
construct. Each case label takes only a single argument, but when execution jumps
to one of these labels, it continues downward until it reaches a break statement.
The default keyword – is comparable to the else part of the if statement. The
statements associated with this keyword are executed if the value of the switch
variable does not match any of the case constants.
The while loop
while(expression){
statements(s);
}
The do…while loop
In the do…while loop, the expression (condition) is
evaluated at the end of the loop to ensure that the statements
are executed at least once. Similar to the while loop, the
statements are executed repeatedly as long as the expression is
true.
do{
statements(s);
}while(expression)
The for loop
This loop construct executes a given statement or block
of statements for a definite number of times. It performs a loop
so that a single variable is incremented over a range of values
between two limits.