C
C#2y ago
moshimoshi

❔ Understanding guidelines around use of 'bool'

Hi folks, i need help understanding the usage of bool before a loop. I've looked at the way some people write their code, sometimes bool is made equals to true and sometimes false. Are there certain guidelines for this?
7 Replies
vdvman1
vdvman12y ago
It entirely depends on what you are doing in the loop and what that bool is for
moshimoshi
moshimoshi2y ago
In this code, for example, could you explain why bool is made false initially?
/* Write a static method ReadInteger(string message) that would return an integer. The
method should prompt the user with the message, get the input from the user using
Console.ReadLine and if the input can be parsed into integer return the integer. If the
input cannot be parsed into integer, the method should repeat the prompt until the user
enter an integer input. */

class ExerciseHQ1
{

static int ReadInteger(string message)
{
Console.Write(message);
int output;
bool isParsable = false;

do
{
isParsable = Int32.TryParse(Console.ReadLine(), out output);


if (!isParsable)
{
Console.WriteLine("Your input is not an integer.");
}
} while (!isParsable);

return output;
}

static void Main(string[] args)
{
int x = ReadInteger("Please enter x: ");
Console.WriteLine(x);
}

}
/* Write a static method ReadInteger(string message) that would return an integer. The
method should prompt the user with the message, get the input from the user using
Console.ReadLine and if the input can be parsed into integer return the integer. If the
input cannot be parsed into integer, the method should repeat the prompt until the user
enter an integer input. */

class ExerciseHQ1
{

static int ReadInteger(string message)
{
Console.Write(message);
int output;
bool isParsable = false;

do
{
isParsable = Int32.TryParse(Console.ReadLine(), out output);


if (!isParsable)
{
Console.WriteLine("Your input is not an integer.");
}
} while (!isParsable);

return output;
}

static void Main(string[] args)
{
int x = ReadInteger("Please enter x: ");
Console.WriteLine(x);
}

}
vdvman1
vdvman12y ago
In that specific case it doesn't matter, and you can even leave out the = false, because it immediately gets replaced unconditionally in the isParsable = ... line at the start of the loop They probably set it to false because they use !isParsable in the while part, so they're making absolutely sure it always runs once (although the do ... while already does that) They probably wanted it to not be affected if that do ... while was converted to a while
moshimoshi
moshimoshi2y ago
Ah I see! If lets say i initialize a bool but leave it undefined and then use it in a loop, would bool = true or bool = false let me leave the loop?
vdvman1
vdvman12y ago
Which "leaves the loop" is entirely dependent on how you write the condition inside the while(...) Also not all while loops will even use a bool variable
moshimoshi
moshimoshi2y ago
Ah okay, got it thanks sir! 🫡 Also i'd just started learning modular programming but am kinda struggling to wrap my head around the concepts. Are there any online resources youd recommend? 🙂
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts