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

PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
PPTX
Introduction to Java Programming - Lecture 11.pptx
PPT
Java presentation
PPTX
Chapter 5 java
PPTX
for loop in java
DOCX
Java loops
PDF
how to write loops in java explained vividly
PPTX
FOR LOOP TOPIC(syntax, flow diagram and examples).pptx
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
Introduction to Java Programming - Lecture 11.pptx
Java presentation
Chapter 5 java
for loop in java
Java loops
how to write loops in java explained vividly
FOR LOOP TOPIC(syntax, flow diagram and examples).pptx

Similar to prt123.pptx (20)

PPT
Looping statements in Java
PPTX
Java loops for, while and do...while
PDF
Control structures in Java
PPTX
PPTX
Looping statements
PPT
Eo gaddis java_chapter_05_5e
PPT
Eo gaddis java_chapter_05_5e
PPTX
dizital pods session 5-loops.pptx
PPTX
C-PROGRAMING FOR LOOP PPT
PPTX
Loop(for, while, do while) condition Presentation
PPTX
07 flow control
PPT
Programming loop
PDF
ICP - Lecture 9
PDF
PPTX
Iterative control structures, looping, types of loops, loop working
PPTX
While , For , Do-While Loop
PPTX
Looping statements
PPTX
For loop java
PPTX
COM1407: Program Control Structures – Repetition and Loops
PPT
Cso gaddis java_chapter4
Looping statements in Java
Java loops for, while and do...while
Control structures in Java
Looping statements
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
dizital pods session 5-loops.pptx
C-PROGRAMING FOR LOOP PPT
Loop(for, while, do while) condition Presentation
07 flow control
Programming loop
ICP - Lecture 9
Iterative control structures, looping, types of loops, loop working
While , For , Do-While Loop
Looping statements
For loop java
COM1407: Program Control Structures – Repetition and Loops
Cso gaddis java_chapter4

Recently uploaded (20)

PDF
BeMetals_Presentation_September_2025.pdf
PDF
El futuro empresarial 2024 una vista gen
PDF
The Evolution of Legal Communication through History (www.kiu.ac.ug)
PPTX
TS - CIM-as of august 2023 .pptx
PDF
the role of manager in strategic alliances
PDF
Chembond Chemicals Limited Presentation 2025
PPTX
organizational behavior notes prepared by sonam lama sawan lama
PDF
From Legacy to Velocity: how we rebuilt everything in 8 months.
PDF
Unit-1 Introduction to Electronic-Commerce.pptx
PDF
Не GPT єдиним: можливості AI в бізнес-аналізі | Вебінар з Тетяною Перловською
 
PPTX
Accounting Management SystemBatch-4.pptx
PPTX
Enterprises are Classified into Two Categories
PPTX
Oracle Cloud Infrastructure Overview July 2020 v2_EN20200717.pptx
PDF
COVID-19 Primer for business case prep.pdf
PDF
The Relationship between Leadership Behaviourand Firm Performance in the Read...
PPTX
IndustrialAIGuerillaInnovatorsARCPodcastEp3.pptx
PDF
The Impact of Policy Changes on Legal Communication Strategies (www.kiu.ac.ug)
PPTX
Warehouse. B pptx
PDF
109422672-Doc-8973-05-Security-Manual-Seventh-Edition.pdf
PDF
The Impact of Historical Events on Legal Communication Styles (www.kiu.ac.ug)
BeMetals_Presentation_September_2025.pdf
El futuro empresarial 2024 una vista gen
The Evolution of Legal Communication through History (www.kiu.ac.ug)
TS - CIM-as of august 2023 .pptx
the role of manager in strategic alliances
Chembond Chemicals Limited Presentation 2025
organizational behavior notes prepared by sonam lama sawan lama
From Legacy to Velocity: how we rebuilt everything in 8 months.
Unit-1 Introduction to Electronic-Commerce.pptx
Не GPT єдиним: можливості AI в бізнес-аналізі | Вебінар з Тетяною Перловською
 
Accounting Management SystemBatch-4.pptx
Enterprises are Classified into Two Categories
Oracle Cloud Infrastructure Overview July 2020 v2_EN20200717.pptx
COVID-19 Primer for business case prep.pdf
The Relationship between Leadership Behaviourand Firm Performance in the Read...
IndustrialAIGuerillaInnovatorsARCPodcastEp3.pptx
The Impact of Policy Changes on Legal Communication Strategies (www.kiu.ac.ug)
Warehouse. B pptx
109422672-Doc-8973-05-Security-Manual-Seventh-Edition.pdf
The Impact of Historical Events on Legal Communication Styles (www.kiu.ac.ug)

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