Introduction
c# if...Statement condition
If... statement condition syntax
How c# if statement works?
Example of if statement condition
C# if...else statement
if...else statement syntax
Example 1.2: if...else statement demonstration
c# if...else if...else statement
c# if...else if...else statement syntax
Example 1.3: if... else if... else statement demonstration
Nested if...else statement
c# nested if statement syntax
Example 1.4: Nested if...else statement demonstration
if, if...else, if...else if and the Nested if statement in C# is used to make decisions when we have to test more than one condition at time.
In this article, am going to teach you how to use c# if statement,if else statement,if else and multiple if statement in C# to control program's flow of your program’s execution.
In programming, in order to ensure that your application is performing robustly, you have to make sure that conditions testing are used during your coding. It is not out of point if I tell you that you will often face situations where you need to test conditions whether it is true or false to control the flow of program. And these conditions may be affected several factors such as user's input, programmers spefication etc.
Maybe you might have been facing so many issues in the case of C# if... statement, if...else statement, nested if else statement but after this article, you understand how to test conditions using if statement in C#.
The if... statement will only execute a block of code if only the given condition returns true.
The following is c# if statement syntax
if (expression)
{
// statements to execute
}
When the above block of code is been executed, C# will check if the condition is true or not. However, the expression will definitely return either true or false. If the expression returns true, the statements following the if statement will be executed.
If the expression returns false, the statements inside the body of if will be ignored.
For example,
if (month = 2)
{
MessageBox.Show("We are in the month of February");
}
In this example, the MessageBox
will be executed only if the value of month is 2.
If... statement in C# works the same way just like every other programming languages. It analyze the condition and make a decision based on the instructions given by the programmer.
In other words, an if... statement help us to control any program flow. However, it can also make your program to give abnormal result if not used properly.
In the Example 1.1 below, I have written a simple c# console program that will accept user age and display message that user is eligible to vote if age is greater than 18.
Example 1.1: if statement demonstration
using System;
namespace Conditional
{
class IfExample
{
public static void Main(string[] args)
{
int age;
Console.Write("Enter age to check voting eligibility");
age = Console.ReadLine();
if (age >= 18)
{
Console.WriteLine("You are eligible to vote");
}
}
}
}
If we run this program, the output will only be "You are eligible to vote if user age is greater than or equal to 18.
What the program does is accept input from user, check the accepted input to ascertain if it matches the instructions given to it.
This statement is always executed if the condition returns true.
If you study the example 1.1, you will see that the program only returns "eligible to vote" if the user's input is 18 and above. But, if the user happen to input age below 18, the if block will not be executed meaning at this point, the program will be useless because nothing to tell the user that the age you have inputted is eligible for voting or not.
This is where if...then...else become handing and very useful.
The if...else statement will test for test a condition in two ways. It checks to know if the given condition is true or not. Based on its evaluation, the final result will be either true or false.
Its syntax is as follows
if (expression)
{
// statements to execute
}
else
{
//statement to execute
}
The example 1.2 below demonstrate if...else (if...then...else).
using System;
namespace Conditional
{
class IfElseExample
{
public static void Main(string[] args)
{
int age;
Console.Write("Enter age to check voting eligibility");
age = Console.ReadLine();
if (age >= 18)
{
Console.WriteLine("You are eligible to vote");
}
else
{
Console.WriteLine("Sorry, you are not eligible to vote");
}
}
}
}
If we run the program, the output will be either true or false i.e eligible to vote or not
The if...else statement will process the user's input in order to know if the input is below or above 18 then make its final decision based on the instructions given to it.
For instance, run the program and input value >= 18, you will receive message that you are eligible to vote.
Now, change the value of age to something less than 18, say 17.
You will see that the result will be "sorry you are not eligible to vote"
The if... else if...else is used when we want to test for multiple conditions. Almost all applications used this if... else if...else statement to control program's flow.
Let's take the example 1.2 for instance. We only check to see if the user's input is greater than, equal to or less than 18. What happens if the user input wrong values like alphabets? The program will not be able to handle this cos we didn't test for it. This is where if... else if...else statement become useful.
Something we must take note of here is that the else keyword always end this control state.
It has the following syntax;
if (expression)
{
// statements to execute
}
else if (expression)
{
//statement to execute
}
else
{
//statement to execute
}
using System;
namespace Conditional
{
class IfElseIf_elseExample
{
public static void Main(string[] args)
{
int age;
Console.Write("Enter age to check voting eligibility");
age = Console.ReadLine();
if (age >= 18)
{
Console.WriteLine("You are eligible to vote");
}
else if(age < 18)
{
Console.WriteLine("Sorry, you are not eligible to vote");
}
else
{
Console.WriteLine("Sorry, invalid input. Age must be number");
}
}
}
}
An if...else statement that exists within another if...else statement is referred to as nested if statement. Such statements are called nested if...else statement because it can check for another condition even inside the if block.
The general syntax of nested if…else statement is:
if (expression)
{
if (nested-expression)
{
// code to be executed
}
else
{
// code to be executed
}
}
Nested if statement is used when we have to test one condition followed by another. In a nested if statement, if the outer if statement returns true, it enters the body to check the inner if statement.
Still on the voting eligibility test, we are going to tell users if they are teenagers, youths or adults.
using System;
namespace Conditional
{
class NestedExample
{
public static void Main(string[] args)
{
int age;
Console.Write("Enter age to check voting eligibility");
age = Console.ReadLine();
if (age >= 18)
{
//Beginning of nested if
if (age >= 40)
{
Console.WriteLine("Welcome adult, you are eligible to vote");
}
else
{
Console.WriteLine("Welcome youth, you are eligible to vote");
}//Ending of nested if
}
else if(age < 18)
{
Console.WriteLine("Sorry, you are not eligible to vote");
}
else
{
Console.WriteLine("Sorry, invalid input. Age must be number");
}
}
}
}
Run the program and you will see the effect.