Nested if statement within a do-while loop

  • Thread starter Thread starter dudforreal
  • Start date Start date
  • Tags Tags
    If statement Loop
AI Thread Summary
The discussion revolves around the use of a nested if statement within a do-while loop, specifically questioning why the loop condition is written as while(!x) instead of while(x). The key point is that a do-while loop checks the termination condition after executing the loop body, allowing the code to run at least once. Both while(x) and while(!x) can function correctly but yield different behaviors based on the intended logic. It is suggested that using a boolean variable instead of an integer for the loop condition can clarify the code's intent and avoid confusion. The overall consensus emphasizes the importance of clear variable naming and understanding boolean logic in programming.
dudforreal
Messages
116
Reaction score
0
I don't understand why this would work, shouldn't it be "while(x)" because we don't want to repeat the loop?

Code:
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;
}
 
Technology news on Phys.org
dudforreal said:
I don't understand why this would work, shouldn't it be "while(x)" because we don't want to repeat the loop?

Code:
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;
}

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?
 
dudforreal said:
I don't understand why this would work, shouldn't it be "while(x)" because we don't want to repeat the loop?

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)?
 
Borek said:
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)?

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
 
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.
 
Borek said:
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.

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:
dudforreal said:
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?
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:
Borg said:
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'.

if 0 is false then !false is true which is 1 and it will repeat the loop?
 
dudforreal said:
if 0 is false then !false is true [STRIKE]which is 1[/STRIKE] and it will repeat the loop?
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.
 
  • #10
Borg said:
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.

Can you elaborate further so don't quite understand, isn't 1 true?
 
  • #11
dudforreal said:
Can you elaborate further so don't quite understand, isn't 1 true?
Give me a couple of minutes. I'm working on a table that should hopefully clarify a little better.
 
  • #12
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:
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:
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.
 
  • #13
Boolean: x (note that we're not checking for !x now - just x)

why are we only checking for x? what do you mean by modified?
 
  • #14
dudforreal said:
why are we only checking for x? what do you mean by modified?
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.
 
  • #15
Boolean: unchanged

How come the boolean is unchanged, isn't it changed in the two tables?
 
  • #16
dudforreal said:
How come the boolean is unchanged, isn't it changed in the two tables?
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?
 
  • #17
Borg said:
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?

So through boolean, !x is testing x=1 and not the starting x=0?
 
  • #18
dudforreal said:
So through boolean, !x is testing x=1 and not the starting x=0?
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)
 
  • #19
dudforreal said:
Can you elaborate further so don't quite understand, isn't 1 true?

Yes, but no. Any non-zero value is treated as true.
 
Back
Top