0% found this document useful (0 votes)
15 views

Lesson 03 - Decision and Looping

The document discusses Java's decision and looping constructs including if/else statements, switch/case statements, while loops, do/while loops, and for loops. It provides examples and explanations of how each construct works and its syntax.

Uploaded by

Hehe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lesson 03 - Decision and Looping

The document discusses Java's decision and looping constructs including if/else statements, switch/case statements, while loops, do/while loops, and for loops. It provides examples and explanations of how each construct works and its syntax.

Uploaded by

Hehe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Chapter 3

Decision and Looping


Decision and Looping
Java’s decision and looping constructs are very much similar to
that of the syntax in C and C++ language. Java provides a choice of two
selection constructs: the if…else and switch…case mechanisms. Simple
conditional code for a choice of two execution paths based on the value
of a Boolean expression is easily written using if…else. The switch…case
construct on the other hand, is used when more complex choices
between multiple execution paths are needed. Java also supports the
use of nested or sequences of if…else.
Iteration is catered for by three styles of loop: the for, while, and
do…while constructions.
The if…else construct
Similar to C and C++, the Java if decision construct is
followed by a logical or relational expression in which data is
compared and a decision is made based on the result of the
comparison.

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

This construct executes a given statement or block of


statements again and again as long as the value of the
expression is true. The expression must be a boolean expression
and must return a value of either true or false. This expression
checks the variable called the loop control variable.

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.

for(initialization; condition; altering list){


statements(s);
}
Initialization expression – is the statement executed immediately before
the loop itself is started. It is often used to set up starting conditions such as
loop variable declarations and initialization. This expression is executed only
once.
Test condition – must be a Boolean expression and is treated exactly the
same as in the while loop. The body of the loop will be executed repeatedly
until the condition ceases to be true. This condition is executed each time
the control passes to the beginning of the loop. The body of the loop is only
executed after this test condition is checked.
Altering list (iteration expression or increment/decrement) – is executed
immediately after the body of the loop, just before the test is performed
again. Commonly, this is used to increment a loop counter.
The following are used to further control the loop statements:
The break statement – causes the program flow to exit prematurely
from the body
break [label];
The continue statement – causes the program flow skip over and jump
to the end of the loop body and then return the control to the loop
control statement
continue [label];
[label:] statement, where the statement should be a loop – identifies
any valid statement to which control must be transferred
[label:] statement;

You might also like