SlideShare a Scribd company logo
1 of 22
Programming
Loop
Repetition Statements
• Repetition statements allow us to execute a cstatement
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

 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
The while Statement

Logic of a while Loop
condition
evaluated
statement
true false

 An example of a while statement:
int count = 1;
while (count <= 5){
System.out.println (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 Statement

 Let's look at some examples of loop processing
 A loop can be used to maintain a running sum
 A sentinel value is a special input value that
represents the end of input
 A loop can also be used for input validation, making a
program more robust
The while Statement

 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 (semantic) 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.println (count);
count = count - 1;
}
 This loop will continue executing until interrupted (Control-C)
or until an underflow error occurs
Infinite 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
 Your second course project involves a while loop
nested inside of a for loop
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
Nested Loops

 A do-while statement (also called a do loop) 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
The do-while Statement

Logic of a do-while
Loop
statement
condition
evaluated
true
false

 An example of a do loop:
 int count = 0;
do{
count++;
System.out.println (count);
} while (count < 5);
 The body of a do loop executes at least once
The do Statement

Comparing while and
do
statement
true false
condition
evaluated
The while Loop
true
condition
evaluated
statement
false
The do Loop

 for ( initialization ; condition ;
increment ){
statement;
}
The for Statement
The statement is
executed until the
condition becomes false
The increment portion is
executed at the end of each
iteration
The initialization
is executed once
before the loop begins

Logic of a for loop
condition
evaluated
initialization
statement
true
increment
false

 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
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
 We usually call this a “forever loop”
 If the increment is left out, no increment operation is
performed
The for Statement

 Remember the break keyword that we used to stop
a switch statement from executing more than one
statement?
 break can also be used to exit an infinite loop
 But it is almost always best to use a well-written
while loop
break revisited
THANKS TO ALL

More Related Content

What's hot

C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
 
If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++Bishal Sharma
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While LoopAbhishek Choksi
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Looping statement in vb.net
Looping statement in vb.netLooping statement in vb.net
Looping statement in vb.netilakkiya
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java Ravi_Kant_Sahu
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for DesignersR. Sosa
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception HandlingMegha V
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentationtigerwarn
 

What's hot (20)

Java
JavaJava
Java
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++
 
Forloop
ForloopForloop
Forloop
 
Java-java virtual machine
Java-java virtual machineJava-java virtual machine
Java-java virtual machine
 
Exception handling
Exception handlingException handling
Exception handling
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
 
Looping statement in vb.net
Looping statement in vb.netLooping statement in vb.net
Looping statement in vb.net
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
 
Type conversion
Type conversionType conversion
Type conversion
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
C# Loops
C# LoopsC# Loops
C# Loops
 
user defined function
user defined functionuser defined function
user defined function
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
JVM
JVMJVM
JVM
 
Java interface
Java interfaceJava interface
Java interface
 

Viewers also liked

Introduction to basic programming repetition
Introduction to basic programming repetitionIntroduction to basic programming repetition
Introduction to basic programming repetitionJordan Delacruz
 
Computer programming:Know How to Flowchart
Computer  programming:Know How to FlowchartComputer  programming:Know How to Flowchart
Computer programming:Know How to FlowchartAngelo Tomboc
 
1.4 core programming [understand error handling]
1.4 core programming [understand error handling]1.4 core programming [understand error handling]
1.4 core programming [understand error handling]tototo147
 
1.3 core programming [identify the appropriate method for handling repetition]
1.3 core programming [identify the appropriate method for handling repetition]1.3 core programming [identify the appropriate method for handling repetition]
1.3 core programming [identify the appropriate method for handling repetition]tototo147
 
1.2 core programming [understand computer decision structures]
1.2 core programming [understand computer decision structures]1.2 core programming [understand computer decision structures]
1.2 core programming [understand computer decision structures]tototo147
 
(Aug.31) introduction to macflash
(Aug.31) introduction to macflash(Aug.31) introduction to macflash
(Aug.31) introduction to macflashJordan Delacruz
 
Software Development Fundamentals
Software Development FundamentalsSoftware Development Fundamentals
Software Development FundamentalsChris Farrell
 
1.1 core programming [understand computer storage and data types]
1.1 core programming [understand computer storage and data types]1.1 core programming [understand computer storage and data types]
1.1 core programming [understand computer storage and data types]tototo147
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and FlowchartsDeva Singh
 

Viewers also liked (11)

Introduction to basic programming repetition
Introduction to basic programming repetitionIntroduction to basic programming repetition
Introduction to basic programming repetition
 
Computer programming:Know How to Flowchart
Computer  programming:Know How to FlowchartComputer  programming:Know How to Flowchart
Computer programming:Know How to Flowchart
 
1.4 core programming [understand error handling]
1.4 core programming [understand error handling]1.4 core programming [understand error handling]
1.4 core programming [understand error handling]
 
CPP03 - Repetition
CPP03 - RepetitionCPP03 - Repetition
CPP03 - Repetition
 
1.3 core programming [identify the appropriate method for handling repetition]
1.3 core programming [identify the appropriate method for handling repetition]1.3 core programming [identify the appropriate method for handling repetition]
1.3 core programming [identify the appropriate method for handling repetition]
 
Software Development Fundamentals 1
Software Development Fundamentals 1Software Development Fundamentals 1
Software Development Fundamentals 1
 
1.2 core programming [understand computer decision structures]
1.2 core programming [understand computer decision structures]1.2 core programming [understand computer decision structures]
1.2 core programming [understand computer decision structures]
 
(Aug.31) introduction to macflash
(Aug.31) introduction to macflash(Aug.31) introduction to macflash
(Aug.31) introduction to macflash
 
Software Development Fundamentals
Software Development FundamentalsSoftware Development Fundamentals
Software Development Fundamentals
 
1.1 core programming [understand computer storage and data types]
1.1 core programming [understand computer storage and data types]1.1 core programming [understand computer storage and data types]
1.1 core programming [understand computer storage and data types]
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and Flowcharts
 

Similar to Programming loop

Similar to Programming loop (20)

Looping statements
Looping statementsLooping statements
Looping statements
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 
Looping statements
Looping statementsLooping statements
Looping statements
 
15-Loops.ppt
15-Loops.ppt15-Loops.ppt
15-Loops.ppt
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Loops c++
Loops c++Loops c++
Loops c++
 
While loop
While loopWhile loop
While loop
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
Conditional Loops
Conditional LoopsConditional Loops
Conditional Loops
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Control structure
Control structureControl structure
Control structure
 
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
 
Ch4_part1.ppt
Ch4_part1.pptCh4_part1.ppt
Ch4_part1.ppt
 
Ch4_part1.ppt
Ch4_part1.pptCh4_part1.ppt
Ch4_part1.ppt
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
 
M C6java6
M C6java6M C6java6
M C6java6
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
 

More from University of Potsdam (20)

Computer fundamentals 01
Computer fundamentals 01Computer fundamentals 01
Computer fundamentals 01
 
Workshop on android apps development
Workshop on android apps developmentWorkshop on android apps development
Workshop on android apps development
 
Transparency and concurrency
Transparency and concurrencyTransparency and concurrency
Transparency and concurrency
 
Database System Architecture
Database System ArchitectureDatabase System Architecture
Database System Architecture
 
Functional dependency and normalization
Functional dependency and normalizationFunctional dependency and normalization
Functional dependency and normalization
 
indexing and hashing
indexing and hashingindexing and hashing
indexing and hashing
 
data recovery-raid
data recovery-raiddata recovery-raid
data recovery-raid
 
Query processing
Query processingQuery processing
Query processing
 
Machine Learning for Data Mining
Machine Learning for Data MiningMachine Learning for Data Mining
Machine Learning for Data Mining
 
Tree, function and graph
Tree, function and graphTree, function and graph
Tree, function and graph
 
Sonet
SonetSonet
Sonet
 
Sets in discrete mathematics
Sets in discrete mathematicsSets in discrete mathematics
Sets in discrete mathematics
 
Set in discrete mathematics
Set in discrete mathematicsSet in discrete mathematics
Set in discrete mathematics
 
Series parallel ac rlc networks
Series parallel ac rlc networksSeries parallel ac rlc networks
Series parallel ac rlc networks
 
Series parallel ac networks
Series parallel ac networksSeries parallel ac networks
Series parallel ac networks
 
Relations
RelationsRelations
Relations
 
Relations
RelationsRelations
Relations
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Prim algorithm
Prim algorithmPrim algorithm
Prim algorithm
 

Recently uploaded

Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 

Recently uploaded (20)

Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 

Programming loop

  • 2. Repetition Statements • Repetition statements allow us to execute a cstatement 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
  • 3.   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 The while Statement
  • 4.  Logic of a while Loop condition evaluated statement true false
  • 5.   An example of a while statement: int count = 1; while (count <= 5){ System.out.println (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 Statement
  • 6.   Let's look at some examples of loop processing  A loop can be used to maintain a running sum  A sentinel value is a special input value that represents the end of input  A loop can also be used for input validation, making a program more robust The while Statement
  • 7.   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 (semantic) error  You should always double check the logic of a program to ensure that your loops will terminate normally Infinite Loops
  • 8.   An example of an infinite loop: int count = 1; while (count <= 25){ System.out.println (count); count = count - 1; }  This loop will continue executing until interrupted (Control-C) or until an underflow error occurs Infinite Loops
  • 9.   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  Your second course project involves a while loop nested inside of a for loop Nested Loops
  • 10.  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 Nested Loops
  • 11.   A do-while statement (also called a do loop) 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 The do-while Statement
  • 12.  Logic of a do-while Loop statement condition evaluated true false
  • 13.   An example of a do loop:  int count = 0; do{ count++; System.out.println (count); } while (count < 5);  The body of a do loop executes at least once The do Statement
  • 14.  Comparing while and do statement true false condition evaluated The while Loop true condition evaluated statement false The do Loop
  • 15.   for ( initialization ; condition ; increment ){ statement; } The for Statement The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration The initialization is executed once before the loop begins
  • 16.  Logic of a for loop condition evaluated initialization statement true increment false
  • 17.   A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ){ statement; increment; } The for Statement
  • 18.   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
  • 19.   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 The for Statement
  • 20.   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  We usually call this a “forever loop”  If the increment is left out, no increment operation is performed The for Statement
  • 21.   Remember the break keyword that we used to stop a switch statement from executing more than one statement?  break can also be used to exit an infinite loop  But it is almost always best to use a well-written while loop break revisited