This document discusses three types of loops in Java programming: for loops, while loops, and do-while loops. It provides the syntax, flow diagrams, and examples of each loop type. Additionally, it covers the break and continue statements that can be used within loops to alter their flow control, with examples of how each statement works.
THREE TYPE OFLOOP
• For loop
• While loop
• Do while loop
3.
FOR LOOP
A forloop is a repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number of times.
4.
SYNTAX
Here is theflow of control in a 'for' loop −
The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables.
Next, the condition is evaluated. If it is true, the body of the loop is executed.
After the body of the 'for' loop executes, the flow of control jumps back up to
the increment statement.
The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition).
WHILE LOOP
A whileloop in java programming repeatedly executes a target statement as long as a
given condition is true.
8.
SYNTAX
The syntax ofa while loop in java programming language is −
Here, statement(s) may be a single statement or a block of statements.
Thecondition may be any expression, and true is any nonzero value. The loop
iterates while the condition is true.
When the condition becomes false, the program control passes to the line
immediately following the loop.
DO WHILE LOOP
•A do...while loop is similar to a while loop, except the fact that it is guaranteed to
execute at least one time.
12.
SYNTAX
The syntax ofa do...while loop in C programming language is −
Notice that the conditional expression appears at the end of the loop, so the
statement(s) in the loop executes once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in
the loop executes again. This process repeats until the given condition becomes false.
BREAK STATEMENT
Terminates theloop statement and transfers execution to the statement immediately
following the loop or switch.
When a break statement is encountered inside a loop, the loop is immediately
terminated and the program control resumes at the next statement following the loop.
CONTINUE STATEMENTS
Causes theloop to skip the remainder of its body and immediately retest its condition
prior to reiterating.
The continue statement in C programming works somewhat like the breakstatement.
Instead of forcing termination, it forces the next iteration of the loop to take place,
skipping any code in between.