0% found this document useful (0 votes)
15 views14 pages

Looping Statements

Uploaded by

Ratan Thakur
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)
15 views14 pages

Looping Statements

Uploaded by

Ratan Thakur
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/ 14

LOOPING STATEMENTS

• Looping is a feature which facilitates the execution of a set of


instructions 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.

→While loop
→Do while loop
→For loop
While loop :
• 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 (test_expression)
{
// statements

update_expression;
}
The various parts of the While loop are:
1.Test Expression: In this expression we have to test the condition. If the
condition evaluates to true then we will execute the body of the loop and go to
update expression.
Otherwise, we will exit from the while loop.

Example: i <= 10

2.Update Expression: After executing the loop body, this expression


increments/decrements the loop variable by some value.

Example: i++;
Flow chart while loop (for Control Flow):
• Example : this program will print numbers from 1 to 10

class whileLoopDemo
{
public static void main(String args[])
{
// initialization expression
int i = 1;
// test expression
while (i < =10)
{
System.out.println(i);
// update expression
i++;
}
}
}
Do while loop :
• do-while loop is an Exit control loop. Therefore, unlike for or while loop,
a do-while check for the condition after executing the statements or the loop
body.

Syntax:
do
{
// statements

update_expression ;
}
while (test_expression);
Flowchart do-while loop:
• Example : this program will print numbers from 1 to 10
class dowhileloopDemo
{
public static void main(String args[])
{
// initialisation expression
int i = 1;
do {
// Print the statement
System.out.println(i);
// update expression
i++;
}
// test expression
while (i <=10);
}
}
Difference between while and do while loop:

while do-while
• Condition is checked first then statement(s) is • Statement(s) is executed at least once,
executed. thereafter condition is checked.

• It might occur statement(s) is executed zero


• At least once the statement(s) is executed.
times, If condition is false.

• If there is a single statement, brackets are not


• Brackets are always required.
required.

• while loop is entry controlled loop. • do-while loop is exit controlled loop.
For loop :

• for loop provides a concise way of writing the loop structure. The 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 expr; test expr; update exp)


{
// body of the loop
// statements we want to execute
}
The various parts of the For loop are:

1. Initialization Expression: In this expression, we have to initialize the loop counter


to some value.

Example: int i=1;


2. Test Expression: In this expression, we have to test the condition. If the condition
evaluates to true then, we will execute the body of the loop and go to update
expression. Otherwise, we will exit from the for loop.

Example: i <= 10
3. Update Expression: After executing the loop body, this expression
increments/decrements the loop variable by some value.

Example: i++;
Flow chart for loop (For Control Flow):
• Example : this program will print numbers from 1 to 10

class forLoopDemo
{
public static void main(String args[])
{
// Writing a for loop

for (int i = 1; i <= 5; i++)


{
System.out.println(i);
}
}
}

You might also like