Looping construct

Hi

Could we use only one looping construct for all conditions ?

Thanks.
Yes.
There are many different ways of doing the same thing.
Choose the one that (in your own opinion) makes it easier to read the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

int main()
{
    const int N = 10 ;
    const int a[N] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;

    int i = 0 ; // initialize counter
    while( i < N ) // check condition
    {
        std::cout << a[i] << ' ' ; // do our stuff for this iteration
        ++i ; // increment counter
    }
    std::cout << '\n' ; // do this when the lop exits

    i = 0 ; // initialize counter
    if( i < N ) do // check condition at the beginning
    {
        std::cout << a[i] << ' ' ; // do our stuff for this iteration
        ++i ; // increment counter
    } while( i < N ) ; // check condition at the end of each iteration
    std::cout << '\n' ; // do this when the lop exits

    for( int i = 0 /* initialize counter */ ; i < N  /* check condition */ ; ++i /* increment counter */ )
        std::cout << a[i] << ' ' ; // do our stuff for this iteration
    std::cout << '\n' ; // do this when the lop exits

    // http://www.stroustrup.com/C++11FAQ.html#for
    for( int v : a ) // for each integer in the array
       std::cout << v << ' ' ; // do our stuff for this iteration
    std::cout << '\n' ; // do this when the lop exits

    std::cout << "a[5] == " << a[5] << '\n' ;
    std::cout << "a[5] == " << *(a+5) << '\n' ;
    std::cout << "a[5] == " << 5[a] << '\n' ;
    std::cout << "a[5] == " << *(5+a) << '\n' ;

    int x = 23 ;
    int y = 23 ;

    if( x == y ) std::cout << "x is equal to y\n" ;

    ++x ; y += 1 ;
    if( !(x<y) && !(y<x) ) std::cout << "x is still equal to y\n" ;

    x = x+1 ; y++ ;
    if( !(x>y) && !(y>x) ) std::cout << "x is still equal to y\n" ;

    x -= 1 ; y += -1 ;
    if( !( x!=y ) ) std::cout << "x is still equal to y\n" ;

}

http://coliru.stacked-crooked.com/a/4e452e7a9b934743
Could we do work with while loop as same as we can do with Do-while and for loop ?
I wanna get information about it, so I could explain C/C++ things easily. and please explain only one loop is sufficient for doing every thing ?

Thanks.
all type kinds of loop has an equal advantages
All looping constructs are ways of specifying transfer of control back to the beginning conditionally (a conditional goto or jump) to repeat a sequence of operations some number of times.

while
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/////////////////////////////////////

    initialise ;

loop_while:
    if( some_condition_is_true )
    {
        // these are not done even once if some_condition_is_not_true at the beginning
        do_something ;
        increment ;
        goto loop_while ;
    }

 ////////////////////////////////////

    initialise ;

    while( some_condition_is_true )
    {
        // these are not done even once if some_condition_is_not_true at the beginning
        do_something ;
        increment ;
    }

 ////////////////////////////////////

    initialise ;

    if( some_condition_is_true )
    {
        // these are not done if some_condition_is_not_true at the beginning
        do
        {
            do_something ;
            increment ;
        } while( some_condition_is_true ) ;
    }

 ////////////////////////////////////

    for( initialise ; some_condition_is_true ; increment /* at the end of each iteration, if some_condition_was_true */ )
    {
        // this is not done even once if some_condition_is_not_true at the beginning
        do_something ;
    }



do-while
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/////////////////////////////////////

    initialise ;

loop_do_while:

    // do these at least once, irrespective of whether, at the beginning, some_condition_is_true or not
    do_something ;
    increment ;

    if( some_condition_is_true )
    {
        goto loop_do_while ; // repeat if some_condition_is_true 
    }

 ////////////////////////////////////

    initialise ;

    // do these at least once, irrespective of whether, at the beginning, some_condition_is_true or not
    do_something ;
    increment ;

    while( some_condition_is_true )
    {
        // repeat these while some_condition_is_true
        do_something ;
        increment ;
    }

 ////////////////////////////////////

    initialise ;

    do
    {
        // do these at least once, irrespective of whether, at the beginning, some_condition_is_true or not
        do_something ;
        increment ;
    } while( some_condition_is_true ) ; // repeat while some_condition_is_true

 ////////////////////////////////////

    // do these at least once, irrespective of whether, at the beginning, some_condition_is_true or not
    initialise ;
    do_something ;
    increment ;

    for( ; some_condition_is_true ; increment /* at the end of the loop, if some_condition_was_true */  )
    {
        // repeat these while some_condition_is_true
        do_something ;
    }


An example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>

int main()
{
    int i ;
    const int N = 10 ;

    ///////////////////////////////////////////////////////////////////////
    ////////////////////////// while ///////////////////////////////////
    ///////////////////////////////////////////////////////////////////////

        i = 0 ;

    loop_while:
        if( i < N )
        {
            // do these only if i < N
            std::cout << i << ' ' ;
            ++i ;
            goto loop_while ;

        }

        std::cout << '\n' ;

     ////////////////////////////////////

        i = 0 ;
        while( i < N )
        {
            // do these only if i < N
            std::cout << i << ' ' ;
            ++i ;
        }

        std::cout << '\n' ;

     ////////////////////////////////////

        i = 0 ;
        if( i < N )
        {
            // do these only if i < N
            do
            {
                std::cout << i << ' ' ;
                ++i ;
            } while( i < N ) ;
        }

        std::cout << '\n' ;

     ////////////////////////////////////

        for( i = 0 ; i < N ; ++i /* do this at the end, if i < N was true */ )
        {
            // do this only if i < N
            std::cout << i << ' ' ;
        }

        std::cout << '\n' ;




    ///////////////////////////////////////////////////////////////////////
    ////////////////////////// do while ///////////////////////////////////
    ///////////////////////////////////////////////////////////////////////

        i = 0 ;

    loop_do_while:

        // do these at least once, irrespective of whether, at the beginning,  i < N or not
        std::cout << i << ' ' ;
        ++i ;

        if( i < N )
        {
            goto loop_do_while ; // repeat loop if i < N
        }

        std::cout << '\n' ;

     ////////////////////////////////////

        i = 0 ;

        // do these at least once, irrespective of whether, at the beginning,  i < N or not
        std::cout << i << ' ' ;
        ++i ;

        while( i < N ) // repeat as long as i < N
        {
            std::cout << i << ' ' ;
            ++i ;
        }

        std::cout << '\n' ;

     ////////////////////////////////////

        i = 0 ;

        do
        {
            // do these at least once, irrespective of whether, at the beginning,  i < N or not
            std::cout << i << ' ' ;
            ++i ;
        } while( i < N ) ; // repeat as long as i < N

        std::cout << '\n' ;

     ////////////////////////////////////

        i = 0 ;

        // do these at least once, irrespective of whether, at the beginning,  i < N or not
        std::cout << i << ' ' ;
        ++i ;

        for( ; i < N ; ++i ) // repeat as long as i < N
        {
            std::cout << i << ' ' ;
        }
        std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/d395384f0c131bf7

In the end, there is only one fundamental looping construct in all of programming; each of the different forms provide syntactic sugar for equivalent functionality; nothing more than a stylised alternative to writing a conditional goto to repeat a sequence of statements.

Sometimes, these stylised constructs, on their own, prove to be inadequate; so we also have continue (stylised goto to the beginning of the loop) and break (stylised goto to the statement immediately after the loop)
I guess understanding goto statement, is difficult than understanding looping construct for beginners , right ?
> I guess understanding goto statement, is difficult than understanding looping construct for beginners , right ?

The goto statement, in isolation, is not difficult to understand for anyone.
The internet is rife with goto horror stories, but in reality, it is a very simple construct that does not do any of those terrible things it is routinely accused of doing. http://www.cplusplus.com/forum/lounge/122363/#msg666967

The looping constructs in the language (while, do-while, classical-for and range-based-for) are ready-made canned constructs for the four canonical loop forms most commonly encountered in programming.

Using these ready-made constructs are easier for all of us (particularly so for beginners) when the loop that we want to write falls under one of these canonical looping forms. These are the kinds of loops we would need to write almost all of the time, and these prefabricated canonical constructs make the code less error-prone and more readable.

Consider writing a loop using goto (working out the loop logic from first principles) only when a non-canonical loop-form is actually required. That is, only when, some one, who believes in evil in certain programming constructs, would strongly recommend a 'bool-controlled-while-loop' with a few continues and/or some number of breaks thrown in for good measure.
Last edited on
Topic archived. No new replies allowed.