0% found this document useful (0 votes)
6 views8 pages

W6-Presentation01-Repetition Control Structures

Repetition control structures in Java, also known as loop statements, allow for the repeated execution of code based on a specified condition. The while loop is a primary example, executing its body as long as the loop condition is true, and can lead to infinite loops if not properly controlled. Various examples illustrate the functionality of while loops, including cases of proper execution, infinite loops, and conditions that prevent execution.

Uploaded by

igcasan.jc07
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)
6 views8 pages

W6-Presentation01-Repetition Control Structures

Repetition control structures in Java, also known as loop statements, allow for the repeated execution of code based on a specified condition. The while loop is a primary example, executing its body as long as the loop condition is true, and can lead to infinite loops if not properly controlled. Various examples illustrate the functionality of while loops, including cases of proper execution, infinite loops, and conditions that prevent execution.

Uploaded by

igcasan.jc07
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/ 8

Repetition Control Structures

Repetition Control Structures


-are Java statements that allow the program to execute statements
repeatedly.
-is also called as loop statement, you can use this statement if you
want your program to perform the desired number of actions.
The following are the repetition structures used in Java:
• The while Loop
-executes the statements repeatedly as long as the condition
is true. The while loop has this form:
while(loop_condition){
//loop body
statements;
}
Below is the flowchart of while loop statement:

The statements inside the while loop will be executed as long as the
loop_condition evaluates to true. If the loop_condition evaluates to false it will exit
or stop the loop.
For example:
int x = 1;
while(x <= 5){
System.out.print(x);
x++;
}
Below is the flowchart for the code snippet above:
The following are other examples of while loops,
Example 1:
int x = 5;
while (x>0)
{
System.out.print (x);
x--;
}

The example above will display 54321. In this case we used decrement
operator. The while loop will stop when the value of x becomes 0.
Example 2:
int x = 0;
while(x<=5){
System.out.println(“Hello World!”);
}

The sample code above will have an infinite loop or it will just display “Hello
World” continuously without stopping. It leads to infinite loop because we didn't put
statement that controls the iteration. In this case, the value of x remains 0 and
loop_condition (x<=5) is will always be true.
Example 3:
//no loops
int x = 5;
while(x<5){
System.out.println(“Hello World!”);
}

The sample code above will display nothing because it will not reach the
statements System.out.println(“Hello World!”); due to loop_condition (x<5)
evaluates to false.
While Loop Sample Program:

You might also like