KEMBAR78
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC | PPTX
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

LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC

  • 1.
  • 2.
    Outline Looping Statement While loop Do-whileloop For loop For each loop Nested Loops Break Statement Continue Statement
  • 3.
    Looping Statement  Loopingin 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 througha 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  whileloop 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
  • 6.
  • 7.
    Do-while Loop  Thedo/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-whileloop 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
  • 9.
  • 10.
    For Loop  forloop 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 // Programto 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
  • 13.
  • 14.
    For Each Loopin 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 Loopin Java Syntax: for (type var : array) { statements using var; }
  • 16.
    For Each Loopin Java  Example
  • 17.
    Nested Loops inJava  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 Patternusing 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 Patternusing 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.
  • 23.
    Break Statement Example classTest { 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 withInner 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(inti=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 classMain { 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 withInner 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 withLabeled 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