SlideShare una empresa de Scribd logo
1 de 18
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

Más contenido relacionado

Similar a prt123.pptx

Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 

Similar a prt123.pptx (20)

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
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 

Último

Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan CytotecJual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
ZurliaSoop
 
Mckinsey foundation level Handbook for Viewing
Mckinsey foundation level Handbook for ViewingMckinsey foundation level Handbook for Viewing
Mckinsey foundation level Handbook for Viewing
Nauman Safdar
 
Challenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
Challenges and Opportunities: A Qualitative Study on Tax Compliance in PakistanChallenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
Challenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
vineshkumarsajnani12
 

Último (20)

How to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityHow to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League City
 
New 2024 Cannabis Edibles Investor Pitch Deck Template
New 2024 Cannabis Edibles Investor Pitch Deck TemplateNew 2024 Cannabis Edibles Investor Pitch Deck Template
New 2024 Cannabis Edibles Investor Pitch Deck Template
 
PARK STREET 💋 Call Girl 9827461493 Call Girls in Escort service book now
PARK STREET 💋 Call Girl 9827461493 Call Girls in  Escort service book nowPARK STREET 💋 Call Girl 9827461493 Call Girls in  Escort service book now
PARK STREET 💋 Call Girl 9827461493 Call Girls in Escort service book now
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
 
Durg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTS
Durg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTSDurg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTS
Durg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTS
 
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
 
HomeRoots Pitch Deck | Investor Insights | April 2024
HomeRoots Pitch Deck | Investor Insights | April 2024HomeRoots Pitch Deck | Investor Insights | April 2024
HomeRoots Pitch Deck | Investor Insights | April 2024
 
Kalyan Call Girl 98350*37198 Call Girls in Escort service book now
Kalyan Call Girl 98350*37198 Call Girls in Escort service book nowKalyan Call Girl 98350*37198 Call Girls in Escort service book now
Kalyan Call Girl 98350*37198 Call Girls in Escort service book now
 
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan CytotecJual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
 
Ooty Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Avail...
Ooty Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Avail...Ooty Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Avail...
Ooty Call Gril 80022//12248 Only For Sex And High Profile Best Gril Sex Avail...
 
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165
 
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business Growth
 
Buy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail AccountsBuy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail Accounts
 
Paradip CALL GIRL❤7091819311❤CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
Paradip CALL GIRL❤7091819311❤CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDINGParadip CALL GIRL❤7091819311❤CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
Paradip CALL GIRL❤7091819311❤CALL GIRLS IN ESCORT SERVICE WE ARE PROVIDING
 
Mckinsey foundation level Handbook for Viewing
Mckinsey foundation level Handbook for ViewingMckinsey foundation level Handbook for Viewing
Mckinsey foundation level Handbook for Viewing
 
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
 
Challenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
Challenges and Opportunities: A Qualitative Study on Tax Compliance in PakistanChallenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
Challenges and Opportunities: A Qualitative Study on Tax Compliance in Pakistan
 
Call 7737669865 Vadodara Call Girls Service at your Door Step Available All Time
Call 7737669865 Vadodara Call Girls Service at your Door Step Available All TimeCall 7737669865 Vadodara Call Girls Service at your Door Step Available All Time
Call 7737669865 Vadodara Call Girls Service at your Door Step Available All Time
 
Getting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAI
Getting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAIGetting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAI
Getting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAI
 

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