Skip to main content

C# Control Statements (if-else)

C# if-else

In C# programming, the if statement is used to test the condition. There are various types of if statements in C#.
  • if statement
  • if-else statement
  • nested if statement
  • if-else-if ladder

C# IF Statement

The C# if statement tests the condition. It is executed if condition is true.
Syntax:
  1. if(condition){  
  2. //code to be executed  
  3. }  

C# If Example

  1. using System;      
  2. public class IfExample  
  3.     {  
  4.        public static void Main(string[] args)  
  5.         {  
  6.             int num = 10;  
  7.             if (num % 2 == 0)  
  8.             {  
  9.                 Console.WriteLine("It is even number");  
  10.             }  
  11.               
  12.         }  
  13.     }  
Output:
It is even number

C# IF-else Statement

The C# if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.
Syntax:
  1. if(condition){  
  2. //code if condition is true  
  3. }else{  
  4. //code if condition is false  
  5. }  

C# If-else Example

  1. using System;      
  2. public class IfExample  
  3.     {  
  4.         public static void Main(string[] args)  
  5.         {  
  6.             int num = 11;  
  7.             if (num % 2 == 0)  
  8.             {  
  9.                 Console.WriteLine("It is even number");  
  10.             }  
  11.             else  
  12.             {  
  13.                 Console.WriteLine("It is odd number");  
  14.             }  
  15.               
  16.         }  
  17.     }  
Output:
It is odd number

C# If-else Example: with input from user

In this example, we are getting input from the user using Console.ReadLine() method. It returns string. For numeric value, you need to convert it into int using Convert.ToInt32() method.
  1. using System;      
  2. public class IfExample  
  3.     {  
  4.        public static void Main(string[] args)  
  5.         {  
  6.             Console.WriteLine("Enter a number:");  
  7.             int num = Convert.ToInt32(Console.ReadLine());  
  8.   
  9.             if (num % 2 == 0)  
  10.             {  
  11.                 Console.WriteLine("It is even number");  
  12.             }  
  13.             else  
  14.             {  
  15.                 Console.WriteLine("It is odd number");  
  16.             }  
  17.               
  18.         }  
  19.     }  
Output:
Enter a number:11
It is odd number
Output:
Enter a number:12
It is even number

C# IF-else-if ladder Statement

The C# if-else-if ladder statement executes one condition from multiple statements.
Syntax:
  1. if(condition1){  
  2. //code to be executed if condition1 is true  
  3. }else if(condition2){  
  4. //code to be executed if condition2 is true  
  5. }  
  6. else if(condition3){  
  7. //code to be executed if condition3 is true  
  8. }  
  9. ...  
  10. else{  
  11. //code to be executed if all the conditions are false  
  12. }  
C# If else-if Example
  1. using System;      
  2. public class IfExample  
  3.     {  
  4.         public static void Main(string[] args)  
  5.         {  
  6.             Console.WriteLine("Enter a number to check grade:");  
  7.             int num = Convert.ToInt32(Console.ReadLine());  
  8.   
  9.             if (num <0 || num >100)  
  10.             {  
  11.                 Console.WriteLine("wrong number");  
  12.             }  
  13.             else if(num >= 0 && num < 50){  
  14.                 Console.WriteLine("Fail");  
  15.             }  
  16.             else if (num >= 50 && num < 60)  
  17.             {  
  18.                 Console.WriteLine("D Grade");  
  19.             }  
  20.             else if (num >= 60 && num < 70)  
  21.             {  
  22.                 Console.WriteLine("C Grade");  
  23.             }  
  24.             else if (num >= 70 && num < 80)  
  25.             {  
  26.                 Console.WriteLine("B Grade");  
  27.             }  
  28.             else if (num >= 80 && num < 90)  
  29.             {  
  30.                 Console.WriteLine("A Grade");  
  31.             }  
  32.             else if (num >= 90 && num <= 100)  
  33.             {  
  34.                 Console.WriteLine("A+ Grade");  
  35.             }  
  36.         }  
  37.     }  
Output:
Enter a number to check grade:66
C Grade
Output:
Enter a number to check grade:-2
wrong number

Comments

Popular posts from this blog

C# -Type Conversion

Type Conversion means to Convert one data type to another. Generally type conversion is done in two form Implicit Type Conversion : In This type of conversion the conversion is done automatically by the compiler, from smaller data type to larger data type or from derived classes to base class Explicit Type Conversion: In This type of conversion the conversion is done automatically by the compiler, from smaller data type to larger data type or from derived classes to base class Implicit Type Conversion Example: 1 2 3 4 int i_num; i_num = 78 ; double d_num; d_num = i_num; // Implicit Conversion Explicit Type Conversion Example : 1 2 3 4 int i_num; double d_num; d_num = 78.45 ; i_num = ( int )d_num; //Explicity Type Conversion C# provide three difference ways for data type conversion Type Cast Operator ()  Convert Class Paring Type Cast operation we have used in the above example. The second is Convert Class  which has different methods...

How to debug or execute C# program in Visual Studio?

Using Visual Studio 2005/2008/2012/2013 or any version Visual Studio is easiest way to handle C# code. To execute the program on visual studio, go through the following steps: Step 1: Launch Visual Studio and go to File > New Project Step 2: Select Visual C# in left pane and then choose Console Application. Name your Project and select Location and ok Step 3: Visual Studio will open a code editor window including some necessary code. Step 5: Now write now Write in Main method the following line of code  code and press F5 to run C# code. Console.WriteLine("Hello Welcome to ChashLearner"); Step 6: You will see the  output in Visual Studio Command Prompt.