Successfully reported this slideshow.
Your SlideShare is downloading. ×

3. Flow Controls in C (Part II).pdf

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
5. Functions in C.pdf
5. Functions in C.pdf
Loading in 3
×

Check these out next

1 of 15 Ad
Advertisement

More Related Content

Recently uploaded (20)

Advertisement

3. Flow Controls in C (Part II).pdf

  1. 1. C PROGRAMMING Flow Controls By, Mr. Shekharkumar
  2. 2. Loop statements What is loop? Loop is used to execute the block of code several times according to the condition given in the loop. It means it executes the same code multiple times so it saves code
  3. 3. Types of loops for loop while loop do...while loop
  4. 4. Syntax of for loop for (initialization Statement; test Expression; update Statement; { Code }
  5. 5. How for loop works? The initialization statement is executed only once. Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop is terminated. However, if the test expression is evaluated to true, statements inside the body of the for loop are executed, and the update expression is updated. Again the test expression is evaluated. This process goes on until the test expression is false. When the test expression is false, the loop terminates.
  6. 6. Flowchart of for loop
  7. 7. Example of for loop Print numbers from 1 to 10 #include <stdio.h> void main() { int i; for (i = 1; i < 11; ++i) { printf("%d ", i); } }
  8. 8. Syntax of while loop while (testexpression) { Code }
  9. 9. How while loop works? The while loop evaluates the test expression inside the parentheses (). If test expression is true, statement inside the body of while loop are executed .Then test expression is evaluated again. The process goes until test expression is evaluated false. If test expression is false, the loop terminates.
  10. 10. Flowchart of while loop
  11. 11. Example of while loop Print numbers from 1 to 5 #include <stdio.h> void main() { int i = 1; while (i <= 5) { printf("%dn", i); ++i; } }
  12. 12. The syntax of do...while loop do { Code } while (test expression)
  13. 13. How do...while loop works? The body of do...while loop is executed once. Only then, the test expression is evaluated. If test expression is true, the body of the loop is executed again and test expression is evaluated once more. This process goes on until test expression becomes false. If test expression is false, the loop ends.
  14. 14. Flowchart of do...while Loop
  15. 15. Example of do安hile loop Program to accept numbers until the user enters zero #include <stdio.h> void main() { double number, sum = 0; // the body of the loop is executed at least once do { printf("Enter a number: "); scanf("%lf", &number); sum += number; } while(number != 0.0); printf("Sum = %.2lf",sum); }