Learn More

Tuesday, December 27, 2011

Loop Group Structure

c++ threelas logo
The ability of someone when making a c++ programming will decrease if there is no loop structure. So it is not surprised if loop structure and if structure always used together in c++ programming. For that background, we write this tutorial about loop group structure.




For Structure

This structure will work based on an increment or decrement. This structure very effective for prevent in writing the same statements. Especially, if contain so many same statements.

for (variable_name=initial_value;condition;increment){
statement;
}

See the example below




The program above will writing words i with it values from one until five, where the increment of this program is one. This fact can be seen from i++ statement (see our explanation).






While Structure


This structure will do looping based on a condition. If the condition is true then it will do looping. For general, this structure will has form as follow



Variable_name = initial_value;

While (condition) {

Statement;

Variable_name++;

}


See the simple example below



The result of this program is same with the result from the first program. From this program we can see that the initial value is one, but the condition is true if i less than or equal with five. It is mean the initial value is under that condition. So that the while structure will do looping. One thing you must notice, if you using while loop then you must add i++ increment (or other increment), if you don’t do it this then your program will not stop.




Do ... While Structure

The different between this structure with while structure is, this structure will do checking on the last term of the condition. And the while structure will do checking on the first term of the condition. The general form for this structure is

Variable_name = initial_value;
do {
statement loop;
Variable_name++;
} while (condition);

See the example below


The result of this program is same as before.

  1. I want to add another difference between while and do-while...

    The contents of do-while is executed at least once, while the contents of while maybe executed at least once and maybe never been executed, depends on the initial check of condition.

    [code]
    int n = -1;
    while (n > 0) {
    cout << n << endl;
    --n;
    }
    [/code]

    will print nothing. but:

    [code]
    int n = -1;
    do {
    cout << n << endl;
    --n;
    } while (n > 0);
    [/code]

    will print:
    -1

    ReplyDelete
    Replies
    1. For Muhammad Alvin:
      Thanks. This good for others. We like this add, very basic, so the readers more understand between while and do-while.

      Delete
  2. When using for (initial; condition; increment), you are not forced to use all the 3 parameters. You can for example write for (;;) that is equivalent with while (true).

    What you have to know when using for (initial; condition; increment) is the control flow. The basic form of for is:

    for (_initial_; _condition_; _increment_) {
    _body_
    }

    and it is equivalent with:

    _initial_
    while (_condition_ is true) {
    _body_
    _increment_
    }

    Try to write the following code:
    for (int i = 1; i <= 10; i++) {
    cout << i << endl;
    }

    then compare the result with the following code:
    int i = 1;
    while (i <= 10) {
    cout << i << endl;
    i++;
    }

    ReplyDelete
    Replies
    1. For Muhammad Alvin:
      Thanks for your share. If we do that in this post, then we never explain step by step to others. We hope you can understand for that. On the next future, we will make it, so the readers can understand the different and when they must using both.

      Delete