If Else Control Statements in C#

Introduction

The flow of program control using the given condition whether the condition is “TRUE” or “FALSE”. Some keyword are given below which are used for selection statements:

  • If
  • Else
  • Switch
  • Case
  • Break
  • Default

Here we will discuss if else statement.

Definition

The if statement runs when the condition is “TRUE”, while when the condition is “FALSE” the else statement runs.

Example

  1. if(statement==true//condition true  
  2. {  
  3.    //Execute this part.  
  4. }  
  5. else //condition false  
  6. {  
  7.    //Execute this part.   
  8. }  

Simple Code

  1. static void Main(string[] args)   
  2. {  
  3.     int a = 2; //initialization and declaration..  
  4.     int b = 3; //initialization and declaration..  
  5.     int c; //initialization..  
  6.     c = a + b; //doing sum of a & b and store in c..  
  7.     if (c == 5) //checking condition..  
  8.     {  
  9.         Console.Write("Condition True.."); //true condition code..  
  10.     }   
  11.     else //false statement  
  12.     {  
  13.         Console.Write("conditiion false.."); //false condition code..  
  14.     }  
  15.     Console.ReadKey();  
  16. }  
Ebook Download
View all
Learn
View all