blogger Indonesia

Follow judhyns blog

THE ITERATION LOOP (for LOOP)

Introduction

When you want to execute certain statements repeatedly you can use iteration statements. C has provided three types of iteration statements: the for loop, while loop, and do...while loop. Generally, the statements in the loop are executed until the specified condition is true or false.

Program

 #include  main() { int i,n;  //the scanf("%d",&n); for(i = 0; i

Explanation

  1. Statement A indicates the for loop. The statements in the enclosing braces, such as statement B, indicate the statements that are executed repeatedly because of the for loop.

  2. The format of the for loop is

    for (expr1;expr2;expr3) { s1; s2 ;  // repeat section } 
  3. expr2 is a Boolean expression. If it is not given, it is assumed to be true.

  4. The expressions expr1, expr2 and expr3 are optional.

  5. expr1 is executed only once, the first time the for loop is invoked.

  6. expr2 is executed each time before the execution of the repeat section.

  7. When expr2 is evaluated false, the loop is terminated and the repeat section is not executed.

  8. After execution of the repeat section, expr3 is executed. Generally, this is the expression that is used to ensure that the loop will be terminated after certain iterations.

Points to Remember

  1. The for loop is used for repeating the execution of certain statements.

  2. The statements that you want to repeat should be written in the repeat section.

  3. Generally, you have to specify any three expressions in the for loop.

  4. While writing expressions, ensure that expr2 is evaluated to be false after certain iterations; otherwise your loop will never be terminated, resulting in infinite iterations.

No comments:

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |