C# – If-Else Statement

Programming in general often requires a decision or a branch within the code to account for how the code operates under different inputs or conditions. Within the C# programming language (and most programming languages for this matter), the simplest and sometimes the most useful way of creating a branch within your program is through an If-Else statement.

C# provides many decision making statements that help the flow of the C# program based on certain logical conditions. C# includes the following decision making statements.

  1. if statement
  2. if-else statement
  3. switch statement
  4. Ternary operator 😕

C# IF Statement

The C# if statement tests the condition. It is executed if condition is true.

 if (Boolean expression)
 {  
 Body of the conditional statement;
 } 

It includes: if-clause, Boolean expression and body of the conditional statement.
The Boolean expression can be a Boolean variable or Boolean logical expression. Boolean expressions cannot be integer (unlike other programming languages like C and C++).

The expression in the brackets which follows the keyword if must return the Boolean value true or false. If the expression is calculated to the value true, then the body of a conditional statement is executed. If the result is false, then the operators in the body will be skipped.

class PosNeg {  
static void Main() {
int i;    
for(i=-5; i <= 5; i++) {
    Console.Write("Testing " + i + ": ");
    if(i < 0) Console.WriteLine("negative");
    else Console.WriteLine("positive");  
      }  
    } 
 }

The output is shown here:

Testing -5: negative
Testing -4: negative
Testing -3: negative
Testing -2: negative
Testing -1: negative
Testing 0: positive
Testing 1: positive
Testing 2: positive
Testing 3: positive
Testing 4: positive
Testing 5: positive

In this example, if i is less than zero, then the target of the if is executed. Otherwise, the target of the else is executed. In no case are both executed.

Nested ifs

A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. The main thing to remember about nested ifs in C# is that an else clause always refers to the nearest if statement that is within the same block as the else and not already associated with an else. Here is an example:

 if(i == 10) 
 {  
 if(j < 20)
 a = b;  
 if(k > 100) 
 c = d;  
 else a = c; 
 // this else refers to if(k > 100) } 
 else a = d; // 
 this else refers to if(i == 10) 

As the comments indicate, the final else is not associated with if(j < 20) because it is not in the same block (even though it is the nearest if without an else). Rather, the final else is associated with if(i == 10). The inner else refers to if(k > 100) because it is the closest if within the same block.

C# “if-else”

In C#, as in most of the programming languages there is a conditional statement with else clause: the if-else statement. Its format is the following:

if (Boolean expression) {
  Body of the conditional statement;
 } else { 
 Body of the else statement;
 }

The format of the if-else structure consists of the reserved word if, Boolean expression, body of a conditional statement, reserved word else and else-body statement. The body of else-structure may consist of one or more operators, enclosed in curly brackets, same as the body of a conditional statement.

This statement works as follows: the expression in the brackets (a Boolean expression) is calculated. The calculation result must be Boolean – true or false. Depending on the result there are two possible outcomes. If the Boolean expression is calculated to true, the body of the conditional statement is executed and the else-statement is omitted and its operators do not execute. Otherwise, if the Boolean expression is calculated to false, the else-body is executed, the main body of the conditional statement is omitted and the operators in it are not executed.

static void Main() {
  int x = 2;
  if (x > 3)  {
  Console.WriteLine("x is greater than 3");
  }  else  {
  Console.WriteLine("x is not greater than 3"); 
  } 
 } 

C# if-else-if Ladder

A common programming construct that is based upon the nested if is the if-else-if ladder. It looks like this:

 if(condition) 
 statement; 
 else if(condition)
 statement; 
 else if(condition)
 statement;
 . . . 
 else 
 statement;

The conditional expressions are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else clause will be executed. The final else often acts as a default condition. That is, if all other conditional tests fail, then the last else clause is executed. If there is no final else and all other conditions are false, then no action will take place.

static void PrintPassOrFail(int score) {
     if (score > 100) // If score is greater than 100     
	 {         
	 Console.WriteLine("Error: score is greater than 100!");
     }     
	 else if (score < 0) // Else If score is less than 0 
	 {         
	 Console.WriteLine("Error: score is less than 0!");   
	 }     
	 else if (score >= 50) // Else if score is greater or equal to 50     
	 {         
	 Console.WriteLine("Pass!");   
	 }     
	 else // If none above, then score must be between 0 and 49
     {         
	 Console.WriteLine("Fail!");
     } 
}

0 0 vote
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x