If Else Statement in C#
Dec 29, 2020 07:46 0 Comments C# (C-Sharp) PARTH

                       If Else Statement in C#: 

 In C#, we have an if statement which is executed if the condition is true otherwise it does not execute. Let us say, we want to execute something if the condition is false. For this purpose, we use the else statement. Else statement is used with an if statement to execute some block of code if the given condition is false.   Else statement can contain single or multiple statements inside the curly brackets. We can skip the curly brackets if the else statement only contains a single statement. The statements inside the else block may contain another if-else statement as well.  

Syntax:

if(condition)

{   

   // statements execute if condition is true 

}

else

{    

  // statements execute if condition is false 

}     

Below you can see the flow chart representation of the if else statement - Let us understand the if else statement with an example -

using System;   

class Program

{

static public void Main()

{     

string x = "Parth";     

string y = "ParthJolly";   

   if (x == y)  

   {    

      Console.WriteLine(“Both are same”);   

   }   

else  

   {     

      Console.WriteLine(“Both are different”);  

    }

}

}

    Output: Both are different

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect