Advertisement



< Prev
Next >


C# If Statement





In C#, an If statement is used when we want to test a condition, which is evaluated for a boolean value - true or false.






Note:





Advertisement




C# - if statement example


//C# - if 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 block then 
	//no curly braces are needed
	if(a>b)
		Console.WriteLine("100 is greater than 50");


	//If there is more than one statement associated with an if block then 
	//curly braces are needed	
	if(b<a)
	{
		Console.WriteLine("50 is less than 100");
		Console.WriteLine("Second statement to execute");
	}


	//You may add curly braces even with a single statement associated with an if block
	if(f1>f2)
	{
		Console.WriteLine("2 is greater than 4");
	}


	//This statement associated with any if block
	Console.WriteLine("Statement outside an If Statement"); 
}
}


Output is -


100 is greater than 5
50 is less than 100
Second statement to execute
Statement outside If Statement



Program Analysis





Please share this article -





< Prev
Next >
< C# variables, keywords, constants
C# If Else 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

x-fast