if - else Statement Tutorials

Using if - else

While writing a program, we may sometimes need to perform different actions depending on whether a condition is fulfilled or not. In such cases, the if-else control structure is used. 

In this structure, a condition is specified in order to control the situation, and which actions to be taken if this condition is fulfilled and which actions to be taken in case of failure are written separately.

The use of the if else construct is as follows:

if (our condition)

{

Actions if true

}

else

{

Actions if wrong

}

 

Explanation : After the if statement, the condition is written in parenthesis.

If the condition is true, the actions to be taken are written between the first curly brackets.

Else part is optional. If the condition is false, the operations to be performed are written between the parenthesis after the else command.

Example: The following example must be done while studying this subject. The program that says "Fail" if the grade entered by the student is less than 50, and "Passed" if it is higher:

Console.Write("Enter your note: ");
int a = int.Parse(Console.ReadLine());
 
if(a<50)
{
Console.WriteLine("Failed.");
}
else
{
Console.WriteLine("Passed.");
}
 
Console.ReadKey();

Else part is optional. In other words, if there is nothing to do when the condition is false, the else part may not be written. The example below gives a warning when a number less than 0 is entered. Since else is not used, nothing will be done if the condition is not met.

Console.Write("Enter number: ");
int a = int.Parse(Console.ReadLine());
 
if(a<0)
{
Console.WriteLine("Enter a positive number.");
}
 
Console.ReadKey();

In addition, curly braces may not be used if the operations to be performed when the condition is true or false consist of 1 line: 

Console.Write("Enter number: ");
int a = int.Parse(Console.ReadLine());
 
if(a<0)
Console.WriteLine("Enter a positive number.");
       
Console.ReadKey();

Another example of parenthesis use:

Console.Write("Enter your note: ");
int a = int.Parse(Console.ReadLine());
 
if(a<50) Console.WriteLine("You failed.");
else Console.WriteLine("Passed.");
 
Console.ReadKey();

If the code we will write is more than one line, we must put it between curly brackets.

Using else if

If the number of possible possibilities is more than 2, we can specify the actions to be taken for each situation by using else if.

After the if command, we start by writing our first condition. For other conditions else if is used.

If none of the conditions are met, we write the codes we want to be done in the else section. The else part is still optional. 

Example: Converting the grade entered in the 100 system to the 5 system:

Console.Write("Enter your note: ");
int a = int.Parse(Console.ReadLine());
 
if (a < 0)
    Console.WriteLine("Enter a positive number.");
else if (a < 50)
    Console.WriteLine("Your grade: 1");
else if (a < 60)
    Console.WriteLine("Your grade: 2");
else if (a < 70)
    Console.WriteLine("Your grade: 3");
else if (a < 85)
    Console.WriteLine("Your grade: 4");
else if (a < 100)
    Console.WriteLine("Your grade: 5");
else
    Console.WriteLine("You entered an invalid note.");
       
Console.ReadKey();

 

c# console if else tutorials, using if else statement, if else examples, use of elseif if not

EXERCISES

if else Tutorials
  1. Program to find out if the entered number is odd or even
  2. If the entered number is divisible by both 2 and 3, “OK”, otherwise “ERROR” message.
  3. "Multiple of 2 and 3" if the entered number is divisible by both 2 and 3, "multiple of 2" if it is only divisible by 2, "multiple of 3" if it is only divisible by 3, neither 2 nor 3' If it is not divisible by e, the program that gives the message "not a multiple of 2 or 3"
  4. If the entered number is divisible by both 4, 5, and 6, the program displays the message "Number OK", otherwise "Number Not Available".
  5. If the entered number is divisible by at least one of the numbers 4, 5 and 6, the program that gives the message "Number OK", otherwise "Number Not Available".
  6. If the entered number is between 0 and 100 and is even, the program displays "Number OK", otherwise "Number Not Available".
  7. If at least one of the 3 numbers entered is greater than 50, the program gives the message "Sufficient", otherwise "Insufficient"
  8. If the 3 numbers entered are all greater than 0 and even, the program displays “Successful Login”, otherwise “Failed login”.
if else exercise: Is the number entered odd or even?
Console.Write("Enter number: ");
int a = int.Parse(Console.ReadLine());
 
if (a % 2 == 0)
    Console.WriteLine("Even Number.");
else
    Console.WriteLine("Odd Number.");
       
Console.ReadKey();

 



COMMENTS




Read 500 times.

Online Users: 144



if-else-tutorials