You are on page 1of 4

IT1708

Control Structure (Repetition)


Java provides three (3) repetition (or looping) structures that are used to execute a block of statements repeatedly: while,
for, and do…while loop. while, for, and do are all reserved words.
while Loop
The while loop repeats a block of statements while a given condition evaluates to true. It evaluates the condition before
executing the loop body. The general form of the while loop in Java is:
while (expression) {
//statements
}
The expression is a loop condition enclosed in parentheses. The expression can be a relational or a logical expression that
returns either true or false value. The statements enclosed in curly braces is called the loop body. The loop body can be
either a simple or a compound statement.
When executing the while loop, the expression provides an entry condition. If it evaluates to true, the loop body executes
and the expression is reevaluated. If it evaluates again to true, the loop body continues to execute until the expression
evaluates to false.
A loop that continues to execute endlessly is called an infinite loop. To avoid an infinite loop, make sure that the loop body
contains one (1) or more statements that ensure that the loop condition will eventually be false.
Example:
//this loop prints whole numbers from 0 to 10
int num = 0;
while ( num <= 10) {
System.out.println(num);
num++; //num = num + 1;
}

In the example, the variable, num is a loop control variable, a variable whose value determines whether loop execution
continues. Within the loop body, the statement num++; is used to update the loop control variable until the loop condition
evaluates to false.
for Loop
The for loop executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. It
is used when a definite number of loop iterations is required. The general form of for loop in Java is:
for ( initialization; condition; update ) {
//statements
}

The initialization indicates the starting value for the loop control variable, the condition is the loop condition that controls
the loop entry, and the update is the expression that alters the loop control variable. The parameters of the for loop are
enclosed within parentheses and are separated by two (2) semicolons (;).
The following is the flow of control in a for loop:
1. The initialization executes first and only once.
2. The condition is evaluated. If the loop condition evaluates to false, the loop terminates. If it evaluates to true:
a. Execute the loop body of for.
b. Update the loop control variable.
3. Repeat Step 2 until the loop condition evaluates to false.
Example:
int num;
for ( num = 0; num <= 10; num++ ) {
System.out.println(num);
}

08 Handout 1 *Property of STI


Page 1 of 5
IT1708

do…while Loop
A do…while loop is similar to a while loop, except that it executes the loop body first before evaluating the expression. The
general form of do…while loop in Java is:
do {
//statements
} while ( expression );

The expression appears at the end of the loop, and the statements in the loop are executed once before the expression is
evaluated. If the expression evaluates to true, the control jumps back up to do statement, and the loop body executes
again. This process repeats until the expression evaluates to false.
Example:
int num = 0;
do {
System.out.println(num);
num++;
} while ( num <= 10 );

break and continue Statements


The break statement terminates the loop or switch statement and transfers the flow of the program to the statements
following the loop or switch.
Example of implementing the break statement:
int sum = 0, num;
while (true) {
System.out.println("Type 0 to end program. A negative number is invalid.");
System.out.print("Enter a number to add: ");
num = console.nextInt();

if (num == 0) {
System.out.println("You entered 0. End of program. Thank you!");
break;
}

sum = sum + num;


System.out.println("The sum of entered numbers is: " + sum);
}

The example is an infinite loop, but with the use of a break statement, the loop can be terminated. In the example, when
the user enters a 0, the expression in the if statement evaluates to true, then the appropriate message is printed, and
the break statement terminates the loop.
The break statement terminates only the innermost loop or switch statement that contains the break statement. If a
loop containing a break statement is inside another loop, the break statement ends only the innermost loop. If a break
statement is within a switch statement that is inside a loop, the break statement terminates the switch statement but
not the loop.
The continue statement causes the loop to skip the remainder of its body and immediately reevaluate its condition, and
proceeds with the next iteration of the loop. The following is the process of a continue statement in for and while and
do…while loop:
• In a for loop, the continue statement causes the control of the loop to immediately jump to the update statement,
and then the loop condition is evaluated.
• In a while or do…while loop, using the continue statement makes the control of the loop immediately jumps to
the loop condition.

08 Handout 1 *Property of STI


Page 2 of 5
IT1708

Example of implementing break and continue statements:


int sum = 0, num;
while (true) {
System.out.println("Type 0 to end program. A negative number is invalid.");
System.out.print("Enter a number to add: ");
num = console.nextInt();

if (num == 0) {
System.out.println("You entered 0. End of program. Thank you!");
break;
} else if (num < 0) {
System.out.println(“You entered a negative number.”);
continue;
}

sum = sum + num;


System.out.println("The sum of entered numbers is: " + sum);
}

In the example, when the user enters a negative number, the continue statement executes, and the flow of the loop skips
the remaining statements and jumps back to the logical condition of the while loop.
Note: The break and continue statements are effective ways to avoid extra variables to control a loop and produce a clean
program. The break statement can only terminate the process of an enclosing switch statement or any repetition
structures in which it appears. The continue statement can only appear inside a loop statement but can appear inside a
switch statement only if the switch statement is inside a loop statement.
Nested Control Structures
Nested control structures are control statements that are placed within another. Control structures in Java can be nested in
many levels. When nesting control structures, you must make sure that their start and end braces are placed correctly within
its control structure. The following are some examples of nested control structures with different levels:
Example #1: for loop nested in if…else statement
System.out.print("Enter a range of loop greater than 0: ");
int range = console.nextInt();
if (range > 0) {
System.out.println("The output of the loop is: ");
for (int i = 1; i <= range; i++) {
System.out.println(i);
} //end of for loop
} else {
System.out.println("The entered number is invalid.");
} //end of if…else statement

Example #2: Nested for loop


int x, y;
System.out.println("Multiplication Table of 1-10:");
for (x = 1; x <= 10; x++) {
for (y = 1; y <= 10; y++) {
System.out.print(x * y + "\t");
} //end of inner for loop
System.out.println();
} //end of outer for loop

08 Handout 1 *Property of STI


Page 3 of 5
IT1708

Example #3: for loop nested in if…else statement within while loop
char flag = 'y'; int range;
while (flag != 'n') {
System.out.print("Enter the range of loop greater than 0: ");
range = console.nextInt();
if (range > 0) {
for (int i = 1; i <= range; i++) {
System.out.print(i + ", ");
} //end of inner for loop
} else {
System.out.println("Invalid number. Try again!");
continue;
} //end of inner if…else statement
System.out.print("\nRepeat the program? (y/n): ");
flag = console.next().charAt(0);
} //end of outer while loop

REFERENCES:
Baesens, B., Backiel, A., & Broucke, S. (2015). Beginning java programming: The object-oriented approach. Indiana: John
Wiley & Sons, Inc.
Farrell, J. (2014). Java programming, 7th edition. Boston: Course Technology, Cengage Learning.
Savitch, W. (2014). Java: An introduction to problem solving and programming, 7th edition. California: Pearson Education,
Inc.

08 Handout 1 *Property of STI


Page 4 of 4

You might also like