blogger Indonesia

Follow judhyns blog

THE while LOOP

Introduction

The while loop is used when you want to repeat the execution of a certain statement or a set of statements (compound statement).

Program/Example

The general format for a while loop is

    while (condition)     simple or compound statement (body of the loop)     For example, i = 0; while (i<5) {     printf(" the value of i is %d\n", i);     i = i + 1; } 

Explanation

  1. Before entering into the loop, the while condition is evaluated. If it is true then only the loop body is executed.

  2. Before making an iteration, the while condition is checked. If it is true then the loop body is executed.

  3. It is the responsibility of the programmer to ensure that the condition is false after certain iterations; otherwise, the loop will make infinite iterations and it will not terminate.

  4. The programmer should be aware of the final value of the looping variable. For example, in this case, the final value of the looping variable is 5.

  5. While writing the loop body, you have to be careful to decide whether the loop variable is updated at the start of the body or at the end of the body.

No comments:

feed

PR Check

activesearchresults

judhyn's blog

 

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