Definition of for loop
In Java, there are two forms of for loops. The first form is “traditional” form and the second is “for-each” form.
Syntax
The general form of traditional for loop statement.
- for (initialization; condition; iteration)
- {
- //body of for loop
- }
- Initialization – The initialization of the loop controlling variable of for loop is executed only once, during the first iteration of the loop. Here, the loop controlling variable is initialized, sometimes if the loop variable is not used again anywhere in the program and is only used as the controlling variable of the loop, then it is both declared and initialized in the ‘for’ loop.
- Condition – The condition of the ‘for’ loop is executed each time the loop is iterated.
- Increment and iteration– The iteration statement is an expression that increment or decrements the loop controlling variable.
Whenever the loop is executed, its initialization condition is executed first; then the condition is checked. If the condition is satisfied, the body of the loop is executed, then the iteration statement is executed. Then again, the condition is checked to know whether the loop will iterate further or will terminate.
In Java, the initialization statement and the iteration statement may include more than one statement. Each statement is separated by other by a comma, in Java, a comma is a separator whereas, in C++, “comma” is an operator that can be used in any valid expression.
for-each loop and its syntax
The “for-each” form is an enhanced for loop. The general form of the for-each loop is as follow.
- for(type iter_variable: collection) statement-block
Here, the “type” specifies the type of iteration variable, followed by the iteration variable. The iteration variable will receive the element from the collection variable. The type must be the same as the type of elements stored in the collection variable. The for-each form of for loop automates the iteration of the loop from starting to end accessing the values in sequential order.
Example
There are various types of collection used with for loop. Let’s discuss it with an array as a collection.
- public class Main
- {
- public static void main(String[] args)
- {
- int array[]={10, 20, 30, 40, 50, 60};
- int add=0;
- for( int c: array)
- {
- System.out.println( "value in c " + c);
- add = add+c;
- }
- System.out.println("additon of array elements is " +add);
- }
- }
- // output
- value in c 10
- value in c 20
- value in c 30
- value in c 40
- value in c 50
- value in c 60
- additon of array elements is 210
Here, ‘c’ is an iteration variable; it receives the values from array[ ], one at a time, from the lowest index to the highest index in the array. Here, the loop iterates until all the elements of the array are examined. It is possible to terminate the loop in between by using “break”. However, the change in the iteration variable does not affect the array, as it is only a read-only variable.
STAR PATTERNS IN C SO CLICK
Definition of while loop
The while loop is the most fundamental loop available in C++ and Java. The working of a while loop is similar in both C++ and Java.
Syntax
The declaration of a while loop is as follows
- while ( condition)
- {
- statements; //body of loop
- }
The while loop initially checks the condition and then executes the statements till the condition in while loop turns out to be true. The condition in while loop can be any boolean expression. When an expression returns any non-zero value, then the condition is true, and if the expression returns a zero value, the condition becomes false.
If the condition becomes true, then loop iterates itself, and if the condition becomes false, then the control passes to the next line of the code immediately followed by the loop. The statements or the body loop can either be an empty statement or a single statement or a block of statements.
Example
Let’s discuss the working of a while loop. In the example below the code will print from 1 to 10.
- //example is in Java.
- public class Main
- {
- public static void main (String args[])
- {
- int n=0;
- while(n<10)
- {
- n++;
- System.out.println("n=" +n);
- }
- }
- }
- //output
- n=1
- n=2
- n=3
- n=4
- n=5
- n=6
- n=7
- n=8
- n=9
- n=10
Here, the initial value of ‘n’ is 0, which makes the condition in while loop true. The control then enters the body of the while loop and the value of ‘n’ is incremented according to the first statement in the body of a while loop. The value of ‘n’ is printed, then the control goes back to the condition in a while loop, now the value of ‘n’ is 1 which again satisfies the condition, and the body of the loop is executed again. This continues till the condition is true, as soon as the condition becomes false, the loop is terminated.
Definition of do-while Loop
As in the while loop, if the controlling condition becomes false in the first iteration only, then the body of the while loop is not executed at all. But the do-while loop is somewhat different from while loop. The do-while loop executes the body of the loop at least once even if the condition is false at the first attempt.
Syntax
The general form of do-while is as follows.
- do{
- .
- statements // body of loop.
- .
- } while( Condition );
In a do-while loop, the body of loop occurs before the controlling condition, and the conditional statement is at the bottom of the loop. As in while loop, here also, the body of the loop can be empty as both C++ and Java allow null statements or, there can be only a single statement or, a block of statements. The condition here is also a boolean expression, which is true for all non-zero value.
In a do-while loop, the control first reaches to the statement in the body of a do-while loop. The statements in the body get executed first, and then the control reaches the condition part of the loop. The condition is verified and, if it is true, the loop is iterated again, and if the condition is false, then the control resumes to the next line immediately after the loop.
Example of the do-while loop
Let’s understand it by implementing the above example in do-while.
- // example is in Java.
- class example2{
- public static void main (String args[] ){
- int n=1;
- do{
- System.out.println("n=" +n);
- n++;
- }while(n<=10);
- }
- }
- //output
- n=1
- n=2
- n=3
- n=4
- n=5
- n=6
- n=7
- n=8
- n=9
- n=10
Here, the value of n=1 the control resumes to the body of the loop, the value of ‘n’ is printed, and then its value is incremented. Then control resumes to the condition of the do-while loop; the condition is verified, which turns out true for n=1, so, the loop is iterate’s again and continue till the condition become false.
No comments:
Post a Comment