Suppose, you have to write a program to add the numbers 1 to 10. You will write the following code -
int sum = 1;
sum = sum + 2;
sum = sum + 3;
sum = sum + 4;
sum = sum + 5;
sum = sum + 6;
sum = sum + 7;
sum = sum + 8;
sum = sum + 9;
sum = sum + 10;
Or you will write like this -
int sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10;
So, here the addition operation is a repeated action. In Java, repeated actions can be converted in a loop which executes no of iterations or until a condition is met. Here, we will talk about for loop and it has several forms. The simple syntax for java loop is as follows -
for( initiation statement (one time); condition; increment / decrement (every time) ) { statements }
Initiation statement - When loop starts first time, variables are initialized. This statement executes only once and only at start of the loop.
Condition statement - Checks the condition, if true goes to the loop and executes the statements inside the loop. If condition is false, it comes out from the loop.
Increment / Decrement statement - Once the statements in the loop are executed, variables are updated (increment / decrement) and repeats the condition statements and increment / decrement statements till the condition is false.
We can write above program like this :
Condition statement - Checks the condition, if true goes to the loop and executes the statements inside the loop. If condition is false, it comes out from the loop.
Increment / Decrement statement - Once the statements in the loop are executed, variables are updated (increment / decrement) and repeats the condition statements and increment / decrement statements till the condition is false.
We can write above program like this :
Class ForExample1 (ForExample1.java)
Variations of For Loop
There are variations of the for loop. There are various way to write same for loop.
public class ForExample1 { public static void main(String args[]) { int i, sum = 0; for(i = 1; i <= 10; i++) { sum = sum + i; } System.out.println(sum); } }Here in initialization part, integer variable i is initialized by 1. Then it repeats the following process -
- It checks whether i is less than 10, if it is, it adds i to integer variable sum else comes out from the loop.
- It increments value of variable i and goes back to the condition statement.
Variations of For Loop
There are variations of the for loop. There are various way to write same for loop.
Multiple statements separated with comma
We can have multiple statements in initialization and increment / decrement part, like in the following example -
Class ForExample2 (ForExample2.java)
public class ForExample2 { public static void main(String args[]) { int i, j, sum = 0; for(i = 1, j = 10; i <= j; i++, j--) { sum = sum + i + j; } System.out.println(sum); } }
Here, we have two variables initialized, one is incremented and one is decremented. It comes out from the loop, when both variables are equal.
Declare variables in initialization statement
And also we can declare variable in the for loop itself. Here we have initialized variable i in the initialization statement itself. If you declare any variable with for loop initialization statement or inside the loop, you can not access same variable outside the loop. Scope of the variable is only inside the loop.
Class ForExample3 (ForExample3.java)
public class ForExample3 { public static void main(String args[]) { int sum = 0; for(int i = 1 ; i <= 10; i++) { sum = sum + i; } System.out.println(sum); } }
NOTE: We will discuss more about scope later.
Skipping statemnets
And we can skip one or all parts altogether. We can write same program like this -
Class ForExample4 (ForExample4.java)
public class ForExample4 { public static void main(String args[]) { int i = 1, sum = 0; for( ; i <= 10; ) { sum = sum + i; i++; } System.out.println(sum); } }
Here we have skipped the initialization part and increment /. decrement part.
Without body for loop
And also we can have without body. Here the for loop statement doesn't need any body. It executes itself in the loop. You can see a ; after the loop to end the for loop statement.
Class ForExample5 (ForExample5.java)
public class ForExample5 { public static void main(String args[]) { int i, sum = 0; for(i = 1 ; i <= 10; sum+=i++) ; System.out.println(sum); } }
NOTE: There are few more variations of for loop, we will learn about them later after arrays and collections.
No comments:
Post a Comment