0% found this document useful (0 votes)
3 views3 pages

Loop Notes

The document explains looping in Java, detailing three types of loops: for loop, while loop, and do-while loop. Each loop type has its own syntax and characteristics, with for loops being concise, while loops checking conditions before execution, and do-while loops executing at least once before checking conditions. The document provides syntax examples and descriptions of how each loop operates and terminates.

Uploaded by

Uday Sharma
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)
3 views3 pages

Loop Notes

The document explains looping in Java, detailing three types of loops: for loop, while loop, and do-while loop. Each loop type has its own syntax and characteristics, with for loops being concise, while loops checking conditions before execution, and do-while loops executing at least once before checking conditions. The document provides syntax examples and descriptions of how each loop operates and terminates.

Uploaded by

Uday Sharma
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/ 3

LOOPS IN JAVA

Looping in programming languages is a feature which facilitates the execution of a


set of instructions/functions repeatedly while some condition evaluates to true. Java
provides three ways for executing the loops. While all the ways provide similar basic
functionality, they differ in their syntax and condition checking time.

FOR LOOP: For loop provides a concise way of writing the loop structure. Unlike a
while loop, a for statement consumes the initialization, condition and
increment/decrement in one line thereby providing a shorter, easy to debug structure
of looping.
Syntax:
for (initialization condition; testing condition; increment/decrement)
{
statement(s)
}
Flowchart: for-loop-in-java

Initialization condition: Here, we initialize the variable in use. It marks the start
of a for loop. An already declared variable can be used or a variable can be declared,
local to loop only.
Testing Condition: It is used for testing the exit condition for a loop. It must return
a boolean value. It is also an Entry Control Loop as the condition is checked prior to
the execution of the loop statements.
Statement execution: Once the condition is evaluated to true, the statements in
the loop body are executed.
Increment/ Decrement: It is used for updating the variable for next iteration.
Loop termination: When the condition becomes false, the loop terminates marking
the end of its life cycle.

WHILE LOOP: A while loop is a control flow statement that allows code to be
executed repeatedly based on a given Boolean condition. The while loop can be
thought of as a repeating if statement.
Syntax :
while (boolean condition)
{
loop statements...
}
Flowchart: while-loop-in-java

 While loop starts with the checking of Boolean condition. If it evaluated to true,
then the loop body statements are executed otherwise first statement following the
loop is executed. For this reason it is also called Entry control loop
 Once the condition is evaluated to true, the statements in the loop body are
executed. Normally the statements contain an update value for the variable being
processed for the next iteration.
 When the condition becomes false, the loop terminates which marks the end of its
life cycle.
DO - WHILE LOOP: do while loop is similar to while loop with only difference that
it checks for condition after executing the statements, and therefore is an example
of Exit Control Loop.
Syntax:
do
{
statements..
}
while (condition);

Flowchart: do – while loop-in-java

 do while loop starts with the execution of the statement(s). There is no checking
of any condition for the first time.
 After the execution of the statements, and update of the variable value, the
condition is checked for true or false value. If it is evaluated to true, next
iteration of loop starts.
 When the condition becomes false, the loop terminates which marks the end of
its life cycle.
 It is important to note that the do-while loop will execute its statements atleast
once before any condition is checked, and therefore is an example of exit control
loop.

You might also like