LOOPS IN C PROGRAMMING LANGUAGE - DEEP CODER

Breaking

printf("Learn Deep Coding Here!!");

LOOPS IN C PROGRAMMING LANGUAGE

In C,C++ and Java, the iteration statements, for loop, while loop and do-while loop, allow the set of instructions to be repeatedly executed, till the condition is true and terminates as soon as the condition becomes false. 

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.
  1. for (initialization; condition; iteration)
  2. {
  3. //body of for loop
  4. }
  • 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.
  1. 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.
  1. public class Main
  2. {
  3. public static void main(String[] args)
  4. {
  5. int array[]={10, 20, 30, 40, 50, 60};
  6. int add=0;
  7. for( int c: array)
  8. {
  9. System.out.println( "value in c " + c);
  10. add = add+c;
  11. }
  12. System.out.println("additon of array elements is " +add);
  13. }
  14. }
  15. // output
  16. value in c 10
  17. value in c 20
  18. value in c 30
  19. value in c 40
  20. value in c 50
  21. value in c 60
  22. 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
  1. while ( condition)
  2. {
  3. statements; //body of loop
  4. }
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.
  1. //example is in Java.
  2. public class Main
  3. {
  4. public static void main (String args[])
  5. {
  6. int n=0;
  7. while(n<10)
  8. {
  9. n++;
  10. System.out.println("n=" +n);
  11. }
  12. }
  13. }
  14. //output
  15. n=1
  16. n=2
  17. n=3
  18. n=4
  19. n=5
  20. n=6
  21. n=7
  22. n=8
  23. n=9
  24. 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.
  1. do{
  2. .
  3. statements // body of loop.
  4. .
  5. } 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.
  1. // example is in Java.
  2. class example2{
  3. public static void main (String args[] ){
  4. int n=1;
  5. do{
  6. System.out.println("n=" +n);
  7. n++;
  8. }while(n<=10);
  9. }
  10. }
  11. //output
  12. n=1
  13. n=2
  14. n=3
  15. n=4
  16. n=5
  17. n=6
  18. n=7
  19. n=8
  20. n=9
  21. 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