What is bools?
bools are used to see if something is true or false.
Is used alot in forms, but you can use it for everything.
Create a new Console Application called Boolean.
Now as in the previous tutorials, u learned about variables.
Now you will learn some more about them and also about local variables.
Okay first we need a variable for the bool.
It could be done like this:
PHP Code:
static bool MyFirstBool;
How do u set it true or false then?
As we got it now, then is true.
If u want it false you can put it:
PHP Code:
static bool MyFirstBool = false;
But more of that later.
Now the class should look like this:
PHP Code:
class Program
{
static bool MyFirstBool;
static void Main(string[] args)
{
}
}
Now how can we use it?
To check bools if is true or false.
Use a simple if check.
False checks:
PHP Code:
!MyFirstBool
MyFirstBool == false
True checks:
PHP Code:
MyFirstBool
MyFirstBool == true
Now lets check if it is true.
We use the 2nd way, as thats the most normal to use.
This should be the class now, if u followed it right.
PHP Code:
class Program
{
static bool MyFirstBool;
static void Main(string[] args)
{
if (MyFirstBool == true)
{
}
}
}
Now you can do a check like
else, for what should happen if is not true.
Could be done like this:
PHP Code:
class Program
{
static bool MyFirstBool;
static void Main(string[] args)
{
if (MyFirstBool == true)
{
}
else
{
}
}
}
Now we need some actions for the code.
Lets make it write "Is true" or "Is not true".
You can do it like u learned in the HelloWorld tutorial with Console.WriteLine.
Now you should have something like this:
PHP Code:
class Program
{
static bool MyFirstBool;
static void Main(string[] args)
{
if (MyFirstBool == true)
{
Console.WriteLine("Is true");
}
else
{
Console.WriteLine("Is not true");
}
}
}
Well, we don't want console to close after, so we put a ReadLine in the end of it.
(Before the last } in the void).
You will learn more about voids etc. later.
Now your code should be like this:
PHP Code:
class Program
{
static bool MyFirstBool;
static void Main(string[] args)
{
if (MyFirstBool == true)
{
Console.WriteLine("Is true");
}
else
{
Console.WriteLine("Is not true");
}
Console.ReadLine();
}
}
Now how can you make it true or false?
You can just use for checking true:
PHP Code:
MyFirstBool = true;
You can just use for checking false:
PHP Code:
MyFirstBool = false;
Result for the true check:
PHP Code:
class Program
{
static bool MyFirstBool;
static void Main(string[] args)
{
MyFirstBool = true;
if (MyFirstBool == true)
{
Console.WriteLine("Is true");
}
else
{
Console.WriteLine("Is not true");
}
Console.ReadLine();
}
}
Result for the false check:
PHP Code:
class Program
{
static bool MyFirstBool;
static void Main(string[] args)
{
MyFirstBool = false;
if (MyFirstBool == true)
{
Console.WriteLine("Is true");
}
else
{
Console.WriteLine("Is not true");
}
Console.ReadLine();
}
}