Showing posts with label while and do...while Loops. Show all posts
Showing posts with label while and do...while Loops. Show all posts

Thursday, February 18, 2016

'while' Loop in C

After 'for' loop, let's look at 'while' loop in C language. Unlike 'for' loop, 'while' loop contains only a single expression and it is used as the test condition. The initialization and modification expressions of 'for' loop is omitted in 'while' loop. The general syntax of'while'loop is given below.
  1. while(expression)
  2. {
  3. statement block
  4. }
If the 'expression' evaluates to true, i.e. it evaluates to a non-zero value, code in the 'statement block' gets executed. And after execution of 'statement block', the expression is evaluated again to check it's truth value and this process continues until the expression evaluates to false.
So if you place a non-zero value as the 'expression', 'while' loop executes infinitely unless you have an explicit 'break'statement as part of 'statement block'.
  1. while(1) {
  2. statement block
  3. }
In the above code snippet, the 'statement block' gets executed infinitely.

'while' Loop Example in C

We can write our program to print first 10 numbers using 'while'loop as given below
  1. #include<stdio.h>
  2. void main()
  3. {
  4. int index = 0;
  5. while(index < 10)
  6. {
  7. printf("%d\n", index++);
  8. }
  9. }
  10.  
  11. Output:
  12. 0
  13. 1
  14. 2
  15. 3
  16. 4
  17. 5
  18. 6
  19. 7
  20. 8
  21. 9
Here, the index is initialized during declaration and it is incremented inside 'while' code block. The 'while' statement contains only test condition (index < 10). Note that, here the increment is post-increment. So, the 'printf' will print the value starting from 0 up to 9 asindex++ will return the present value of index and then increment it by 1.
In case of 'while' loop and 'for' loop if you omit the braces, then only the immediate statement will be considered as the loop body.

Wednesday, February 17, 2016

Loops in C - for, while and do...while Loops

Looping statement saves us from writing same code blocks again and again and executes the specific code block multiple times until some conditions are met. We may have known how many times we need to execute the code block or this decision could be made on the go. Looping has a solution for both cases.

'for' Loop in C

Most commonly used loop statement in C programming language is 'for' loop. Syntax of'for'loop is as given below.
  1. for(initialize expression; test condition; index modification expression)
  2. {
  3. Statement block
  4. }
The 'for'loop generally includes three expressions separated by semicolon (;),
  1. Init expression, which is executed first. You can initialize the loop control variable at this point. This expression is optional.
  2. Test condition is evaluated next. The body of loop is executed only if this condition evaluates to true. If this condition evaluates to false, the control moves to the next line after'for' loop
  3. Index modification expression is executed after body of the loop. You can update the loop control variable at this location. After the execution of index modification expression, loop test condition is evaluated again and this procedure follows until the test condition is evaluated to false

'for' Loop Example in C

Suppose we want to print first 10 integers starting from 0. How we can write this using for loop? What will be the three expressions? Let’s look at the following program.
  1. #include<stdio.h>
  2. void main()
  3. {
  4. int index;
  5. for(index = 0; index < 10; index++)
  6. {
  7. printf("%d\n", index);
  8. }
  9. }
  10.  
  11. Output:
  12.  
  13. 0
  14. 1
  15. 2
  16. 3
  17. 4
  18. 5
  19. 6
  20. 7
  21. 8
  22. 9
First, the initialize expression assigns the value 0 to 'index' variable. It declares that the loop will run with initial value 0. The second expression or test condition checks whether the value of the 'index' is less than 10 or not. The last expression modifies the 'index' variable, i.e. increments its value by 1. So, at start index is 0, the test condition is satisfied and the code block enclosed in braces is executed.