SlideShare a Scribd company logo
Introduction to Java Programming
Lecture 11: Repetition Statements (Loops)
Ahmad Jawid Jami
Outline
The while Statement
The do while
Statement The for
Statement
Repetition Statements
• Repetition statements allow us to execute a statement 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:
• the while loop
• the do loop
• the for loop
• The programmer should choose the right kind of loop for the situation
The while Statement
• A while statement has the following syntax:
while ( condition )
statement;
• 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
Logic of a while Loop
statement
true false
condition
evaluated
The while Statement
• An example of a while statement:
int count = 1;
while (count <= 5)
{
System.out.prin
tln (count);
count++;
}
• 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
The while Repetition Structure
• Flowchart of while
loop
product <= 1000 product = 2 * product
true
false
int product = 2
int product = 2;
while ( product <= 1000 )
product = 2 * product;
Parts of a while loop
int x = 1;
while (x < 10) {
System.out.println(x);
x++;
}
int product = 2;
while ( product <= 1000 )
product = 2 * product;
Another loop example
int x = 1;
int y = 2;
while (x < 10)
{
System.out.println(x + “ “ + y);
y *= 2;
x++;
}
Quiz
• Sum the numbers from 1 to 100
• Find the average of a series of numbers
Infinite Loops
• The body of a while loop eventually must 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 normally
Infinite Loops
• An example of an infinite loop:
int count = 1;
while (count <= 25)
{
System.out.print
ln (count);
count = count -
1;
}
• This loop will continue executing until interrupted
(Control-C) or until an underflow error occurs
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
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++;
}
Outline
The while Statement
The do while
Statement The for
Statement
The do while Statement
• A do statement has the following syntax:
do
{
statement;
}
while ( condition )
• The statement is executed once initially, and
then the condition is evaluated
• The statement is executed repeatedly until the
condition becomes false
Logic of a do while Loop
statement
true
condition
evaluated
false
The do while Statement
• An example of a do while loop:
int count = 0;
do
{
count++;
System.out.
println
(count);
} while (count
< 5);
• The body of a do while loop executes at
least once
Comparing while and do while
The while Loop
The do while Loop
statement
true false
condition
evaluated
true
condition
evaluated
statement
false
Outline
The while Statement
The do while
Statement The for
Statement
The for Statement
• A for statement has the following syntax:
for ( initialization ; condition ; increment )
statement;
The increment portion is
executed at the end of each
iteration
The initialization
is executed once
before the loop begins
The statement is
executed until the
condition becomes
false
Logic of a for loop
statement
true
condition
evaluated
false
increment
initialization
The for Statement
• A for loop is functionally equivalent to the following while
loop
structure: initialization;
while ( condition )
{
statement;
increment;
}
The for Statement
• An example of a for loop:
for (int count=1; count <= 5; count++)
System.out.println (count);
• The initialization section can be used to
declare a
variable
• Like a while loop, the condition of a for loop
is tested prior to executing the loop body
• Therefore, the body of a for loop will execute
zero or more times
The for Statement
• The increment section can perform any calculation
for (int num=100; num > 0; num -= 5)
System.out.println (num);
• A for loop is well suited for executing
statements a specific number of times that can be
calculated or determined in advance
Print Stars
//
// Prints a triangle shape using asterisk
(star)
// characters.
//
public static void main (String[] args)
{
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++)
{
for (int star = 1; star <= row; star+
+) System.out.print ("*");
System.out.println();
}
}
The for Statement
• Each expression in the header of a for loop is optional
• If the initialization is left out, no initialization is performed
• If the condition is left out, it is always considered to be true, and
therefore creates an infinite loop
• If the increment is left out, no increment operation is performed
for loop Exercises
• How many times is the loop body repeated?
• for (int x = 3; x <= 15; x += 3)
System.out.println(x);
• for (int x = 1; x <= 5; x += 7)
System.out.println(x);
• for (int x = 12; x >= 2; x -= 3)
System.out.println(x);
• Write the for statement that print the following sequences
of values.
• 1, 2, 3, 4, 5, 6, 7
• 3, 8, 13, 18, 23
• 20, 14, 8, 2, -4, -10
• 19, 27, 35, 43, 51
Nested loops. What do these print?
• for (int i = 1; i < 4; i++) for
(int j = 1; j < i; j++)
for (int j = 1; j < i; j++)
for (int j = 1; j < i; j++)
System.out.println(i + “ “
+ j);
System.out.println(“******”)
;
• int T = 0;
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 2*i; j += 2)
T += j * i;
System.out.println(“T = “ + T);
}
System.out.println(i + “ “ + j);
• for (int i = 0; i < 4; i++)
System.out.println(i + “ “ + j);
• for (int i = 1; i < 4; i++)
Questions?
Ad

More Related Content

Similar to Introduction to Java Programming - Lecture 11.pptx (20)

object orineted programming looping .ppt
object orineted programming looping .pptobject orineted programming looping .ppt
object orineted programming looping .ppt
zeenatparveen24
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
Dr. SURBHI SAROHA
 
Ch4_part1.ppt
Ch4_part1.pptCh4_part1.ppt
Ch4_part1.ppt
RuthikReddyGarlapati
 
Ch4_part1.ppt
Ch4_part1.pptCh4_part1.ppt
Ch4_part1.ppt
ssuserc70988
 
C Programming Looping Statements For Students
C Programming Looping Statements For StudentsC Programming Looping Statements For Students
C Programming Looping Statements For Students
SoumyaKantiSarkar2
 
3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf
santosh147365
 
Lesson 5
Lesson 5Lesson 5
Lesson 5
Fernando Loizides
 
Loops
LoopsLoops
Loops
SAMYAKKHADSE
 
Week04
Week04Week04
Week04
hccit
 
M C6java6
M C6java6M C6java6
M C6java6
mbruggen
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
eShikshak
 
Loops in matlab
Loops in matlabLoops in matlab
Loops in matlab
TUOS-Sam
 
Conditional Loops
Conditional LoopsConditional Loops
Conditional Loops
primeteacher32
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
Amy Nightingale
 
Iteration
IterationIteration
Iteration
Liam Dunphy
 
Control structures.ppt
Control structures.pptControl structures.ppt
Control structures.ppt
Rahul Borate
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
Badrul Alam
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
Tanzeel Ahmad
 
Chapter 12 Computer Science ( ICS 12).pdf
Chapter 12 Computer Science ( ICS 12).pdfChapter 12 Computer Science ( ICS 12).pdf
Chapter 12 Computer Science ( ICS 12).pdf
AamirShahzad527024
 
object orineted programming looping .ppt
object orineted programming looping .pptobject orineted programming looping .ppt
object orineted programming looping .ppt
zeenatparveen24
 
C Programming Looping Statements For Students
C Programming Looping Statements For StudentsC Programming Looping Statements For Students
C Programming Looping Statements For Students
SoumyaKantiSarkar2
 
3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf
santosh147365
 
Week04
Week04Week04
Week04
hccit
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
eShikshak
 
Loops in matlab
Loops in matlabLoops in matlab
Loops in matlab
TUOS-Sam
 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
Amy Nightingale
 
Control structures.ppt
Control structures.pptControl structures.ppt
Control structures.ppt
Rahul Borate
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
Badrul Alam
 
Chapter 12 Computer Science ( ICS 12).pdf
Chapter 12 Computer Science ( ICS 12).pdfChapter 12 Computer Science ( ICS 12).pdf
Chapter 12 Computer Science ( ICS 12).pdf
AamirShahzad527024
 

Recently uploaded (20)

How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...
DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...
DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...
PoojaSen20
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
Rebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter worldRebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter world
Ned Potter
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Look Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History EverywhereLook Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History Everywhere
History of Stoke Newington
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
How to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo SlidesHow to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo Slides
Celine George
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFAMCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
libbys peer assesment.docx..............
libbys peer assesment.docx..............libbys peer assesment.docx..............
libbys peer assesment.docx..............
19lburrell
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...
DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...
DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...
PoojaSen20
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
Rebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter worldRebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter world
Ned Potter
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Look Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History EverywhereLook Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History Everywhere
History of Stoke Newington
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
How to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo SlidesHow to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo Slides
Celine George
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFAMCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
libbys peer assesment.docx..............
libbys peer assesment.docx..............libbys peer assesment.docx..............
libbys peer assesment.docx..............
19lburrell
 
Ad

Introduction to Java Programming - Lecture 11.pptx

  • 1. Introduction to Java Programming Lecture 11: Repetition Statements (Loops) Ahmad Jawid Jami
  • 2. Outline The while Statement The do while Statement The for Statement
  • 3. Repetition Statements • Repetition statements allow us to execute a statement 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: • the while loop • the do loop • the for loop • The programmer should choose the right kind of loop for the situation
  • 4. The while Statement • A while statement has the following syntax: while ( condition ) statement; • 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
  • 5. Logic of a while Loop statement true false condition evaluated
  • 6. The while Statement • An example of a while statement: int count = 1; while (count <= 5) { System.out.prin tln (count); count++; } • 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
  • 7. The while Repetition Structure • Flowchart of while loop product <= 1000 product = 2 * product true false int product = 2 int product = 2; while ( product <= 1000 ) product = 2 * product;
  • 8. Parts of a while loop int x = 1; while (x < 10) { System.out.println(x); x++; } int product = 2; while ( product <= 1000 ) product = 2 * product;
  • 9. Another loop example int x = 1; int y = 2; while (x < 10) { System.out.println(x + “ “ + y); y *= 2; x++; }
  • 10. Quiz • Sum the numbers from 1 to 100 • Find the average of a series of numbers
  • 11. Infinite Loops • The body of a while loop eventually must 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 normally
  • 12. Infinite Loops • An example of an infinite loop: int count = 1; while (count <= 25) { System.out.print ln (count); count = count - 1; } • This loop will continue executing until interrupted (Control-C) or until an underflow error occurs
  • 13. 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
  • 14. 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++; }
  • 15. Outline The while Statement The do while Statement The for Statement
  • 16. The do while Statement • A do statement has the following syntax: do { statement; } while ( condition ) • The statement is executed once initially, and then the condition is evaluated • The statement is executed repeatedly until the condition becomes false
  • 17. Logic of a do while Loop statement true condition evaluated false
  • 18. The do while Statement • An example of a do while loop: int count = 0; do { count++; System.out. println (count); } while (count < 5); • The body of a do while loop executes at least once
  • 19. Comparing while and do while The while Loop The do while Loop statement true false condition evaluated true condition evaluated statement false
  • 20. Outline The while Statement The do while Statement The for Statement
  • 21. The for Statement • A for statement has the following syntax: for ( initialization ; condition ; increment ) statement; The increment portion is executed at the end of each iteration The initialization is executed once before the loop begins The statement is executed until the condition becomes false
  • 22. Logic of a for loop statement true condition evaluated false increment initialization
  • 23. The for Statement • A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }
  • 24. The for Statement • An example of a for loop: for (int count=1; count <= 5; count++) System.out.println (count); • The initialization section can be used to declare a variable • Like a while loop, the condition of a for loop is tested prior to executing the loop body • Therefore, the body of a for loop will execute zero or more times
  • 25. The for Statement • The increment section can perform any calculation for (int num=100; num > 0; num -= 5) System.out.println (num); • A for loop is well suited for executing statements a specific number of times that can be calculated or determined in advance
  • 26. Print Stars // // Prints a triangle shape using asterisk (star) // characters. // public static void main (String[] args) { final int MAX_ROWS = 10; for (int row = 1; row <= MAX_ROWS; row++) { for (int star = 1; star <= row; star+ +) System.out.print ("*"); System.out.println(); } }
  • 27. The for Statement • Each expression in the header of a for loop is optional • If the initialization is left out, no initialization is performed • If the condition is left out, it is always considered to be true, and therefore creates an infinite loop • If the increment is left out, no increment operation is performed
  • 28. for loop Exercises • How many times is the loop body repeated? • for (int x = 3; x <= 15; x += 3) System.out.println(x); • for (int x = 1; x <= 5; x += 7) System.out.println(x); • for (int x = 12; x >= 2; x -= 3) System.out.println(x); • Write the for statement that print the following sequences of values. • 1, 2, 3, 4, 5, 6, 7 • 3, 8, 13, 18, 23 • 20, 14, 8, 2, -4, -10 • 19, 27, 35, 43, 51
  • 29. Nested loops. What do these print? • for (int i = 1; i < 4; i++) for (int j = 1; j < i; j++) for (int j = 1; j < i; j++) for (int j = 1; j < i; j++) System.out.println(i + “ “ + j); System.out.println(“******”) ; • int T = 0; for (int i = 1; i < 4; i++) { for (int j = 1; j < 2*i; j += 2) T += j * i; System.out.println(“T = “ + T); } System.out.println(i + “ “ + j); • for (int i = 0; i < 4; i++) System.out.println(i + “ “ + j); • for (int i = 1; i < 4; i++)
  翻译: