“if” instruction works very simple, we put condition to brackets e.g if a > 10 if condition will be true, we will execute code in brackets, if condition won’t be true, will execute code in instruction else, but it doesn’t works on conversely, without “if” instruction can’t be else instruction, if won’t be “if” will execute next lines of code.
Here is an example only with the if statement (and is greater than b so will execute the code in the curly brackets in the if statement)
int a = 234;
int b = 23;
if(a>b)
{
Console.WriteLine("a is greater than b");
}
Console.WriteLine("End instruction");
Now the if statement with the else statement
int a = 234;
int b = 23;
if (a < b)
{
Console.WriteLine("a is greater than b");
}
else
{
Console.WriteLine("not true variable a is greater than b");
}
Console.WriteLine("End instruction");
In this example, the else statement will be executed because the condition made in the if statement was not true
There is one more option worth knowing, let’s look at this piece of code:
int a = 234;
int b = 23;
if(a<b)
{
Console.WriteLine("a is greater than b");
}
if(a>b)
{
Console.WriteLine("not true variable a is greater than b");
}
if(a==b)
{
Console.WriteLine("a is equal b");
}
Console.WriteLine("End instruction");
Here we have to check several conditions, the computer will check them all, even if the second one is correct then it will check the third condition, the computer loses then on computing power therefore there is a conditional “else if” statement which if correct will not execute the next instructions below and the computer will be able to take a little rest 🙂
Here is a small example with an else if statement
int a = 234;
int b = 23;
if(a==234)
{
Console.WriteLine("a is equal 234");
}
else if(a>b)
{
Console.WriteLine("not true variable a is greater than b");
}
else if(a==b)
{
Console.WriteLine("a is equal b");
}
Console.WriteLine("End instruction");
Here the first instruction will be executed, so the compiler will skip the rest of the instructions even if the second one is also true
In fact, the “else if” statement is a shortened version of such code, much more illegible, right?
int a = 234;
int b = 23;
if (a == 234)
{
Console.WriteLine("a is equal 234");
}
else
{
if (a > b)
{
Console.WriteLine("not true variable a is greater than b");
}
else
{
if (a == b)
{
Console.WriteLine("a is equal b");
}
}
}
Console.WriteLine("End instruction");
The switch is used when the given value is to call a specific action, using the if statement you can do it, but using the switch statement is all more readable. This looks something like this:
static void Main(string[] args)
{
int number;
Console.Write("Enter number: ");
number = int.Parse(Console.ReadLine());
switch (number)
{
case 0:
Console.WriteLine("Your number is 0!");
break;
case 1:
Console.WriteLine("Your number is 1!");
break;
case 2:
Console.WriteLine("Your number is 2!");
break;
case 3:
Console.WriteLine("Your number is 3!");
break;
case 4:
Console.WriteLine("Your number is 4!");
break;
default:
Console.WriteLine("You gave too big number!");
break;
}
Console.ReadKey();
}
You should always remember to “break” otherwise each “case” will be done
Still this line looks strange, right?
number = int.Parse (Console.ReadLine ());
In order not to be confused, the Console.ReadLine() function converts the entered value into a string type and we need the int type and the intParse () function changes into the int type.
This content also you can find on my blog devman.pl: http://devman.pl/csharplan/c-language-3-conditional-instructions/
If you recognise it as useful, share it with others so that others can also use it and leave upvote and follow if you wait for next articles :) .
I think it’s enough today. See you in the next lesson!