I used these condition in do while loop , and I still getting the forever loop. It just doesn't stop.
while( x > 0 || x < 23);
CS50 Stack Exchange is a question and answer site for students of Harvard University's CS50. It only takes a minute to sign up.
Sign up to join this communityYour condition is flawed. If x is greater than 0 OR x is less than 23, the loop will keep going. x > 0
includes all positive numbers, and x < 23
includes all negative numbers in addition to positive numbers up to 23. There are no integer values for which both would not be true, and thus you will forever be in that loop. Remember, || (OR) means if either of them are true, the entire thing is true. Simply switching to && will not fix the problem, however. You want your loop condition to be the opposite of the condition you are looking for, so that as long as your desired condition is NOT met, you stay inside the loop.
To get out of a loop, you need two thing
Meanwhile, in case x > 0
or < 23
and it doesn't change, the loop will keep executing infinitely.
Your condition remains true for all test cases. Eg, if you input -10,
while( x > 0 || x < 23)
will evaluate to true since x < 23 is true.
If you input 35,
while( x > 0 || x < 23)
will evaluate to true since x > 0 is true. Hence, this is an infinite loop.
If you wish to run a piece of code for an input between the two numbers, use
while(x > 0 && x < 23)