SlideShare a Scribd company logo
For loop in java
Presented by:
Shubham S Kamate.
MCA 2nd sem .
CONTENTS:
The for loop in java:
• Syntax
• Flow chart
• Example code
The varitions in for loop:
• Multiple loop control vaiable
• missing pieces
• The infinite loop
• Loops with no body
The enhanced for loop
• Syntax
• Example code
Java for Loop
Java for loop is used to run a block of code for a certain number of times. The
syntax of for loop is:
for (initialExpression; testExpression; updateExpression) {
// body of the loop
}
Here,
The initialExpression initializes and/or declares variables and executes only once.
The condition is evaluated. If the condition is true, the body of the for loop is
executed.
The updateExpression updates the value of initialExpression.
The condition is evaluated again. The process continues until the condition is
false.
To learn more about the conditions, visit Java relational and logical operators.
Example 1: Display a Text Five Times
// Program to print a text 5 times
class Main {
public static void main(String[] args) {
int n = 5;
// for loop
for (int i = 1; i <= n; ++i) {
System.out.println("Java is fun");
}
}
}
Output:
Java is fun
Java is fun
Java is fun
Java is fun
Java is fun
SOME VARIATIONS ON THE LOOP
// Infinite for Loop
class Infinite {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; --i) {
System.out.println("Hello");
}
}
}
Run Code
Here, the test expression ,i <= 10, is never false and Hello is printed repeatedly
until the memory runs out.
Out put
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Multiple loop control variable
The for is one of the most versatile statements in the Java language because it
allows a wide range of varia tions. One of the most common is the use of multiple
loop control variables. When using multiple loop control variables, the
initialization and iteration expressions for each variable are separated by commas
Here is a simple example:
//Use multiple loop control variable in a for.
Class Multiple loop control variable in a for.
public static void main(string[] args) {
Int i,j;
for(i=0, j=10; i<j ; i++, j--) --------notice the two loop control variables.
system.out.println(“i and j : “ + i + “ “ + j);
}
}
The output from program:
i and j : 0 10
i and j : 1 9
i and j : 2 8
i and j : 3 7
i and j : 4 6
MISSING PIECES
Some interesting for loop variations are created by leaving pieces of the loop
definition empty. In Java, a is possible for any or all of the initialization, condition,
or intration portions of the for loop to be blank.
For example, consider the following program.
//Parts of the for can be empty
class Empty {
public static void main(string[] args) {
Int I;
For(i=0;i<10;) {
System.out.println(“Pass #”+i );
i++; // increment loop control var
}
}
}
Here the iteration expression of the for is empty .Instead, the loop control
variable i is incremented inside the body of the loop This means that each
time the loop repeats, i is tested to see whether it equals 10, but the no no
further action takes place Of course, since i is still incremented within the
body of the loop, the loop runs normally, displaying the following output:
Pass#0
pass #1
Pass #2
Pass #3
Pass #4
Pass #5
Pass #6
Pass #7
Pass #8
Pass #9
Loops with No body:
In Java, the body associated with a for loop (or any other loop)can be empty. This
is because a null statement is syntactically valid. Body-less loops are often useful.
For example, the following program uses one to sum the numbers 1 through 5
//The body of a loop can be empty.
class Empty3 {
public static void main(String[] args) {
int i;
int sum=0;
//sum the numbers through 5
for(i=1; i<=5;sum += i++) ;-----No body in this loop!
System .out .println (“Sum is “ + sum);
}
}
The output from the program is shown here:
Sum in 15
THE ENHANCED FOR LOOP
In Java, the for-each loop is used to iterate through elements of arrays and
collections (like ArrayList). It is also known as the enhanced for loop.
for-each Loop Sytnax
The syntax of the Java for-each loop is:
for(dataType item : array) {
...
}
Here,
array - an array or a collection
item - each item of array/collection is assigned to this variable
dataType - the data type of the array/collection
Example 1: Print Array Elements
// print array elements
class Main {
public static void main(String[] args) {
// create an array
int[] numbers = {3, 9, 5, -5};
// for each loop
for (int number: numbers) {
System.out.println(number);
}
}
}
Run Code
Output:-
3
9
5
-5
Here, we have used the for-each loop to print each element of the numbers array
one by one.
In the first iteration, item will be 3.
In the second iteration, item will be 9.
In the third iteration, item will be 5.
In the fourth iteration, item will be -5
THANK YOU

More Related Content

Similar to prt123.pptx

Loops in java script
Loops in java scriptLoops in java script
Loops in java script
Ravi Bhadauria
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
While loop and for loop 06 (js)
While loop and for loop 06 (js)While loop and for loop 06 (js)
While loop and for loop 06 (js)
AbhishekMondal42
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
JavvajiVenkat
 
Java do-while Loop
Java do-while LoopJava do-while Loop
Java do-while Loop
Rhythm Suiwal
 
Java learning
Java learningJava learning
Java learning
moew3
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
Prasanna Kumar SM
 
LOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptxLOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptx
AFANJIPHILL
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
manish kumar
 
Control statements
Control statementsControl statements
Control statements
CutyChhaya
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Krishna Raj
 
javaloop understanding what is java.pptx
javaloop understanding what is java.pptxjavaloop understanding what is java.pptx
javaloop understanding what is java.pptx
RobertCarreonBula
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
Abhijeet Dubey
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
Roman Okolovich
 
JAVA LOOP.pptx
JAVA LOOP.pptxJAVA LOOP.pptx
JAVA LOOP.pptx
SofiaArquero2
 
Core java
Core javaCore java
Core java
Mallikarjuna G D
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
Rj Baculo
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 

Similar to prt123.pptx (20)

Loops in java script
Loops in java scriptLoops in java script
Loops in java script
 
Loops c++
Loops c++Loops c++
Loops c++
 
While loop and for loop 06 (js)
While loop and for loop 06 (js)While loop and for loop 06 (js)
While loop and for loop 06 (js)
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Java do-while Loop
Java do-while LoopJava do-while Loop
Java do-while Loop
 
Java learning
Java learningJava learning
Java learning
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
 
LOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptxLOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptx
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
 
Control statements
Control statementsControl statements
Control statements
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
javaloop understanding what is java.pptx
javaloop understanding what is java.pptxjavaloop understanding what is java.pptx
javaloop understanding what is java.pptx
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
 
JAVA LOOP.pptx
JAVA LOOP.pptxJAVA LOOP.pptx
JAVA LOOP.pptx
 
Core java
Core javaCore java
Core java
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 

Recently uploaded

Creative Web Design Company in Singapore
Creative Web Design Company in SingaporeCreative Web Design Company in Singapore
Creative Web Design Company in Singapore
techboxsqauremedia
 
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
AnnySerafinaLove
 
Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...
Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...
Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...
my Pandit
 
Easily Verify Compliance and Security with Binance KYC
Easily Verify Compliance and Security with Binance KYCEasily Verify Compliance and Security with Binance KYC
Easily Verify Compliance and Security with Binance KYC
Any kyc Account
 
The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...
The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...
The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...
Stephen Cashman
 
Innovation Management Frameworks: Your Guide to Creativity & Innovation
Innovation Management Frameworks: Your Guide to Creativity & InnovationInnovation Management Frameworks: Your Guide to Creativity & Innovation
Innovation Management Frameworks: Your Guide to Creativity & Innovation
Operational Excellence Consulting
 
Building Your Employer Brand with Social Media
Building Your Employer Brand with Social MediaBuilding Your Employer Brand with Social Media
Building Your Employer Brand with Social Media
LuanWise
 
Observation Lab PowerPoint Assignment for TEM 431
Observation Lab PowerPoint Assignment for TEM 431Observation Lab PowerPoint Assignment for TEM 431
Observation Lab PowerPoint Assignment for TEM 431
ecamare2
 
Structural Design Process: Step-by-Step Guide for Buildings
Structural Design Process: Step-by-Step Guide for BuildingsStructural Design Process: Step-by-Step Guide for Buildings
Structural Design Process: Step-by-Step Guide for Buildings
Chandresh Chudasama
 
How to Implement a Real Estate CRM Software
How to Implement a Real Estate CRM SoftwareHow to Implement a Real Estate CRM Software
How to Implement a Real Estate CRM Software
SalesTown
 
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Kalyan Satta Matka Guessing Matka Result Main Bazar chart
 
3 Simple Steps To Buy Verified Payoneer Account In 2024
3 Simple Steps To Buy Verified Payoneer Account In 20243 Simple Steps To Buy Verified Payoneer Account In 2024
3 Simple Steps To Buy Verified Payoneer Account In 2024
SEOSMMEARTH
 
amptalk_RecruitingDeck_english_2024.06.05
amptalk_RecruitingDeck_english_2024.06.05amptalk_RecruitingDeck_english_2024.06.05
amptalk_RecruitingDeck_english_2024.06.05
marketing317746
 
Authentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto RicoAuthentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto Rico
Corey Perlman, Social Media Speaker and Consultant
 
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
taqyea
 
Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...
Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...
Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...
my Pandit
 
Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024
Top Forex Brokers Review
 
-- June 2024 is National Volunteer Month --
-- June 2024 is National Volunteer Month ---- June 2024 is National Volunteer Month --
-- June 2024 is National Volunteer Month --
NZSG
 
Business storytelling: key ingredients to a story
Business storytelling: key ingredients to a storyBusiness storytelling: key ingredients to a story
Business storytelling: key ingredients to a story
Alexandra Fulford
 
The Genesis of BriansClub.cm Famous Dark WEb Platform
The Genesis of BriansClub.cm Famous Dark WEb PlatformThe Genesis of BriansClub.cm Famous Dark WEb Platform
The Genesis of BriansClub.cm Famous Dark WEb Platform
SabaaSudozai
 

Recently uploaded (20)

Creative Web Design Company in Singapore
Creative Web Design Company in SingaporeCreative Web Design Company in Singapore
Creative Web Design Company in Singapore
 
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
 
Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...
Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...
Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...
 
Easily Verify Compliance and Security with Binance KYC
Easily Verify Compliance and Security with Binance KYCEasily Verify Compliance and Security with Binance KYC
Easily Verify Compliance and Security with Binance KYC
 
The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...
The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...
The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...
 
Innovation Management Frameworks: Your Guide to Creativity & Innovation
Innovation Management Frameworks: Your Guide to Creativity & InnovationInnovation Management Frameworks: Your Guide to Creativity & Innovation
Innovation Management Frameworks: Your Guide to Creativity & Innovation
 
Building Your Employer Brand with Social Media
Building Your Employer Brand with Social MediaBuilding Your Employer Brand with Social Media
Building Your Employer Brand with Social Media
 
Observation Lab PowerPoint Assignment for TEM 431
Observation Lab PowerPoint Assignment for TEM 431Observation Lab PowerPoint Assignment for TEM 431
Observation Lab PowerPoint Assignment for TEM 431
 
Structural Design Process: Step-by-Step Guide for Buildings
Structural Design Process: Step-by-Step Guide for BuildingsStructural Design Process: Step-by-Step Guide for Buildings
Structural Design Process: Step-by-Step Guide for Buildings
 
How to Implement a Real Estate CRM Software
How to Implement a Real Estate CRM SoftwareHow to Implement a Real Estate CRM Software
How to Implement a Real Estate CRM Software
 
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka Result Satta Matka Guessing Satta Fix jodi Kalyan Fin...
 
3 Simple Steps To Buy Verified Payoneer Account In 2024
3 Simple Steps To Buy Verified Payoneer Account In 20243 Simple Steps To Buy Verified Payoneer Account In 2024
3 Simple Steps To Buy Verified Payoneer Account In 2024
 
amptalk_RecruitingDeck_english_2024.06.05
amptalk_RecruitingDeck_english_2024.06.05amptalk_RecruitingDeck_english_2024.06.05
amptalk_RecruitingDeck_english_2024.06.05
 
Authentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto RicoAuthentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto Rico
 
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
 
Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...
Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...
Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...
 
Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024
 
-- June 2024 is National Volunteer Month --
-- June 2024 is National Volunteer Month ---- June 2024 is National Volunteer Month --
-- June 2024 is National Volunteer Month --
 
Business storytelling: key ingredients to a story
Business storytelling: key ingredients to a storyBusiness storytelling: key ingredients to a story
Business storytelling: key ingredients to a story
 
The Genesis of BriansClub.cm Famous Dark WEb Platform
The Genesis of BriansClub.cm Famous Dark WEb PlatformThe Genesis of BriansClub.cm Famous Dark WEb Platform
The Genesis of BriansClub.cm Famous Dark WEb Platform
 

prt123.pptx

  • 1. For loop in java Presented by: Shubham S Kamate. MCA 2nd sem .
  • 2. CONTENTS: The for loop in java: • Syntax • Flow chart • Example code The varitions in for loop: • Multiple loop control vaiable • missing pieces • The infinite loop • Loops with no body The enhanced for loop • Syntax • Example code
  • 3. Java for Loop Java for loop is used to run a block of code for a certain number of times. The syntax of for loop is: for (initialExpression; testExpression; updateExpression) { // body of the loop } Here, The initialExpression initializes and/or declares variables and executes only once. The condition is evaluated. If the condition is true, the body of the for loop is executed.
  • 4. The updateExpression updates the value of initialExpression. The condition is evaluated again. The process continues until the condition is false. To learn more about the conditions, visit Java relational and logical operators.
  • 5. Example 1: Display a Text Five Times // Program to print a text 5 times class Main { public static void main(String[] args) { int n = 5; // for loop for (int i = 1; i <= n; ++i) { System.out.println("Java is fun"); } } }
  • 6. Output: Java is fun Java is fun Java is fun Java is fun Java is fun
  • 7. SOME VARIATIONS ON THE LOOP // Infinite for Loop class Infinite { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 10; --i) { System.out.println("Hello"); } } }
  • 8. Run Code Here, the test expression ,i <= 10, is never false and Hello is printed repeatedly until the memory runs out. Out put Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
  • 9. Multiple loop control variable The for is one of the most versatile statements in the Java language because it allows a wide range of varia tions. One of the most common is the use of multiple loop control variables. When using multiple loop control variables, the initialization and iteration expressions for each variable are separated by commas Here is a simple example: //Use multiple loop control variable in a for. Class Multiple loop control variable in a for. public static void main(string[] args) { Int i,j; for(i=0, j=10; i<j ; i++, j--) --------notice the two loop control variables. system.out.println(“i and j : “ + i + “ “ + j); } }
  • 10. The output from program: i and j : 0 10 i and j : 1 9 i and j : 2 8 i and j : 3 7 i and j : 4 6
  • 11. MISSING PIECES Some interesting for loop variations are created by leaving pieces of the loop definition empty. In Java, a is possible for any or all of the initialization, condition, or intration portions of the for loop to be blank. For example, consider the following program. //Parts of the for can be empty class Empty { public static void main(string[] args) { Int I; For(i=0;i<10;) { System.out.println(“Pass #”+i ); i++; // increment loop control var } }
  • 12. } Here the iteration expression of the for is empty .Instead, the loop control variable i is incremented inside the body of the loop This means that each time the loop repeats, i is tested to see whether it equals 10, but the no no further action takes place Of course, since i is still incremented within the body of the loop, the loop runs normally, displaying the following output: Pass#0 pass #1 Pass #2 Pass #3 Pass #4 Pass #5 Pass #6 Pass #7 Pass #8 Pass #9
  • 13. Loops with No body: In Java, the body associated with a for loop (or any other loop)can be empty. This is because a null statement is syntactically valid. Body-less loops are often useful. For example, the following program uses one to sum the numbers 1 through 5 //The body of a loop can be empty. class Empty3 { public static void main(String[] args) { int i; int sum=0; //sum the numbers through 5 for(i=1; i<=5;sum += i++) ;-----No body in this loop! System .out .println (“Sum is “ + sum); } } The output from the program is shown here: Sum in 15
  • 14. THE ENHANCED FOR LOOP In Java, the for-each loop is used to iterate through elements of arrays and collections (like ArrayList). It is also known as the enhanced for loop. for-each Loop Sytnax The syntax of the Java for-each loop is: for(dataType item : array) { ... } Here, array - an array or a collection item - each item of array/collection is assigned to this variable dataType - the data type of the array/collection Example 1: Print Array Elements
  • 15. // print array elements class Main { public static void main(String[] args) { // create an array int[] numbers = {3, 9, 5, -5}; // for each loop for (int number: numbers) { System.out.println(number); } } }
  • 16. Run Code Output:- 3 9 5 -5 Here, we have used the for-each loop to print each element of the numbers array one by one.
  • 17. In the first iteration, item will be 3. In the second iteration, item will be 9. In the third iteration, item will be 5. In the fourth iteration, item will be -5