Chapter 5: If, else if, and else.
In programming, you'll find yourself needing to perform some action if a variable has a value, or if greater than, or less than, or any check, these can be done through if statements.
an if basically evaluates to, check something true > do something.
Remember bools? You wondered what is the use of them before right? Well, if statements are all about booleans, they can get pretty huge, but the ultimate result basically checs if something is true.
let's look at an example.
int number = 15;
if (number == 15)
{
Console.WriteLine("Number is 15");
}
- if tells the language we want to check something here, the parens used are required and must wrap the condition.
- number == 15 is an expression, an expression is an action in programming that evaluates to give you a value, for example, number == 15 checks if number is equal to 15.
- The opening brace {, after the if is something that opens a block, blocks help the language know what code belongs to a statement, in our case, the if statement, so whatever comes between the braces belongs to the if statement, and it naturally can span multiple lines, like normal code.
- the WriteLine call here, wrapped in the braces after the if statement is the code that executes if number == 15 evaluated to be true.
- The closing brace, }, ends the block belonging to the if.
and it's not all.
what if, we wanted to also write something to the console if it wasn't 15?
int number = 15;
if (number == 15)
{
Console.WriteLine("Number is 15");
}
else
{
Console.WriteLine("Number is not 15");
}
the else statement executes only when the if statement before it fails, an else cannot exist without an if.
Ok, but I still want to handle more cases,let's say I want to check for more than one number.
int number = 15;
if (number == 15)
{
Console.WriteLine("Number is 15");
}
else if (number == 10)
{
Console.WriteLine("Number is 10");
}
else
{
Console.WriteLine("Number doesn't match 15 or 10.");
}
but we can write this a different way, for simpler conditions, something like this can also work.
int number = 10;
if (number == 15 || number == 10)
{
Console.WriteLine($"Number match: Number is {number}");
}
else
{
Console.WriteLine("Number does not match");
}
the || is called a logical or operator, it basically works when the first expression fails, then it tries the one after that, so if number is 10, it tries to check of it equals to 15, returns false, continues to check it's 10, returns true, then the code under the if executes, if both are false however, it goes to the else, if there is no else, it does nothing.
There's also something similar, called the logical and operator, &&
int number = 10;
if (number < 20 && number > 5)
{
Console.WriteLine("Number is more than 5 and less than 20");
}
the && operator is similar to the || or operator, but the difference is that instead of one exppression succeeding for it to continue to the code inside the if block, the && ensures that, while that condition is true this must also be true, if one succeeds and the other fails, both will fail, all must succeed for it to continue.
the < and > operators check if something is less than or greater than a number, respectively
there are also other operators that work similarly.
- <= less than or equal too, as an example, if we did if (number <= 10) above, this will be true, because it's either less or equal to 10, and it's equal to 10.
- >= works similarly to <=, but checks for greater than.
there are other operators for bitwise manipulation, but I decided not to cover them here and instead cover them in a later chapter because I consider them complex for beginners.
now, let's use if conditions for a practical use, it's time to guess the number.
a small guess the number game.
a common beginner game example that is simple but interesting to play with is guess the number, it is a game where the program gives you input and you are supposed to guess the number between 0 to 100.
let's implement this.
int number = 33;
Console.WriteLine("Enter your guess: between 1 and 100");
int guess = int.Parse(Console.ReadLine());
if (guess == number)
{
Console.WriteLine("Congratulations! You guessed right");
}
else if (guess < number)
{
Console.WriteLine("Too low");
}
else
{
Console.WriteLine("Too high.");
}
The int.Parse() line converts a string variable to an int. This is used because Console.ReadLine returns a string.
P.S. If you don't put a valid number there the program will crash, it can be handled, but I didn't want to overwhelm any beginner reading this with a wall of information.
Now, this is all fun and nice, but there's one tiny problem...
The number is always 33.
Well, let's do better, then.
Random random = new();
int number = random.Next(1, 101);
Console.WriteLine("Enter your guess: between 1 and 100");
int guess = int.Parse(Console.ReadLine());
if (guess == number)
{
Console.WriteLine("Congratulations! You guessed right");
}
else if (guess < number)
{
Console.WriteLine("Too low");
}
else
{
Console.WriteLine("Too high.");
}
there, much better.
the Random line creates an instance of a class, for now don't worry about it, because we'll cover classes in a later chapter.
Next, loops.