Introduction to if, if else, if else if and Nested if Statement in C#| ternary operator C# | C# Case Statement
If statements are used where we have two situation and we want to get one based on some results and condition. C# If Statement is used for this purpose.
Here is a step by step guide to learn if or if else statements:
- Create a New project in Visual Studio
- Choose C# (Console Application)
- Suppose we have assign a grade based on marks of Students in Exams i.e. if Marks is greater than 80% he will get A grade and if marks is less than 80% he will get B grade. In this situation we will write a program which will asked from the students marks after that program will show the result.
- After creating program starts writing code in Main(string[] args) method.
- Here is the coding of Simple if Statement in C#
Example Code of If Statement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace If_Demo_Codegainers.com
{
class Program
{
static void Main(string[] args)
{
// To print the sentence which is in ” ”
Console.WriteLine(“Please Enter Student Marks Here”);
/*Console. Readline() will get the Data from the user
Students Marks will be saved in int StudentMarks
Convert.ToInt32 is converting the input from the user to Int.
*/
int StudentMarks =Convert.ToInt32(Console.ReadLine()) ;
//Checking if Student Marks is greater than 80
if (StudentMarks>80)
{
Console.Write(“Your Grade is A”);
}
else
{
// if first condition is not true this will print the below line
Console.WriteLine(“Your Grade is B”);
}
// this is to keep the console visible during run.
Console.ReadKey();
}
}
}
You just need to copy and paste the above code in your program to see the result.
2nd Examples if else if statement
In second example we are making a game. You can say it “Guess Game“. We will ask from user to pick a number between 1 to 100. After that system will generate a Random Number between 1 to 100, if user’s guess number will match with the generated random number user will won. Moreover, we’ll also add a secret number that will be a choice to win the game.
So let’s Start coding…
So let’s Start coding…
Hint. If you are beginner you must first know that how generate random numbers after that making this game is very easy. Have fun!
Console.WriteLine(“Guess Games Starts….”);
Console.WriteLine(“Please Guess a Number between 1 to 100”);
//Reading Input of User and Storing it in guessNumber variable
int guessNumber=Convert.ToInt32(Console.ReadLine());
//generating random numbers
Random rand = new Random();
//random number will store in number variable
int number=rand.Next(100);
//matching the number with random number to show the result
if (guessNumber==number)
{
Console.WriteLine(“Wow, You’ve Won”);
}
else if (guessNumber==78)// this condition will be run if the first is wrong
{
Console.WriteLine(“Wow, You’ve Won a Mobile”);
}
else // this will run if both condition will be false
{
Console.WriteLine(“Ops, Your guess is wrong”);
}
Console.ReadKey();
3rd Examples Nested If statement
In this example we’ll try to explain how you could use nested if statements. Nested if statement is like double checking the data if will be in another if. If one condition will be true there will be another condition to check the data.
We are making an example where we’ll ask from the user if he is male or female. If he is male his age will also be asked to check if he is greater than 18 and if she is female, she will be allowed to go without checking her age.
Console.WriteLine(“3rd Example
Starts….”);
Starts….”);
Console.WriteLine(“Please
Selecte Gender Male or Female”);
Selecte Gender Male or Female”);
//Reading
Input of User and Storing it in gender variable
Input of User and Storing it in gender variable
string gender = Console.ReadLine();
if (gender==“Male”)// first checking if gender
is male
is male
{
Console.WriteLine(“Please Enter
Your Age”);
Your Age”);
int age =Convert.ToInt32(Console.ReadLine());
if (age>18) //
checking if age is greater than 18
checking if age is greater than 18
{
Console.WriteLine(“Your age is greater than 18 you are allowed”);
}
else
{
Console.WriteLine(“You are is less than 18, you are not allowed”);
}
}
else if(gender==“Female”)
{
Console.WriteLine(“You are
female, you are allowed”);
female, you are allowed”);
}
else
{
Console.WriteLine(“Your choice
is wrong”);
is wrong”);
}
Console.ReadKey();