Let us understand the basic structure of the C# program before we jump to the if statement.
To print C# if Statement as output.
using System; //declaring namespace class Example1 //declaring class { static void Main(string[] args) { //declaring class method Console.WriteLine("C# IF STATEMENT"); //print } }
Output:
This article basically focuses on the C# IF statement, so let us get on with it step by step.
The conditional if statement accepts a boolean expression or a condition inside brackets or as a parameter which is followed by a single line or multi-line block of code. During the runtime, when the program has been executed, the condition inside the brackets is evaluated. If this boolean expression results in true, then the code block following the if statement will be executed.
Consider the following example where the if condition contains true as an expression.
The syntax of the if the statement is –
if(a conditional statement or boolean expression) { // the block of code to be executed if the expression results into true }
Let us understand this further with an example.
Consider –
using System; class Ex2 { static void Main(string[] args) { { if(true) Console.WriteLine("True Condition: We are inside the for loop"); if(false) Console.WriteLine("False Condition: We will not be able to enter inside the for loop"); } } }
For Example –
using System; class Ex3 { static void Main(string[] args) { int R_age = 15, A_age = 12; if ( R_age > A_age) Console.WriteLine("Ravi is elder to Amar"); if (R_age < A_age) Console.WriteLine("Ravi is younger than Amar"); if (R_age == A_age) Console.WriteLine("Ravi is of the same age as Amar"); } }
Output –
Note that the boolean expression in the first ‘if’ statement is given as a parameter evaluates to be true as Ravi’s age(15) is greater than Amar’s age(12). As only one if statement holds true, only the first block will be executed associated with the first if condition.
The second type of conditional statement provided by C# is the if-else statement. The second part of the code, which needs to execute if the condition holds false, can be kept inside the else block. The else block cannot exist independently. This means that the else statement must follow an if-statement or else if statement. An else statement can only be used once in an if-else statement chain.
The syntax of the if-else statement is –
if(a conditional statement or boolean expression) { // the block of code to be executed if the expression results into true } else { // executes when “if” exp is false }
For Example –
using System; class Ex4 { static void Main(string[] args) { int R_age = 12, A_age = 15; if ( R_age > A_age) Console.WriteLine("Ravi is elder to Amar"); else Console.WriteLine("Ravi and Amar are of the same age"); } }
Output:
By now, you must have noticed that the boolean expression in the first ‘if’ statement given as a parameter evaluates to be false as Ravi’s age(12) is less than Amar’s age(15). Like the if statement holds false, the second block, i.e. the code block associated with the else condition, will be executed.
The second type of conditional statement provided by C# is an else if statement. If the given conditions to be checked are more than one, then the else-if conditions come into the picture.
Consider –
using System; class Ex5 { static void Main(string[] args) { int R_age = 12, A_age = 15; if ( R_age > A_age) Console.WriteLine("Ravi is elder"); else if (R_age < A_age) Console.WriteLine("Ravi is younger"); else Console.WriteLine("Ravi is of the same age as Amar"); } }
Output:
Nested if the statement is an if statement within an if statement.
For Example –
using System; class Ex6 { static void Main(string[] args) { int R_age = 12, A_age = 15; if(R_age != A_age) //yields true as 12 is not equal to 15 { if( R_age < A_age) //enters inside this Console.WriteLine("Ravi is younger"); else Console.WriteLine("Ravi is elder"); } } }
Output:
The if-else or else-if statement evaluates the boolean expression and, based on the result, controls the flow of the program.
The above is the detailed content of C# if Statement. For more information, please follow other related articles on the PHP Chinese website!