Question regarding While loops
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Question regarding While loops

  1. #1
    Join Date
    Jun 2017
    Location
    Berkeley CA
    Posts
    19

    Question regarding While loops

    Hello all,
    I have a question regarding the following piece of code:

    Code:
    int i=0;
    
    while (i <= 20)
    {
    cout<< i << " ";
    i=i+5
    }
    cout<< endl;
    this is an example from my textbook, they say when the value of i=5 the output is 5, to me it is a little confusing, because 5+5=10 which they say is the new value for i. But that is not what is outputted? what does this new value of i even do then?

    Would you say things like take that new value of i (which is 10) and do something with it?

    Which part of this code is outputting 5?

    Also they say if you omit the statement: i=i+5; from the body of the loop, you will have an infinite loop, continually printing rows of zeros. why is this the case?

    Thanks

    William
    Last edited by cookiemnstr510; July 16th, 2017 at 05:34 PM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,580

    Re: Question regarding While loops

    Use your debugger and step through the code. As written, the code is in an infinite loop.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,829

    Re: Question regarding While loops

    Code:
    int i=0;
    
    while (i <= 20)
    {
         cout<< i << " ";
         i = i+5
    }
    
    cout<< endl;
    Examining this code, first i is of type integer and is initialised to 0. Then there is the while loop. With a while loop, the condition within the round brackets is examined and if the condition is true then the code within the curly brackets is executed and the loop loops back to the beginning of the while loop again and evaluates the condition. This loop continues until the condition is evaluated as false when the code following the closing curly bracket is executed.

    Within the curly brackets in this code, first there is a cout statement. This displays the current value of i then a space. So the first time through the loop the value of i is 0 and so 0 is displayed. Then we have the assignment statement. This might look a little weird compared to maths algebra you might have learnt, but its fairly simple. First the right hand side (rhs) of the = is evaluated using the current values of the variables. So first time round the loop i is 0 so the rhs is 5 as 5 is added to i. The result of the rhs is then assigned to the left hand side (lhs) which is i. So the new value of i is 5.

    We then enter the while condition again. i is less than 20 so the body of the while loop is executed again. i is now 5 so 5 is displayed. the rhs of the assignment evaluates to 10 so i is set to 10 and the whole loop starts again until i is greater than 20 when the loop terminates and the statement after the closing curly bracket is executed - the cout << end; to display a new line.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.4.4)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)