SlideShare a Scribd company logo
Chapter five Java
Iteration statement
There may be a situation when we need to execute a block of code
several number of times, and is often referred to as a loop.
Java has very flexible three looping mechanisms. You can use one of
the following three loops:
 while Loop
 do...while Loop
 for Loop
While loop
A while loop is a control structure that allows you to repeat a
task a certain number of times.
Syntax:
The syntax of a while loop is:
while(Boolean_expression)
{
//Statements
}
Continue..
When executing, if the boolean_expression result is true, then the
actions inside the loop will be executed. This will continue as long as
the expression result is true.
Here, key point of the while loop is that the loop might not ever run.
When the expression is tested and the result is false, the loop body will
be skipped and the first statement after the while loop will be
executed.
Example
public class Test {
public static void main(String args[]) {
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("n");
}
}
}
Do while loop
A do...while loop is similar to a while loop, except that a do...while loop
is guaranteed to execute at least one time.
Syntax:
The syntax of a do...while loop is:
do
{
//Statements
}while(Boolean_expression);
Continue..
Notice that the Boolean expression appears at the end of the loop, so
the statements in the loop execute once before the Boolean is tested.
If the Boolean expression is true, the flow of control jumps back up to
do, and the statements in the loop execute again.
This process repeats until the Boolean expression is false.
Example
public class Test {
public static void main(String args[]){
int x = 10;
do{
System.out.print("value of x : " + x );
x++;
System.out.print("n");
}while( x < 20 );
}
}
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. A for loop is
useful when you know how many times a task is to be repeated.
Syntax:
The syntax of a for loop is:
for(initialization; Boolean_expression; update)
{
//Statements
}
Continue..
Here is the flow of control in a for loop:
The initialization step is executed first, and only once. This step allows
you to declare and initialize any loop control variables.
You are not required to put a statement here, as long as a semicolon
appears. Next, the Boolean expression is evaluated. If it is true, the
body of the loop is executed.
Continue..
If it is false, the body of the loop does not execute and flow of control
jumps to the next statement past the for loop.
After the body of the for loop executes, the flow of control jumps back
up to the update statement. This statement allows you to update any
loop control variables.
This statement can be left blank, as long as a semicolon appears after
the Boolean expression.
Continue..
The Boolean expression is now evaluated again. If it is true, the
loop executes and the process repeats itself (body of loop, then update
step, then Boolean expression). After the Boolean expression is false,
the for loop terminates.
Example
public class Test {
public static void main(String args[]) {
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("n");
}
}
}
The Break Keyword
The break keyword is used to stop the entire loop. The break keyword
must be used inside any loop or a switch statement.
The break keyword will stop the execution of the innermost loop
and start executing the next line of code after the block.
Syntax:
The syntax of a break is a single statement inside any loop:
break;
The continue Keyword
The continue keyword can be used in any of the loop control structures.
It causes the loop to immediately jump to the next iteration of the loop.
In a for loop, the continue keyword causes flow of control to immediately
jump to the update statement.
In a while loop or do/while loop, flow of control immediately jumps to the
Boolean expression.
Syntax:
The syntax of a continue is a single statement inside any loop:
continue;
Thank You

More Related Content

PDF
Java conditional statements
PPTX
Java loops for, while and do...while
PPTX
02 - Prepcode
DOCX
Java loops
PPTX
Java Decision Control
PPTX
Control flow statements in java
PPTX
Phasers to stunning
PPT
M C6java6
Java conditional statements
Java loops for, while and do...while
02 - Prepcode
Java loops
Java Decision Control
Control flow statements in java
Phasers to stunning
M C6java6

What's hot (20)

PPTX
Loops in java script
DOC
Jumping statements
PPTX
PPT
Java control flow statements
PPTX
Control structures in java
PPSX
C lecture 4 nested loops and jumping statements slideshare
PPSX
Break and continue
PPTX
Java Control Statements
PPTX
Loops in C
PPT
03 conditions loops
PPTX
Loops in C Programming Language
PDF
Java input Scanner
PPTX
07 flow control
PPT
M C6java5
PPTX
Loops in c programming
PPT
Decision making and looping
PDF
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
PDF
Java Repetiotion Statements
PPT
Iteration
Loops in java script
Jumping statements
Java control flow statements
Control structures in java
C lecture 4 nested loops and jumping statements slideshare
Break and continue
Java Control Statements
Loops in C
03 conditions loops
Loops in C Programming Language
Java input Scanner
07 flow control
M C6java5
Loops in c programming
Decision making and looping
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Java Repetiotion Statements
Iteration
Ad

Similar to Chapter 5 java (20)

PPTX
Computer programming 2 Lesson 8
PPTX
dizital pods session 5-loops.pptx
PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
PPT
Looping statements in Java
PPTX
Loop(for, while, do while) condition Presentation
PDF
how to write loops in java explained vividly
PPTX
130707833146508191
PPTX
Control statements in java
PPTX
Java chapter 3
PPTX
Looping statements
PPT
Chap05
PPTX
Looping statements
PPTX
Lecture - 5 Control Statement
PPTX
Java 2.pptx
PPTX
Chapter 2 : Programming with Java Statements
PPT
Programming loop
PPTX
Introduction to Java Programming - Lecture 11.pptx
PPTX
Pi j1.4 loops
PPT
Repetition Structure
PPT
Loops Do While Arduino Programming Robotics
Computer programming 2 Lesson 8
dizital pods session 5-loops.pptx
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
Looping statements in Java
Loop(for, while, do while) condition Presentation
how to write loops in java explained vividly
130707833146508191
Control statements in java
Java chapter 3
Looping statements
Chap05
Looping statements
Lecture - 5 Control Statement
Java 2.pptx
Chapter 2 : Programming with Java Statements
Programming loop
Introduction to Java Programming - Lecture 11.pptx
Pi j1.4 loops
Repetition Structure
Loops Do While Arduino Programming Robotics
Ad

More from Ahmad sohail Kakar (20)

PPTX
Lec 1 network types
PPTX
Lec 1 introduction
PPTX
Active directory restoration
PPTX
Active directory backup
PPT
Seii unit7 component-level-design
PPT
Seii unit6 software-testing-techniques
PPT
Seii unit5 ui_design
PPT
Seii unit4 software_process
PPT
Se ii unit3-architectural-design
PPT
Se ii unit2-software_design_principles
PPT
Se ii unit1-se_ii_intorduction
PDF
Second chapter-java
DOCX
Second chapter-java
PPTX
Chapter 9 java
PPTX
Chapter 8 java
PPTX
Chapter 7 java
PPTX
Chapter 6 java
PPTX
Chapter 4 java
PPTX
Chapter 3 java
PPTX
Chapter 2 java
Lec 1 network types
Lec 1 introduction
Active directory restoration
Active directory backup
Seii unit7 component-level-design
Seii unit6 software-testing-techniques
Seii unit5 ui_design
Seii unit4 software_process
Se ii unit3-architectural-design
Se ii unit2-software_design_principles
Se ii unit1-se_ii_intorduction
Second chapter-java
Second chapter-java
Chapter 9 java
Chapter 8 java
Chapter 7 java
Chapter 6 java
Chapter 4 java
Chapter 3 java
Chapter 2 java

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Types and Its function , kingdom of life
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Lesson notes of climatology university.
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
RMMM.pdf make it easy to upload and study
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
master seminar digital applications in india
Classroom Observation Tools for Teachers
Supply Chain Operations Speaking Notes -ICLT Program
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Basic Mud Logging Guide for educational purpose
Microbial diseases, their pathogenesis and prophylaxis
Cell Types and Its function , kingdom of life
102 student loan defaulters named and shamed – Is someone you know on the list?
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pre independence Education in Inndia.pdf
Lesson notes of climatology university.
Insiders guide to clinical Medicine.pdf
Final Presentation General Medicine 03-08-2024.pptx
GDM (1) (1).pptx small presentation for students
RMMM.pdf make it easy to upload and study
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
human mycosis Human fungal infections are called human mycosis..pptx
master seminar digital applications in india

Chapter 5 java

  • 2. Iteration statement There may be a situation when we need to execute a block of code several number of times, and is often referred to as a loop. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while Loop  for Loop
  • 3. While loop A while loop is a control structure that allows you to repeat a task a certain number of times. Syntax: The syntax of a while loop is: while(Boolean_expression) { //Statements }
  • 4. Continue.. When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long as the expression result is true. Here, key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
  • 5. Example public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System.out.print("value of x : " + x ); x++; System.out.print("n"); } } }
  • 6. Do while loop A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time. Syntax: The syntax of a do...while loop is: do { //Statements }while(Boolean_expression);
  • 7. Continue.. Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested. If the Boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute again. This process repeats until the Boolean expression is false.
  • 8. Example public class Test { public static void main(String args[]){ int x = 10; do{ System.out.print("value of x : " + x ); x++; System.out.print("n"); }while( x < 20 ); } }
  • 9. 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. A for loop is useful when you know how many times a task is to be repeated. Syntax: The syntax of a for loop is: for(initialization; Boolean_expression; update) { //Statements }
  • 10. Continue.. Here is the flow of control in a for loop: The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears. Next, the Boolean expression is evaluated. If it is true, the body of the loop is executed.
  • 11. Continue.. If it is false, the body of the loop does not execute and flow of control jumps to the next statement past the for loop. After the body of the for loop executes, the flow of control jumps back up to the update statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the Boolean expression.
  • 12. Continue.. The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then update step, then Boolean expression). After the Boolean expression is false, the for loop terminates.
  • 13. Example public class Test { public static void main(String args[]) { for(int x = 10; x < 20; x = x+1) { System.out.print("value of x : " + x ); System.out.print("n"); } } }
  • 14. The Break Keyword The break keyword is used to stop the entire loop. The break keyword must be used inside any loop or a switch statement. The break keyword will stop the execution of the innermost loop and start executing the next line of code after the block. Syntax: The syntax of a break is a single statement inside any loop: break;
  • 15. The continue Keyword The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop. In a for loop, the continue keyword causes flow of control to immediately jump to the update statement. In a while loop or do/while loop, flow of control immediately jumps to the Boolean expression. Syntax: The syntax of a continue is a single statement inside any loop: continue;