SlideShare a Scribd company logo
Chapter 4
Repetition Statements (loops)
1
2
Loops – While, Do, For
• Repetition Statements
– While
– Do
– For
3
Repetition Statements
• Repetition statements allow us to execute a
statement or a block of statements multiple times
• Often they are referred to as loops
• Like conditional statements, they are controlled by
boolean expressions
• Java has three kinds of repetition statements:
while
do
for
• The programmer should choose the right kind of
loop statement for the situation
4
The while Statement
• A while statement has the following syntax:
• If the condition is true, the statement is
executed
• Then the condition is evaluated again, and if it is
still true, the statement is executed again
• The statement is executed repeatedly until the
condition becomes false
while ( condition )
statement;
Logic of a while Loop
statement
true false
condition
evaluated
5
6
The while Statement
• An example of a while statement:
• If the condition of a while loop is false
initially, the statement is never executed
• Therefore, the body of a while loop will
execute zero or more times
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
Trace while Loop
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
Initialize count
animation
7
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
(count < 2) is true
animation
8
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
Print Welcome to Java
animation
9
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
Increase count by 1
count is 1 now
animation
10
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
(count < 2) is still true since
count is 1
animation
11
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
Print Welcome to Java
animation
12
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
Increase count by 1
count is 2 now
animation
13
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
(count < 2) is false since count is
2 now
animation
14
Trace while Loop
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
The loop exits. Execute the next
statement after the loop.
animation
15
16
Example (Average.java)
17
Infinite Loops
• Executing the statements in the body of a while
loop must eventually make the condition false
• If not, it is called an infinite loop, which will
execute until the user interrupts the program
• This is a common logical error
• You should always double check the logic of a
program to ensure that your loops will terminate
18
Infinite Loops
• An example of an infinite loop:
• This loop will continue executing until the
user externally interrupts the program.
int count = 1;
while (count <= 25)
{
System.out.println(count);
count = count - 1;
}
19
Nested Loops
• Similar to nested if statements, loops can
be nested as well
• That is, the body of a loop can contain
another loop
• For each iteration of the outer loop, the
inner loop iterates completely
20
Nested Loops
• How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 <= 20)
{
System.out.println ("Here");
count2++;
}
count1++;
}
10 * 20 = 200

More Related Content

PPT
Lecture on repetition statements (loops)
PPT
object orineted programming looping .ppt
PPT
Java Programming: Loops
PPT
Programming loop
PPTX
Introduction to Java Programming - Lecture 11.pptx
PPTX
Looping statements
PPT
15-Loops.ppt
PPTX
Looping statements
Lecture on repetition statements (loops)
object orineted programming looping .ppt
Java Programming: Loops
Programming loop
Introduction to Java Programming - Lecture 11.pptx
Looping statements
15-Loops.ppt
Looping statements

Similar to C Programming Looping Statements For Students (20)

PPTX
While , For , Do-While Loop
PDF
PPTX
Going loopy - Introduction to Loops.pptx
PPT
Week2 ch4 part1edited 2020
PPT
Week2 ch4 part1edited 2020
PDF
Loop and while Loop
PPTX
PPT
Looping statements in Java
PPTX
While loop
PDF
DSA 103 Object Oriented Programming :: Week 3
PDF
Java Repetiotion Statements
PPTX
Loops c++
PDF
1660213363910.pdf
PPTX
22H51A6755.pptx
PPTX
Cs1123 6 loops
DOCX
loops and iteration.docx
PDF
whileloop-161225171903.pdf
PPT
While loop
PPTX
C language (Part 2)
While , For , Do-While Loop
Going loopy - Introduction to Loops.pptx
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
Loop and while Loop
Looping statements in Java
While loop
DSA 103 Object Oriented Programming :: Week 3
Java Repetiotion Statements
Loops c++
1660213363910.pdf
22H51A6755.pptx
Cs1123 6 loops
loops and iteration.docx
whileloop-161225171903.pdf
While loop
C language (Part 2)
Ad

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
01-Introduction-to-Information-Management.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharma ospi slides which help in ospi learning
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Classroom Observation Tools for Teachers
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Supply Chain Operations Speaking Notes -ICLT Program
Chinmaya Tiranga quiz Grand Finale.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
01-Introduction-to-Information-Management.pdf
VCE English Exam - Section C Student Revision Booklet
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Final Presentation General Medicine 03-08-2024.pptx
Anesthesia in Laparoscopic Surgery in India
Pharma ospi slides which help in ospi learning
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Classroom Observation Tools for Teachers
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
human mycosis Human fungal infections are called human mycosis..pptx
RMMM.pdf make it easy to upload and study
STATICS OF THE RIGID BODIES Hibbelers.pdf
GDM (1) (1).pptx small presentation for students
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Ad

C Programming Looping Statements For Students

  • 2. 2 Loops – While, Do, For • Repetition Statements – While – Do – For
  • 3. 3 Repetition Statements • Repetition statements allow us to execute a statement or a block of statements multiple times • Often they are referred to as loops • Like conditional statements, they are controlled by boolean expressions • Java has three kinds of repetition statements: while do for • The programmer should choose the right kind of loop statement for the situation
  • 4. 4 The while Statement • A while statement has the following syntax: • If the condition is true, the statement is executed • Then the condition is evaluated again, and if it is still true, the statement is executed again • The statement is executed repeatedly until the condition becomes false while ( condition ) statement;
  • 5. Logic of a while Loop statement true false condition evaluated 5
  • 6. 6 The while Statement • An example of a while statement: • If the condition of a while loop is false initially, the statement is never executed • Therefore, the body of a while loop will execute zero or more times int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; }
  • 7. Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Initialize count animation 7
  • 8. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } (count < 2) is true animation 8
  • 9. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Print Welcome to Java animation 9
  • 10. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Increase count by 1 count is 1 now animation 10
  • 11. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } (count < 2) is still true since count is 1 animation 11
  • 12. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Print Welcome to Java animation 12
  • 13. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Increase count by 1 count is 2 now animation 13
  • 14. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } (count < 2) is false since count is 2 now animation 14
  • 15. Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } The loop exits. Execute the next statement after the loop. animation 15
  • 17. 17 Infinite Loops • Executing the statements in the body of a while loop must eventually make the condition false • If not, it is called an infinite loop, which will execute until the user interrupts the program • This is a common logical error • You should always double check the logic of a program to ensure that your loops will terminate
  • 18. 18 Infinite Loops • An example of an infinite loop: • This loop will continue executing until the user externally interrupts the program. int count = 1; while (count <= 25) { System.out.println(count); count = count - 1; }
  • 19. 19 Nested Loops • Similar to nested if statements, loops can be nested as well • That is, the body of a loop can contain another loop • For each iteration of the outer loop, the inner loop iterates completely
  • 20. 20 Nested Loops • How many times will the string "Here" be printed? count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 <= 20) { System.out.println ("Here"); count2++; } count1++; } 10 * 20 = 200