SlideShare a Scribd company logo
LOOPING STATEMENTS
Outline
Looping Statement
While loop
Do-while loop
For loop
For each loop
Nested Loops
Break Statement
Continue Statement
Looping Statement
 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.
 While loop
 Do-while loop
 For
While Loop
Loops through a block of code as long as a specified condition is true:
Syntax:
while (condition)
{
//code to be executed
Increment / decrement statement
}
Note:
Do not forget to increase the variable used in the condition,
otherwise the loop will never end!
While Loop
 while loop is used to iterate a part of the program several times.
 while is an example of entry controlled loop.
 If the number of iteration is not fixed, it is recommended to use while loop.
// Program to display numbers from 1 to 5
class Main
{
public static void main(String[] args)
{
int i = 1, n = 5;
while(i <= n)
{
System.out.println(i);
i++;
}
}
}
Outpu
t
1
2
3
4
5
While Loop
Do-while Loop
 The do/while loop is a variant of the while loop.
 This loop will execute the code block once, before checking if the condition is
true, then it will repeat the loop as long as the condition is true.
 Syntax:
do{
//code to be executed / loop body
//update statement
}while(condition);
Do-while Loop
 do-while loop is executed at least once because condition is checked after loop
body.
 the do-while loop is an example of an exit-controlled loop.
// Java Program to display numbers from 1 to 5
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
int i = 1, n = 5;
do
{
System.out.println(i);
i++;
} while(i <= n);
}
}
Outpu
t
1
2
3
4
5
Do-while Loop
For Loop
 for loop is used for executing a part of the
program repeatedly.
 When the number of iteration is fixed then
it is suggested to use for loop
Syntax:
for(initialization; condition; increment/
decrement)
{
//statement or code to be executed
}
For Loop
1. Initialization:
 It is the initial condition which is executed once when the loop starts.
 Here, we can initialize the variable, or we can use an already initialized variable.
2.Condition:
 It is the second condition which is executed each time to test the condition of the loop.
 It continues execution until the condition is false.
 It must return boolean value either true or false.
3. Increment/Decrement(Updation):
 It increments or decrements the variable value.
4. Statement:
 The statement of the loop is executed each time until the second condition is false.
For Loop
// Program to print numbers from 1 to 5
class Main
{
public static void main(String[]
args)
{
int n = 5;
for (int i = 1; i <= n; ++i)
{
System.out.println(i);
}
}
}
Outpu
t
1
2
3
4
5
For Loop
For Each Loop in Java
 For-each is another array traversing technique like for loop, while loop, do-while
loop introduced in Java5.
 It starts with the keyword for like a normal for-loop.
 Instead of declaring and initializing a loop counter variable, you declare a
variable that is the same type as the base type of the array, followed by a colon,
which is then followed by the array name.
 In the loop body, This variable is used to access the array/collection elements
without any indexing.
 It’s commonly used to iterate over an array or a Collections class (eg, ArrayList)
For Each Loop in Java
Syntax:
for (type var : array)
{
statements using var;
}
For Each Loop in Java
 Example
Nested Loops in Java
 Every loop consists of below 3 things inside
them:
 Initialization: This step refers to initializing the
value of the counter.
 Condition: This condition refers to the
comparison that needs to be true for continuing
the execution of the loop.
 Increment/Decrement of counter: This refers to
the operation to be performed on the counter
once one flow of loop ends.
 In the case of nested loops, the above three
steps are checked for each loop inside it.
 Thus with each flow of the outer loop, the inner
loop executes completely, which means if we
have m flows for the outer loop and n number
of flows for the outer loop, then this loop
Half Pyramid Pattern using Nested Loops
// Java Program to print a Half pyramid pattern
class Demo
{
public static void main(String[] args)
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(i>=j)
{
System.out.print('*’);
}
}
System.out.println("n");
}
}
}
Half Pyramid Pattern using Nested Loops
Programs to perform (Looping Statements)
 Write a program to print first n odd numbers.
 Write a program to check that the given number is prime or not.
 Write a program to draw given patterns.
Java Break Statement
 Break statement is used to break loop or switch statement.
 It breaks the current flow of the program at specified condition.
 We can use Java break statement in all types of loops such as
 for loop
 while loop
 do-while loop
Working of Break Statement
Break Statement Example
class Test
{
public static void main(String[] args)
{
for (int i = 1; i <= 10; ++i)
{
if (i == 5)
{
break;
}
System.out.println(i);
}
}
}
Outpu
t
1
2
3
4
 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.
Break Statement with Inner Loop
//outer loop
for(int i=1;i<=3;i++)
{
//inner loop
for(int j=1;j<=3;j++)
{
if(i==2&&j==2)
{
break;
}
System.out.println(i+" "+j);
}
}
Outp
ut:
1 1
1 2
1 3
2 1
3 1
3 2
3 3
Labeled Break Statement
aa:
for(int i=1;i<=3;i++)
{
bb:
for(int j=1;j<=3;j++)
{
if(i==2&&j==2)
{
break aa;
}
System.out.println(i+" "+j);
}
}
Outp
ut:
1 1
1 2
1 3
2 1
Java Continue Statement
 Continue statement
breaks one iteration (in
the loop), if a specified
condition occurs, and
continues with the next
iteration in the loop.
Continue Statement Example
class Main
{
public static void main(String[] args)
{
for (int i = 1; i <= 10; ++i)
{
// if value of i is between 4 and 9 continue is executed
if (i > 4 && i < 9)
{
continue;
}
System.out.println(i);
}
}
}
Outp
ut
1
2
3
4
9
10
 The continue statement is used in loop control
structure when you need to immediately jump to the
next iteration of the loop.
Continue Statement with Inner Loop
//outer loop
for(int i=1;i<=3;i++)
{
//inner loop
for(int j=1;j<=3;j++)
{
if(i==2&&j==2)
{
continue;
}
System.out.println(i+" "+j);
}
}
Outp
ut
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
Continue Statement with Labeled For Loop
aa:
for(int i=1;i<=3;i++)
{
bb:
for(int j=1;j<=3;j++)
{
if(i==2&&j==2)
{
continue aa;
}
System.out.println(i+" "+j);
}
}
Outpu
t
1 1
1 2
1 3
2 1
3 1
3 2
3 3

More Related Content

Similar to LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC (20)

PPT
Ch4_part1.ppt
RuthikReddyGarlapati
 
PPT
Ch4_part1.ppt
ssuserc70988
 
PPTX
Lecture - 5 Control Statement
manish kumar
 
PPT
Looping statements in Java
Jin Castor
 
PPT
15-Loops.ppt
harshsolankurephotos
 
PPTX
Computer programming 2 Lesson 8
MLG College of Learning, Inc
 
PPT
object orineted programming looping .ppt
zeenatparveen24
 
PPTX
Unit-02 Selection, Mathematical Functions and loops.pptx
jessicafalcao1
 
PPTX
for loop in java
Majid Ali
 
PPTX
Java loops for, while and do...while
Jayfee Ramos
 
PPT
Programming loop
University of Potsdam
 
DOCX
Java loops
ricardovigan
 
PPTX
Looping statements
AbhishekMondal42
 
PPT
Computer Programming, Loops using Java
Mahmoud Alfarra
 
PPTX
Loop
prabhat kumar
 
PPT
Chap05
Terry Yoast
 
PPTX
Pi j1.4 loops
mcollison
 
PPTX
Loop(for, while, do while) condition Presentation
Badrul Alam
 
PPTX
Looping statements
Marco Alzamora
 
PPTX
Java chapter 3
Abdii Rashid
 
Ch4_part1.ppt
RuthikReddyGarlapati
 
Ch4_part1.ppt
ssuserc70988
 
Lecture - 5 Control Statement
manish kumar
 
Looping statements in Java
Jin Castor
 
15-Loops.ppt
harshsolankurephotos
 
Computer programming 2 Lesson 8
MLG College of Learning, Inc
 
object orineted programming looping .ppt
zeenatparveen24
 
Unit-02 Selection, Mathematical Functions and loops.pptx
jessicafalcao1
 
for loop in java
Majid Ali
 
Java loops for, while and do...while
Jayfee Ramos
 
Programming loop
University of Potsdam
 
Java loops
ricardovigan
 
Looping statements
AbhishekMondal42
 
Computer Programming, Loops using Java
Mahmoud Alfarra
 
Chap05
Terry Yoast
 
Pi j1.4 loops
mcollison
 
Loop(for, while, do while) condition Presentation
Badrul Alam
 
Looping statements
Marco Alzamora
 
Java chapter 3
Abdii Rashid
 

Recently uploaded (20)

PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PDF
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Ad

LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC

  • 2. Outline Looping Statement While loop Do-while loop For loop For each loop Nested Loops Break Statement Continue Statement
  • 3. Looping Statement  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.  While loop  Do-while loop  For
  • 4. While Loop Loops through a block of code as long as a specified condition is true: Syntax: while (condition) { //code to be executed Increment / decrement statement } Note: Do not forget to increase the variable used in the condition, otherwise the loop will never end!
  • 5. While Loop  while loop is used to iterate a part of the program several times.  while is an example of entry controlled loop.  If the number of iteration is not fixed, it is recommended to use while loop. // Program to display numbers from 1 to 5 class Main { public static void main(String[] args) { int i = 1, n = 5; while(i <= n) { System.out.println(i); i++; } } } Outpu t 1 2 3 4 5
  • 7. Do-while Loop  The do/while loop is a variant of the while loop.  This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.  Syntax: do{ //code to be executed / loop body //update statement }while(condition);
  • 8. Do-while Loop  do-while loop is executed at least once because condition is checked after loop body.  the do-while loop is an example of an exit-controlled loop. // Java Program to display numbers from 1 to 5 import java.util.Scanner; class Main { public static void main(String[] args) { int i = 1, n = 5; do { System.out.println(i); i++; } while(i <= n); } } Outpu t 1 2 3 4 5
  • 10. For Loop  for loop is used for executing a part of the program repeatedly.  When the number of iteration is fixed then it is suggested to use for loop Syntax: for(initialization; condition; increment/ decrement) { //statement or code to be executed }
  • 11. For Loop 1. Initialization:  It is the initial condition which is executed once when the loop starts.  Here, we can initialize the variable, or we can use an already initialized variable. 2.Condition:  It is the second condition which is executed each time to test the condition of the loop.  It continues execution until the condition is false.  It must return boolean value either true or false. 3. Increment/Decrement(Updation):  It increments or decrements the variable value. 4. Statement:  The statement of the loop is executed each time until the second condition is false.
  • 12. For Loop // Program to print numbers from 1 to 5 class Main { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; ++i) { System.out.println(i); } } } Outpu t 1 2 3 4 5
  • 14. For Each Loop in Java  For-each is another array traversing technique like for loop, while loop, do-while loop introduced in Java5.  It starts with the keyword for like a normal for-loop.  Instead of declaring and initializing a loop counter variable, you declare a variable that is the same type as the base type of the array, followed by a colon, which is then followed by the array name.  In the loop body, This variable is used to access the array/collection elements without any indexing.  It’s commonly used to iterate over an array or a Collections class (eg, ArrayList)
  • 15. For Each Loop in Java Syntax: for (type var : array) { statements using var; }
  • 16. For Each Loop in Java  Example
  • 17. Nested Loops in Java  Every loop consists of below 3 things inside them:  Initialization: This step refers to initializing the value of the counter.  Condition: This condition refers to the comparison that needs to be true for continuing the execution of the loop.  Increment/Decrement of counter: This refers to the operation to be performed on the counter once one flow of loop ends.  In the case of nested loops, the above three steps are checked for each loop inside it.  Thus with each flow of the outer loop, the inner loop executes completely, which means if we have m flows for the outer loop and n number of flows for the outer loop, then this loop
  • 18. Half Pyramid Pattern using Nested Loops // Java Program to print a Half pyramid pattern class Demo { public static void main(String[] args) { for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { if(i>=j) { System.out.print('*’); } } System.out.println("n"); } } }
  • 19. Half Pyramid Pattern using Nested Loops
  • 20. Programs to perform (Looping Statements)  Write a program to print first n odd numbers.  Write a program to check that the given number is prime or not.  Write a program to draw given patterns.
  • 21. Java Break Statement  Break statement is used to break loop or switch statement.  It breaks the current flow of the program at specified condition.  We can use Java break statement in all types of loops such as  for loop  while loop  do-while loop
  • 22. Working of Break Statement
  • 23. Break Statement Example class Test { public static void main(String[] args) { for (int i = 1; i <= 10; ++i) { if (i == 5) { break; } System.out.println(i); } } } Outpu t 1 2 3 4  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.
  • 24. Break Statement with Inner Loop //outer loop for(int i=1;i<=3;i++) { //inner loop for(int j=1;j<=3;j++) { if(i==2&&j==2) { break; } System.out.println(i+" "+j); } } Outp ut: 1 1 1 2 1 3 2 1 3 1 3 2 3 3
  • 25. Labeled Break Statement aa: for(int i=1;i<=3;i++) { bb: for(int j=1;j<=3;j++) { if(i==2&&j==2) { break aa; } System.out.println(i+" "+j); } } Outp ut: 1 1 1 2 1 3 2 1
  • 26. Java Continue Statement  Continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
  • 27. Continue Statement Example class Main { public static void main(String[] args) { for (int i = 1; i <= 10; ++i) { // if value of i is between 4 and 9 continue is executed if (i > 4 && i < 9) { continue; } System.out.println(i); } } } Outp ut 1 2 3 4 9 10  The continue statement is used in loop control structure when you need to immediately jump to the next iteration of the loop.
  • 28. Continue Statement with Inner Loop //outer loop for(int i=1;i<=3;i++) { //inner loop for(int j=1;j<=3;j++) { if(i==2&&j==2) { continue; } System.out.println(i+" "+j); } } Outp ut 1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3
  • 29. Continue Statement with Labeled For Loop aa: for(int i=1;i<=3;i++) { bb: for(int j=1;j<=3;j++) { if(i==2&&j==2) { continue aa; } System.out.println(i+" "+j); } } Outpu t 1 1 1 2 1 3 2 1 3 1 3 2 3 3