Table of Contents
else if Ladder in C#: Syntax and Usage Explained
Questions
- What is the purpose of using an if-else-if ladder in programming, and why is it important?
- Why is the if-else-if ladder essential in programming, and what benefits does it provide?
- What advantages does the if-else-if ladder offer in decision-making within code?
- In what ways does the if-else-if ladder enhance code functionality and clarity?
- "What are the key reasons for implementing an if-else-if ladder in programming?"
The C# if-else-if ladder statement executes one condition from multiple statements.
if(condition1) { //code to be executed if condition1 is true } else if(condition2) { //code to be executed if condition2 is true } else if(condition3) { //code to be executed if condition3 is true } ... else{ //code to be executed if all the conditions are false }
Program
using System;
public class IfExample
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a number to check grade:");
int num = Convert.ToInt32(Console.ReadLine());
if (num <0 || num >100)
{
Console.WriteLine("wrong number");
}
else if(num >= 0 && num < 50){
Console.WriteLine("Fail");
}
else if (num >= 50 && num < 60)
{
Console.WriteLine("D Grade");
}
else if (num >= 60 && num < 70)
{
Console.WriteLine("C Grade");
}
else if (num >= 70 && num < 80)
{
Console.WriteLine("B Grade");
}
else if (num >= 80 && num < 90)
{
Console.WriteLine("A Grade");
}
else if (num >= 90 && num <= 100)
{
Console.WriteLine("A+ Grade");
}
}
}
Output
Output 1:
Enter a number to check grade:66
C Grade
Output 2:
Enter a number to check grade:-2
wrong number