0% found this document useful (0 votes)
2 views

Loops

Uploaded by

aroojsagir
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)
2 views

Loops

Uploaded by

aroojsagir
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/ 21

WELCOME TO OUR

P R E S E N TAT I O N
THREE TYPE OF LOOP
• For loop
• While loop
• Do while loop
FOR LOOP

A for loop is a repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number of times.
SYNTAX

Here is the flow 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).
FLOW DIAGRAM
EXAMPLE
#Program:
for(int i=0;i<3;i++){
System.out.println("Using For loop: "+i);
}

#Output:
Using For loop: 0
Using For loop: 1
Using For loop: 2
WHILE LOOP

A while loop in java programming repeatedly executes a target statement as long as a


given condition is true.
SYNTAX

The syntax of a 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.
FLOW DIAGRAM
EXAMPLE
#Program:
int i=0;
while(i<3){
System.out.println("Using While loop: "+i);
i++;
}

#Output:
Using While loop: 0
Using While loop: 1
Using While loop: 2
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.
SYNTAX

The syntax of a 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.
FLOW DIAGRAM
EXAMPLE:
#Program:
int i=0;
do{
System.out.println("Using Do while loop: "+i);
i++;
}
while(i<3);

#Output:
Using Do while loop: 0
Using Do while loop: 1
Using Do while loop: 2
BREAK STATEMENT

Terminates the loop 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.
FLOW DIAGRAM
EXAMPLE
#Program:
for(int i=0;i<4;i++){
if(i==2)break;
System.out.println("Output:"+i);
}

#Output:
Output:0
Output:1
CONTINUE STATEMENTS

Causes the loop 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.
FLOW DIAGRAM
EXAMPLE
#Program:
for(int i=0;i<4;i++){
if(i==2)continue;
System.out.println("Output:"+i);
}

#Output:
Output:0
Output:1
Output:3
Thank You.

You might also like