3 minute briefing on while

The command “while” allows you to execute other commands until a certain criterion is met. It is very similar to if, because it also evaluates the truth of a statement, and while that truth is met, it executes the next command again and again and again, until the statement becomes false:

int i;
i=0;
while(i<100)
  i=i+1;

The above example might be simple but it illustrates a way to count to 100. Again like the if, in case you want to execute more than one command use {}:

int i;
i=0;
while(i<100){
  i=i+1;
  printf("%i\n",i);
}

The above code not only increments i a hundred times, it also prints it out.

Cheers Arend

Leave a Reply