40 | P a g e
VI. CONTROL STRUCTURES
Objectives
In this chapter, we will be discussing control structures, which allow us to change the
ordering of how the statements in our programs are executed. Writing several kinds of
decision making statements, statements that choose between alternatives and putting
statements inside one another will be introduced.
At the end of the lesson, the student should be able to:
• Use decision control structures (if, else, switch) which allows selection of specific
sections of code to be executed
• Use repetition control structures (while, do‐while, for) which allow executing specific
sections of code a number of times
• Use branching statements (break, continue, return) which allows redirection of program
flow
Decision Control Structures
Decision control structures allow us to select and execute specific blocks of code
while skipping other sections.
o If statement
if statement is a single‐selection statement because it selects or ignores a single action.
The if statement either performs an action, if a condition is true, or skips it, if the
condition is false.
Syntax for if statement:
if (boolean‐expression)
statement;
Note that the boolean expression must be enclosed in parentheses. Since it contains
Boolean condition, then it is expected to have a true or false value.
Example:
int finalGrade=70;
If(finalGrade>=60)
System.out.println(“Passed!”);
Training-workshop on Object-oriented Programming using Java
41 | P a g e
Figure 6.0: Flow Chart of if statement
or
if( boolean_expression ){
statement1;
statement2;
. . .
}
The statement can also be a statement block enclosed by braces.
Example:
int finalGrade=70;
if (finalGrade>=60){
System.out.println(“Sorry!”);
System.out.println(“You didn’t make it!”);
}
o If‐else statement
if…else statement is called a double‐selection statement because it selects between two
different actions. It performs an action if a condition is true and performs a different
action if the condition is false.
Syntax for if‐else statement:
if( boolean_expression )
statement;
else
statement;
Training-workshop on Object-oriented Programming using Java
42 | P a g e
Example:
int finalGrade=70;
if(finalGrade>=60)
System.out.println(“Passed”);
else {
System.out.println(“Failed!”);
}
Figure 6.1: Flow Chart of if‐else statement
or
if( boolean_expression ){
statement1;
statement2;
. . .
}
else{
statement1;
statement2;
. . .
}
Example:
int finalGrade=70;
if(finalGrade>=60){
System.out.println(“Congrats!”);
System.out.println(“You Passed”);
}
else
System.out.println(“Sorry!,You Failed!”);
Training-workshop on Object-oriented Programming using Java
43 | P a g e
To avoid confusion, always place the statement or statements of an if or if‐else block
inside brackets {}.
Note that in if‐else evaluate Boolean value and use == in comparing values.
o nested if statement
It is said to be nested if statement if one control statement appears inside another. Thus it
tests multiple conditions by placing if…else statements inside other if…else statements.
Syntax for if‐else statement:
if( boolean_expression ){
if( boolean_expression ){
statement;
}
else{
statement;
}
}
Example:
int num;
if(num!=0){
if(num>0){
System.out.println(“You enter positive number!”);
}
else{
System.out.println(“You entered negative number!”);
}
}
o Switch statement
Switch statement is called a multiple‐selection statement because it selects among many
different actions. It performs one of many different actions, depending on the value of an
expression.
Syntax for switch statement:
switch( switch_expression ){
case case_selector1:
statement1; //
statement2; //block 1
Training-workshop on Object-oriented Programming using Java
44 | P a g e
. . . //
break;
case case_selector2:
statement1; //
statement2; //block 2
. . . //
break;
. . .
default:
statement1; //
statement2; //block n
. . . //
break;
}
The switch expression is first evaluated and transfers the control to a certain case every
time the selector value matched with that of the case. All the statements or block of
statements placed inside the case will be executed. After the break had been encounter,
the control transfers to the first statement after the end of the switch structure.
Example:
int grade = 92;
switch(grade){
case 100:
System.out.println( "Excellent!" );
break;
case 90:
System.out.println("Good job!" );
break;
case 80:
System.out.println("Study harder!" );
break;
default:
System.out.println("Sorry, you failed.");
}
Also remember that a switch statement has no default block, meaning it is optional.
Placing if‐else statement inside a switch statement is possible.
Training-workshop on Object-oriented Programming using Java
45 | P a g e
Repetition Control Structures
Repetition control structures are Java statements that allow us to execute specific
blocks of code a number of times. There are three types of repetition control structures,
the while, do‐while and for loops.
o While loop
The most basic of all looping statements in Java is while. While statements is simply a loop
that executes continuously as long as some condition remains true.
Syntax for while statement:
while( boolean_expression ){
statement1;
statement2;
. . .
}
Example:
int sum=4;
while (sum<=8){
sum=2 + sum;
System.out.println("sum:" + sum);
}
In the given example the purpose of having the statement sum=2+sum; is to halt the
looping in some point. In programming it is good to include this kind of statement or else
your program will loop infinitely.
Figure 6.2: Flow Chart of while statement
Training-workshop on Object-oriented Programming using Java
46 | P a g e
o Do‐while loop
A do‐while loop is similar to a while loop: In a do‐while loop, the condition that stops the
loop isn’t tested until after the statements in the loop has been executed. Statements
place inside the body of a do‐while loop are always executed at least once.
Syntax for do‐while statement:
do{
statement1;
statement2;
. . .
}
while( boolean_expression );
statement1;
Example:
int x=3;
do{
System.out.println("*");
}
while (x<=2);
x++;
Always remember to place semicolon after the while condition and make sure that your
program will halt at certain point to avoid infinite looping.
o For loop
The for loop, like the previous loops, allows execution of the same code a number of
times. Basic principle behind a for loop is that the loop itself maintains a counter variable
that is, a variable whose value is increased each time the body of the loop is executed.
Syntax for for statement:
for (InitializationExpression; LoopCondition; StepExpression){
statement1;
statement2;
. . .
}
These three expressions in the parentheses following the keyword for control how the for
loop works:
o initialization expression is executed before the loop begins.
Training-workshop on Object-oriented Programming using Java
47 | P a g e
o test expression is evaluated each time the loop is executed to determine whether
the loop should keep looping.
o count expression is evaluated each time the loop executes. It’s usually to
increment the counter variable.
Example:
for(int i=3; i<=10; i++)
{
System.out.println("*");
}
Here the variable i is declared and initialized as int with an initial value of 3. Then the
condition i<=10 is evaluated, as long as it results to true the statements preceding the for
loop is executed. An increment is evaluated. This goes again and again as long as the
condition results to true value.
Branching Statements
Branching statements allows us to redirect the flow of program execution. Java
offers three branching statements: break, continue and return.
o Break statement
The break statement, when executed in a while, for, do…while or switch, causes
immediate exit from that statement. Flow of control transfers to the first statement, right
after the control statement.
Example:
String msg = " ";
int i;
for ( i = 1; i <= 7; i++ ) {
if ( i == 3 )
break; //if i equals 3 the loop will break
msg += i + " ";
}
msg += "\nLoop stops at = " + i;
System.out.println( msg );
System.exit( 0 );
Output:
1 2
Loop stops at = 3
Training-workshop on Object-oriented Programming using Java
48 | P a g e
o Continue statement
The continue statement, when executed in a while, for or do…while, skips the current
iteration and proceeds with the next iteration of the loop.
Example:
String msg = " ";
for ( int i = 1; i <= 10; i++ ) {
if ( i == 3 )
continue; //if i equals to 3 skip remaining code
msg += i + " ";
}
msg += "\nLoop skips printing at 3";
System.out.println( msg );
System.exit( 0 );
}
}
Output:
1 2 4 5 6 7 8 9 10
Loop skips printing at 3
The break and continue statements, when used properly, perform faster than the
corresponding structured techniques.
Training-workshop on Object-oriented Programming using Java