KEMBAR78
Java iterative statements | PDF
Looping (or)
Iterative Statements
Dr. Kuppusamy .P
Associate Professor / SCOPE
Content
• while
• do… while
• For
• enhance for
• continue statements
Dr. Kuppusamy P
Iterative Statements
• Iterative Statements - one or set of statements executed
multiple times based on a condition.
• Three types of Iterative Statements in java
• while
• do… while
• for
• enhanced for
Dr. Kuppusamy P
while loop
Syntax:
while (condition)
{
Statements //Body of the loop
}
Dr. Kuppusamy P
while loop example
public class WhileExample
{
public static void main(String args[])
{
int i = 0;
while (i < 10)
{
System.out.println("i: “ +i);
i++;
}
}
}
Dr. Kuppusamy P
Do - while loop
Syntax:
do
{
Statements //Body of the loop
} while (condition) ;
Dr. Kuppusamy P
Do-while loop example
public class DoWhileExample
{
public static void main(String args[])
{
int i = 1;
do
{
System.out.println("i: “ +i);
i++;
} while (i < 10) ;
}
}
Dr. Kuppusamy P
for loop
Syntax:
for(initialization; condition; increment/decrement)
{
Statements //Body of the loop
}
Dr. Kuppusamy P
Repeat
Yes
No
Statements
Initialization;
Condition;
increment/decrement
for loop example
public class ForExample
{
public static void main(String args[])
{
for (int i=1; i<=5; i++ )
{
System.out.println("i: “ +i);
}
}
}
Dr. Kuppusamy P
Enhanced for loop
Syntax:
for(declaration : Expression)
{
Statements //Body of the loop
}
Dr. Kuppusamy P
Enhanced for loop example
public class EnhancedForExample
{
public static void main(String args[])
{
int [] numbers = {10, 20, 30, 40, 50};
for(int i : numbers ) // for(int i=0; i < numbers; length(); i++)
{
System.out.println("i: “ +i); // System.out.println( “i: “+numbers[i] );
}
}
}
Dr. Kuppusamy P
continue statement
• The continue statement skips the current iteration of a loop
• In while and do loops, continue causes the control to go directly to the test
condition and then continue the iteration process
• In case of for loop, the increment section of the loop is executed before the test
condition is evaluated.
public class ContinueExample
{
public static void main(String[] args)
{
int [] num = {10, 20, 30, 40, 50};
for(int i : num )
{
if( i == 3 )
continue;
}
System.out.println( "i: "+i );
}
Dr. Kuppusamy P
References
Dr. Kuppusamy P
Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition,
2017.

Java iterative statements

  • 1.
    Looping (or) Iterative Statements Dr.Kuppusamy .P Associate Professor / SCOPE
  • 2.
    Content • while • do…while • For • enhance for • continue statements Dr. Kuppusamy P
  • 3.
    Iterative Statements • IterativeStatements - one or set of statements executed multiple times based on a condition. • Three types of Iterative Statements in java • while • do… while • for • enhanced for Dr. Kuppusamy P
  • 4.
    while loop Syntax: while (condition) { Statements//Body of the loop } Dr. Kuppusamy P
  • 5.
    while loop example publicclass WhileExample { public static void main(String args[]) { int i = 0; while (i < 10) { System.out.println("i: “ +i); i++; } } } Dr. Kuppusamy P
  • 6.
    Do - whileloop Syntax: do { Statements //Body of the loop } while (condition) ; Dr. Kuppusamy P
  • 7.
    Do-while loop example publicclass DoWhileExample { public static void main(String args[]) { int i = 1; do { System.out.println("i: “ +i); i++; } while (i < 10) ; } } Dr. Kuppusamy P
  • 8.
    for loop Syntax: for(initialization; condition;increment/decrement) { Statements //Body of the loop } Dr. Kuppusamy P Repeat Yes No Statements Initialization; Condition; increment/decrement
  • 9.
    for loop example publicclass ForExample { public static void main(String args[]) { for (int i=1; i<=5; i++ ) { System.out.println("i: “ +i); } } } Dr. Kuppusamy P
  • 10.
    Enhanced for loop Syntax: for(declaration: Expression) { Statements //Body of the loop } Dr. Kuppusamy P
  • 11.
    Enhanced for loopexample public class EnhancedForExample { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int i : numbers ) // for(int i=0; i < numbers; length(); i++) { System.out.println("i: “ +i); // System.out.println( “i: “+numbers[i] ); } } } Dr. Kuppusamy P
  • 12.
    continue statement • Thecontinue statement skips the current iteration of a loop • In while and do loops, continue causes the control to go directly to the test condition and then continue the iteration process • In case of for loop, the increment section of the loop is executed before the test condition is evaluated. public class ContinueExample { public static void main(String[] args) { int [] num = {10, 20, 30, 40, 50}; for(int i : num ) { if( i == 3 ) continue; } System.out.println( "i: "+i ); } Dr. Kuppusamy P
  • 13.
    References Dr. Kuppusamy P HerbertSchildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition, 2017.