SlideShare a Scribd company logo
1 of 22
Computer
Programming 2
Lesson 8– Java – Loop Control
Prepared by: Analyn G. Regaton
LoopControl
A loop statement
allows us to
execute a
statement or
group of
statements
multiple times.
Types of Loop
Sr.No. Loop & Description
1 while loop
Repeats a statement or group of statements while a given
condition is true. It tests the condition before executing the
loop body.
2 for loop
Execute a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
3 do...while loop
Like a while statement, except that it tests the condition at
the end of the loop body.
WHILE LOOP
A while loop statement in Java programming language
repeatedly executes a target statement as long as a given
condition is true.
Syntax
The syntax of a while loop is −
while(Boolean_expression) {
// Statements
}
Here, statement(s) may be a single statement or a block of
statements. The condition may be any expression, and true is
any non zero value.
Boolean_expression = true inside the
loop will be executed
Boolean_expression = false program
control passes to the line immediately to the loop
FLOW
DIAGRAM
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.
PROGRAM
EXAMPLE
FOR LOOP
The syntax of a for loop is −
for(initialization; Boolean_expression;update) {
// Statements
}
A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to be executed a specific
number of times.
A for loop is useful when you know how many times a task is
to be repeated.
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 and this step ends with a semi colon (;).
 Next, the Boolean expression is evaluated. If it is true, the
body of the loop is executed. If it is false, the body of the loop
will not be executed and control jumps to the next statement
past the for loop.
 After the body of the for loop gets executed, the control
jumps back up to the update statement.This statement
allows you to update any loop control variables.This
statement can be left blank with a semicolon at the end.
 The Boolean expression is now evaluated again. If it is true,
the loop executes and the process repeats (body of loop, then
update step, then Boolean expression). After the Boolean
expression is false, the for loop terminates.
Flow Diagram
PROGRAM
EXAMPLE
DO-WHILE
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
Following is the syntax of a do...while loop −
do {
// Statements
}
while(Boolean_expression);
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 control jumps back up to
do statement, and the statements in the loop execute again. This
process repeats until the Boolean expression is false.
FLOW
DIAGRAM
PROGRAM
EXAMPLE
LOOP
CONTROL
STATEMENT
Sr.No. Control Statement & Description
1 break statement
Terminates the loop or switch statement and transfers
execution to the statement immediately following the
loop or switch.
2 continue statement
Causes the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.
Loop control statements change execution from its
normal sequence.When execution leaves a scope, all
automatic objects that were created in that scope are
destroyed.
BREAK
STATEMENT
The break statement in Java programming language
has the following two usages −
•When the break statement is encountered inside a
loop, the loop is immediately terminated and the program
control resumes at the next statement following the
• loop.
•It can be used to terminate a case in the switch
statement (covered in the next chapter).
Syntax
The syntax of a break is a single statement inside any loop −
break;
FLOW
DIAGRAM
PROGRAM
EXAMPLE
Output
10
20
CONTINUE
STATEMENT
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
control to immediately jump to the update
statement.
•In a while loop or do/while loop, control
immediately jumps to the Boolean expression.
Syntax
The syntax of a continue is a single statement inside
any loop −
continue;
FLOW
DIAGRAM
PROGRAM
EXAMPLE
Output
10
20
40
50
ENHANCED
for Loop in
Java
As of Java 5, the enhanced for loop was introduced. This is
mainly used to traverse collection of elements including arrays.
Syntax
Following is the syntax of enhanced for loop −
for(declaration : expression) {
// Statements
}
•Declaration − The newly declared block variable, is of a type
compatible with the elements of the array you are accessing.
The variable will be available within the for block and its value
would be the same as the current array element.
•Expression − This evaluates to the array you need to loop
through. The expression can be an array variable or method call
that returns an array.
EXAMPLE
PROGRAM
Output
10, 20, 30, 40, 50,
James, Larry, Tom, Lacy,

More Related Content

What's hot

Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)Calvin Cheng
 
Project presentation
Project presentationProject presentation
Project presentationTrung Dang
 
Java 8 – completion stage
Java 8 – completion stageJava 8 – completion stage
Java 8 – completion stageFahad Cv
 
Functional Groovy - Confess
Functional Groovy - ConfessFunctional Groovy - Confess
Functional Groovy - ConfessAndres Almiray
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C languageErumShammim
 
FPGA Coding Guidelines
FPGA Coding GuidelinesFPGA Coding Guidelines
FPGA Coding GuidelinesChethan Kumar
 
10control statement in c#
10control statement in c#10control statement in c#
10control statement in c#Sireesh K
 
What is to loop in c++
What is to loop in c++What is to loop in c++
What is to loop in c++03446940736
 
Erlang workshopdrammen
Erlang workshopdrammenErlang workshopdrammen
Erlang workshopdrammenReidar Sollid
 
Report for lab 7(2)
Report for lab 7(2)Report for lab 7(2)
Report for lab 7(2)trayyoo
 
Introduction of basics loop and array
Introduction of basics loop and arrayIntroduction of basics loop and array
Introduction of basics loop and arrayprojectsinfo
 
Introduction to Monix Coeval
Introduction to Monix CoevalIntroduction to Monix Coeval
Introduction to Monix CoevalKnoldus Inc.
 

What's hot (16)

Iteration
IterationIteration
Iteration
 
Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)
 
Project presentation
Project presentationProject presentation
Project presentation
 
Java 8 – completion stage
Java 8 – completion stageJava 8 – completion stage
Java 8 – completion stage
 
Functional Groovy - Confess
Functional Groovy - ConfessFunctional Groovy - Confess
Functional Groovy - Confess
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C language
 
FPGA Coding Guidelines
FPGA Coding GuidelinesFPGA Coding Guidelines
FPGA Coding Guidelines
 
C++ loop
C++ loop C++ loop
C++ loop
 
10control statement in c#
10control statement in c#10control statement in c#
10control statement in c#
 
What is to loop in c++
What is to loop in c++What is to loop in c++
What is to loop in c++
 
Erlang workshopdrammen
Erlang workshopdrammenErlang workshopdrammen
Erlang workshopdrammen
 
Switch statement
Switch statementSwitch statement
Switch statement
 
Report for lab 7(2)
Report for lab 7(2)Report for lab 7(2)
Report for lab 7(2)
 
Introduction of basics loop and array
Introduction of basics loop and arrayIntroduction of basics loop and array
Introduction of basics loop and array
 
Introduction to Monix Coeval
Introduction to Monix CoevalIntroduction to Monix Coeval
Introduction to Monix Coeval
 
Soot for dummies
Soot for dummiesSoot for dummies
Soot for dummies
 

Similar to Learn Java loop control with while, for, do-while, break, continue and enhanced for loops

Similar to Learn Java loop control with while, for, do-while, break, continue and enhanced for loops (20)

Cse lecture-7-c loop
Cse lecture-7-c loopCse lecture-7-c loop
Cse lecture-7-c loop
 
C language 2
C language 2C language 2
C language 2
 
Loops
LoopsLoops
Loops
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdf
 
Loops
LoopsLoops
Loops
 
python.pptx
python.pptxpython.pptx
python.pptx
 
R loops
R   loopsR   loops
R loops
 
Loop structures
Loop structuresLoop structures
Loop structures
 
Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptx
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Loops in C
Loops in CLoops in C
Loops in C
 
2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes
 
Chapter 5 java
Chapter 5 javaChapter 5 java
Chapter 5 java
 
Loops in c
Loops in cLoops in c
Loops in c
 
22H51A6755.pptx
22H51A6755.pptx22H51A6755.pptx
22H51A6755.pptx
 
Control statements
Control statementsControl statements
Control statements
 
Java 2.pptx
Java 2.pptxJava 2.pptx
Java 2.pptx
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 

More from MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Recently uploaded

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
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
 
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
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
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
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
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
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 

Recently uploaded (20)

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
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...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
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
 
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
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
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
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
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
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
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
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 

Learn Java loop control with while, for, do-while, break, continue and enhanced for loops

  • 1. Computer Programming 2 Lesson 8– Java – Loop Control Prepared by: Analyn G. Regaton
  • 2. LoopControl A loop statement allows us to execute a statement or group of statements multiple times.
  • 3. Types of Loop Sr.No. Loop & Description 1 while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. 2 for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. 3 do...while loop Like a while statement, except that it tests the condition at the end of the loop body.
  • 4. WHILE LOOP A while loop statement in Java programming language repeatedly executes a target statement as long as a given condition is true. Syntax The syntax of a while loop is − while(Boolean_expression) { // Statements } Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non zero value. Boolean_expression = true inside the loop will be executed Boolean_expression = false program control passes to the line immediately to the loop
  • 5. FLOW DIAGRAM 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.
  • 7. FOR LOOP The syntax of a for loop is − for(initialization; Boolean_expression;update) { // Statements } A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times. A for loop is useful when you know how many times a task is to be repeated.
  • 8. 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 and this step ends with a semi colon (;).  Next, the Boolean expression is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop will not be executed and control jumps to the next statement past the for loop.  After the body of the for loop gets executed, the control jumps back up to the update statement.This statement allows you to update any loop control variables.This statement can be left blank with a semicolon at the end.  The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats (body of loop, then update step, then Boolean expression). After the Boolean expression is false, the for loop terminates.
  • 11. DO-WHILE 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 Following is the syntax of a do...while loop − do { // Statements } while(Boolean_expression); 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 control jumps back up to do statement, and the statements in the loop execute again. This process repeats until the Boolean expression is false.
  • 14. LOOP CONTROL STATEMENT Sr.No. Control Statement & Description 1 break statement Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. 2 continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. Loop control statements change execution from its normal sequence.When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
  • 15. BREAK STATEMENT The break statement in Java programming language has the following two usages − •When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the • loop. •It can be used to terminate a case in the switch statement (covered in the next chapter). Syntax The syntax of a break is a single statement inside any loop − break;
  • 18. CONTINUE STATEMENT 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 control to immediately jump to the update statement. •In a while loop or do/while loop, control immediately jumps to the Boolean expression. Syntax The syntax of a continue is a single statement inside any loop − continue;
  • 21. ENHANCED for Loop in Java As of Java 5, the enhanced for loop was introduced. This is mainly used to traverse collection of elements including arrays. Syntax Following is the syntax of enhanced for loop − for(declaration : expression) { // Statements } •Declaration − The newly declared block variable, is of a type compatible with the elements of the array you are accessing. The variable will be available within the for block and its value would be the same as the current array element. •Expression − This evaluates to the array you need to loop through. The expression can be an array variable or method call that returns an array.
  • 22. EXAMPLE PROGRAM Output 10, 20, 30, 40, 50, James, Larry, Tom, Lacy,