Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Nested if statement within a do-while loop

  1. May 27, 2012 #1
    I don't understand why this would work, shouldn't it be "while(x)" because we don't want to repeat the loop?

    Code (Text):

    int main(void)
    {
    int x=0;
    do {
    printf("Enter an amount:\n");
    scanf("%lf", &amount);
    if (amount < 100)
       x=1;
    else
       printf("Invalid");
    }while(!x);
    return 0;
    }
     
     
  2. jcsd
  3. May 27, 2012 #2

    chiro

    User Avatar
    Science Advisor

    Hey dudforreal.

    The difference between a while loop and a do loop is that the termination check in a while loop gets executed before each cycle of the loop starts and the do loop executes the termination check at the end of the cycle.

    Does this help answer your question?
     
  4. May 27, 2012 #3

    Borek

    User Avatar

    Staff: Mentor

    Your question doesn't make much sense.

    Both while(x) and while(!x) will work, just differently. One will repeat till the amount is lower than 100, other will repeat till the amount is greater or equal 100. Whether they will be doing what you want depends on what you want - and you have not explained what is the expected behavior.

    Why do you complicate the code by addition of another variable (x) if you can close the loop with while(amount < 100) or while(amount >= 100)?
     
  5. May 27, 2012 #4
    From my code I want to check if the amount is less than 100 and to do this I use x to check, which I saw from the answer to a similar question
     
  6. May 27, 2012 #5

    Borek

    User Avatar

    Staff: Mentor

    Then repeat reading the input till amount >= 100, you don't need additional variables for that. Just because someone else did it this way doesn't mean he was right.
     
  7. May 28, 2012 #6
    I'm just curious why this code will work because it was part of a larger code and x was used as a check. When we got the desired result, x=1, wouldn't "!x" repeat the loop when we don't want it to repeat so to exit the loop?
     
    Last edited: May 28, 2012
  8. May 28, 2012 #7

    Borg

    User Avatar
    Science Advisor
    Gold Member

    The check is treating an int essentially as a boolean. In these cases, 0 is treated as false while every other value is considered to be true. While it is allowed in Java, it's not a normally accepted practice because of the confusion that you're encountering.

    Your 'boolean' value will only repeat the loop if the value is true. But, since the check is !x, it repeats if it's "not false". In this case, any x value that isn't zero will cause it to end. If you change your x to an actual boolean, it will become more clear how the loop works. Try to give the boolean a name that means something when you read it - like 'changed'.
     
    Last edited: May 28, 2012
  9. May 28, 2012 #8
    if 0 is false then !false is true which is 1 and it will repeat the loop?
     
  10. May 28, 2012 #9

    Borg

    User Avatar
    Science Advisor
    Gold Member

    Fixed that for you. This is why it's confusing. Yes it, will repeat in this case. If you replace int x = 0 with boolean changed = false, it is much easier to understand.
     
  11. May 28, 2012 #10
    Can you elaborate further so don't quite understand, isn't 1 true?
     
  12. May 28, 2012 #11

    Borg

    User Avatar
    Science Advisor
    Gold Member

    Give me a couple of minutes. I'm working on a table that should hopefully clarify a little better.
     
  13. May 28, 2012 #12

    Borg

    User Avatar
    Science Advisor
    Gold Member

    Let's take a look at the three lines that are controlling your loop:

    Variable: int x =0;
    Modifier: x = 1;
    Boolean: !x

    The following table below shows the boolean value of !x for the case where the modifier gets set (to 1) and the case where it doesn't get set (still 0).

    Code (Text):
    Variable     Modifier     boolean (if modified)       boolean (if not modified)
    int x = 0    x = 1        false (x=1 so !x is false)  true(x=0 so !x is true)
    Part of the confusion is the !x check. You're forced to reverse your logic in order to get the code to work. This happens when a variable is given poor initial values or naming conventions. Change the three lines that are controlling your loop to the following:

    Variable: int x =1;
    Modifier: x = 0;
    Boolean: x (note that we're not checking for !x now - just x)

    Your table now becomes this because your aren't having to reverse your logic:

    Code (Text):
    Variable     Modifier     boolean (if modified)       boolean (if not modified)
    int x = 1    x = 0        false (x=0)                   true(x=1)
    Finally, if you change to the following, your code becomes much easier to understand.

    Variable: boolean unchanged = true;
    Modifier: unchanged = false;
    Boolean: unchanged

    How would the table look with these values? Try reading your code out loud with these values and it becomes very easy to understand the logic.
     
  14. May 28, 2012 #13
    why are we only checking for x? what do you mean by modified?
     
  15. May 28, 2012 #14

    Borg

    User Avatar
    Science Advisor
    Gold Member

    I'm showing you how the logic table would look if you change your variables to be the opposite of what you're using so you have to reverse all three. I am using the word 'modified' to mean that the value of x has been modified from it's initial value of 0 (your x = 1 line). I'm also trying really hard to get you to not use ints in a boolean check.
     
  16. May 28, 2012 #15
    How come the boolean is unchanged, isn't it changed in the two tables?
     
  17. May 28, 2012 #16

    Borg

    User Avatar
    Science Advisor
    Gold Member

    Let's try something different. In your code, change the while(!x) line to an equivalent statement:

    while(x == 0);

    Does it make more sense now?
     
  18. May 28, 2012 #17
    So through boolean, !x is testing x=1 and not the starting x=0?
     
  19. May 28, 2012 #18

    Borg

    User Avatar
    Science Advisor
    Gold Member

    The statement that I wrote (x == 0) is just checking the value of x - is it 0 or not? I could have just as easily written this:

    while(x != 1)

    or this:

    while (x < 1)
     
  20. May 28, 2012 #19

    Borek

    User Avatar

    Staff: Mentor

    Yes, but no. Any non-zero value is treated as true.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Nested if statement within a do-while loop
  1. Do while loop (Replies: 6)

Loading...