Advertisement



< Prev
Next >


C# If-else Statement





The general principle of the if-else statement is that the condition in the parenthesis of its if is evaluated for a boolean value - true or false.



Note: Similar to its cousin Java, the C# language also requires a pair of braces { } to hold multiple statements associated with an if or else statement




Advertisement




if else example


//C# - if else statement program
 

using System;
 
class A
{
public static void Main()
{

	int a = 100, b = 50;
	float f1 = 2.5f, f2 = 5.9f;
	
	//If there is just one statement associated with an if-else block then 
	//no curly braces are needed
	if(a>b)
		Console.WriteLine("{0} is greater than {1}", a, b);
	else 	
		Console.WriteLine("{0} is less than {1}", a, b);


	//If there is more than one statement associated with either of the if/else block in if-else then 
	//a pair of curly braces are needed	
	if(f1>f2)
		Console.WriteLine("{0} is greater than {1}", f1, f2);
	else
	{
		Console.WriteLine("{0} is less than {1}", f1, f2);
		Console.WriteLine("Second statement to execute in else");
	}
}
}


Output -

100 is greater than 50
2.5 is greater than 5.9
Second statement to execute in else


Please share this article -





< Prev
Next >
< C# If Statement
C# Else If Statement >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement