C
C#3y ago
T3CH

Exiting if loop inside of a while statement and starting over

in my else if statement, how do i make it start at the beginning of the while loop if number is lower or higher than guess?
while (true)
{
if (guess == number)
{
Console.WriteLine("Correct!");
break;
}
else if (guess<number)
{
Console.WriteLine("Number is higher!");
}
else
{
Console.WriteLine("Number is lower!");
}
}
}
while (true)
{
if (guess == number)
{
Console.WriteLine("Correct!");
break;
}
else if (guess<number)
{
Console.WriteLine("Number is higher!");
}
else
{
Console.WriteLine("Number is lower!");
}
}
}
16 Replies
TheBoxyBear
TheBoxyBear3y ago
continue to go to the next iteration and break to stop the loop, though you should't be using while(true) Base the while on the exit condition do-while if you don't want to check the exit condition before the first iteration
Bin
Bin3y ago
if you dont break it then the while loop will start over from beginning (when all statements finished), and dont forget to put exit condition unless if u want make it infinitely loop put continue and it will go back to first statement in while loop
T3CH
T3CHOP3y ago
while (true)
{
if (guess == number)
{
Console.WriteLine("Correct!");
break;
}
else if (guess<number)
{
Console.WriteLine("Number is higher!");
continue;
}
else
{
Console.WriteLine("Number is lower!");
}
}
}
while (true)
{
if (guess == number)
{
Console.WriteLine("Correct!");
break;
}
else if (guess<number)
{
Console.WriteLine("Number is higher!");
continue;
}
else
{
Console.WriteLine("Number is lower!");
}
}
}
TheBoxyBear
TheBoxyBear3y ago
Base the loop on guess == number
Bin
Bin3y ago
since you put the number is lower inside else statement, it will not execute if number is higher
TheBoxyBear
TheBoxyBear3y ago
Since there is no guess at the start, do-while to guess before evaluating and potentially exiting the loop
T3CH
T3CHOP3y ago
im so confused lol
Bin
Bin3y ago
if if statement is executed, the following else statement will not executed
T3CH
T3CHOP3y ago
mhm
Bin
Bin3y ago
so i guess in your case u can just omit the continue keyword
TheBoxyBear
TheBoxyBear3y ago
The continue approach works but is less readable. In general you want to avoid while(true) if you can
T3CH
T3CHOP3y ago
oh ok
TheBoxyBear
TheBoxyBear3y ago
Replacing true with an exit condition, such as number == guess, or in this case number != guess since you want to loop until it's equal
Bin
Bin3y ago
you can refactor it like for example: bool isGuessTrue = false; if (!isGuessTrue) { //some code eventually setting isGuessTrue to true}
TheBoxyBear
TheBoxyBear3y ago
That too will prevent evaluating the number when it's already been done Starting with true, you just have to set it to false if < or >
mtreit
mtreit3y ago
I almost always use while (true) and then just use break when the exit criteria are met. It's not a bad practice to use while (true) for those kind of loops. Because sometimes your exit criteria can be complex.
Want results from more Discord servers?
Add your server