You are on page 1of 8

How to make a Condition

We can create conditions with the help of comparison and


logical operators like the ones below:
Examples of logical operators: &&,||,!
Examples of comparison operators: >,<,<=,>=,==,!=

If (8 >= 2) //We read this as if 8 is greater than 2


{
Console.WriteLine(“8 is bigger than 2”);
}
else //We read this as if 8 is NOT greater than 2
{
Console.WriteLine(“8 is not bigger than 2”);
}
Output:

8 is bigger than 2
How to clean code with Variables
In code we don’t like magic numbers so we use variables!

int a = 8;
int b = 2;

If (a >= b)
{
Console.WriteLine(a + ”is bigger than” + b);
} else
{
Console.WriteLine(a + “is not bigger than” + b);
}
Output:

8 is bigger than 2
How to clean code with Variables
We can change the variable and the rest of the code follow

int a = 10;
int b = 3;

If (a >= b)
{
Console.WriteLine(a + ”is bigger than” + b);
} else
{
Console.WriteLine(a + “is not bigger than” + b);
}
Output:

10 is bigger than 3
Making Code more flexible
We can use user-input to gather and use data for our own.

Console.WriteLine("first number:");
string a = Console.ReadLine();
Console.WriteLine("second number");
string b = Console.ReadLine();

int aNum = Convert.ToInt32(a);


int bNum = Convert.ToInt32(b);

if (aNum >= bNum)


{
Console.WriteLine( a+" is bigger than "+b);
}
else
{
Console.WriteLine(a+" is not bigger than "+b);
}
Output:

first number:
7 (type any number and press enter)
second number:
8 (type any number and press enter)
7 is not bigger than 8.

You might also like