Nested if statement within a do-while loop

In summary, 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. The use of the variable "x" in this code adds complexity and confusion, as it is being used as a boolean value. A more efficient way to write this code would be to use the amount variable as the condition for the while loop. The use of !x in the do loop will cause it to repeat until x is false, which can be confusing. It would be clearer to use a boolean variable with a more meaningful name, such as "changed".
  • #1
dudforreal
116
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
  • #2
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?
 
  • #3
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)?
 
  • #4
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
 
  • #5
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.
 
  • #6
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:
  • #7
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:
  • #8
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?
 
  • #9
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.
 

Related to Nested if statement within a do-while loop

What is a nested if statement within a do-while loop?

A nested if statement within a do-while loop is a programming construct that combines the use of conditional statements and loops. It allows multiple conditions to be checked within a loop, and the loop will continue to run until a certain condition is met.

How is a nested if statement within a do-while loop structured?

The structure of a nested if statement within a do-while loop is as follows:

do {
    //code to be executed
    if (condition1) {
        //code to be executed if condition1 is true
    }
    if (condition2) {
        //code to be executed if condition2 is true
    }
    //code to be executed at the end of each loop
} while (condition3);

Why would you use a nested if statement within a do-while loop?

A nested if statement within a do-while loop allows for more complex and specific conditions to be checked within a loop. This can be useful when you want to perform different actions based on multiple conditions, or when you want the loop to continue until a certain combination of conditions is met.

What is the difference between a nested if statement within a do-while loop and a regular if statement?

The main difference is that a nested if statement within a do-while loop will continue to loop until a certain condition is met, while a regular if statement will only execute once and then move on to the next line of code. Additionally, a nested if statement within a do-while loop can check for multiple conditions, while a regular if statement can only check for one condition.

What are some common mistakes when using a nested if statement within a do-while loop?

Some common mistakes include forgetting to include the necessary curly braces to separate the code within the if statements, or forgetting to update the conditions within the loop so that it eventually ends. It is also important to make sure the conditions within the if statements are properly written and will evaluate to either true or false.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
822
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
4
Views
949
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
1
Views
975
  • Programming and Computer Science
Replies
6
Views
6K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top