Nested if statement within a do-while loop

  • Thread starter Thread starter dudforreal
  • Start date Start date
  • Tags Tags
    If statement Loop
Click For Summary

Discussion Overview

The discussion revolves around the use of a nested if statement within a do-while loop in C programming, specifically focusing on the logic of loop termination conditions and variable usage. Participants explore the implications of using a variable as a boolean check and the clarity of code structure.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants question the use of "while(!x)" instead of "while(x)" for loop termination, suggesting that the former may lead to confusion regarding when the loop should repeat.
  • Others explain that both "while(x)" and "while(!x)" can function correctly but will behave differently based on the intended logic of the program.
  • A participant suggests that the use of an additional variable (x) complicates the code unnecessarily, proposing that the loop could be controlled directly by the input condition instead.
  • Some participants express curiosity about the behavior of the loop when x is set to 1, questioning how the negation impacts the loop's execution.
  • There is a discussion about treating an integer as a boolean, with some participants noting that using an integer can lead to confusion and suggesting the use of a boolean type instead for clarity.
  • A table is proposed to illustrate the boolean logic involved in the loop's condition, highlighting how the initial value of x affects the loop's behavior.
  • Participants discuss the implications of changing variable names and values to improve code readability and logic comprehension.

Areas of Agreement / Disagreement

Participants express differing views on the clarity and appropriateness of using an integer variable for boolean checks, with no consensus reached on the best approach. Some agree that using a boolean type could enhance understanding, while others defend the original approach.

Contextual Notes

Limitations in understanding arise from the initial choice of variable types and naming conventions, which may lead to confusion in interpreting the loop's logic.

Who May Find This Useful

Readers interested in programming logic, particularly in C language, and those looking to improve code clarity and structure may find this discussion beneficial.

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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K