C#

C# if else Statement

The objective of this article and youtube video is to familiarize you with the C# if else Statement. How to use different control flow statements like if, if else, if else-if, if else-if else, and finally if else shorthand. Another common arrangement is to have multiple if conditions one inside another called nested if condition.

You will learn

if Statements

The C# if else statement is one of the most commonly used control flow statement. With if statements, you can tell the computer to make a choice by evaluating a Boolean logical expression (true or false) called condition. It allows you to tell the computer whether to run the code inside the block based on the condition or set of conditions. Each Boolean expression can be independent of the others and can include any of the comparison and logical operators.

if else statement – C# tutorial for beginners (2019)

C# if statement can be extended by any number of else if clauses to test more conditions. Each additional condition will only be tested if all previous conditions are false. Finally, you can use the else statement at the end to execute some code if none of the above conditions are true.

Here’s the complete structure which lets you use multiple conditions:

if else-if else Syntax with a Block of Statements

if else-if else flowchart

Here, condition is some Boolean expression that evaluates to either true or false, and the statements inside the code block {…} should be executed if a condition is true.

if else-if else Syntax with a Single Statement

if Condition with a Single Statement

When you only need to execute code for a true condition, use an if statement. It is better to use parentheses for each clause. However, if a clause only contains a single statement, then you can omit the parentheses as in the following example:

if Condition flowchart

if Condition Example

Run Demo

OUTPUT

Condition is true.
Condition is true on a single line.

if Condition with a Block of Statements

Brackets ({ }) would be required for more than one statement. You can code any number of statements between the braces. Coding the braces makes it easy to identify the statements for each clause which makes it easier for you to add more statements later during development.

Run Demo

The value of the number variable is 8. So the expression (number > 7) is evaluated to true because 8 is greater than 7. Hence, the statements inside the body of the if block are executed. If you change the value of the number variable from 8 to 6, then the expression will return false, which will ignore the execution of the if body.

OUTPUT

8 is greater than 7
Condition is true

else Statement

Optionally, you can also have an else clause. Whenever the Boolean condition of the if statement is false, the else clause executes as shown in the following code block. Of course, you can use multiple conditions by adding more else if clauses in between if and else.

if else flowchart

if else Condition Example

Run Demo

In the example above, the value of the number variable is 6. So the expression (number > 7) is evaluated to false because 6 is less than 7. Hence, the code inside else block is executed. One way of visualizing an if statement is a flow chart. This shows the above if statement in graphical form.

OUTPUT

6 is less than 7
Condition is false

else if Statement

The else if statement follows directly after the if statement to let you use multiple conditions for testing. Each additional condition will only be evaluated if all previous conditions are false.

if else-if else flowchart

Note: Order of tests is important if you are using an if elseif construct.

if else-if else Condition Example

if else-if else Condition Example

Run Demo

The if statement tests the value of the gender entered. If the gender equals the value Female, everything inside the pair of braces ({ }) is carried out and the rest of if statement will be skipped. On the other hand, if none of the condition is true, then the else block is executed by default.

In the above example, the expression (gender == “Female“) is evaluated to true, which is the condition of the else if block, and the statement following is executed.

Notice that a test for equality uses the == operator (not the = operator, which is used for assignment).

OUTPUT

Only females are allowed.

Nested if Statement

Another common arrangement is to have multiple if statements one inside another to test multiple conditions. It is called nesting. Nesting helps you create complex programs that rely on chained conditions. There is no limit on the use of nesting that can be achieved with these structures.

Run Demo

In the above example, the value of the gender variable is equal to Male. So the expression (gender == “Male“) is evaluated to true, and the program will execute the statement or sequence of statements within the curly braces that follow. Next, you will see that the second if statement is completely contained within the first. This is called nesting. Once the nested if is encountered, the statement (age >= 18) is checked and evaluates to true because 18 is less than or equal to 18. It then prints “I am 18+. “, followed by “Only males are allowed.”

OUTPUT

Only males are allowed.
I am 18+.

C# if else shorthand

C# provides a conditional operator “?:”, which is sometimes called C# ternary or question operator. This operator is a shorthand method of writing a simple single line if else statement.

I have discussed this in detail. Please read C# if else shorthand

Useful Articles

C# Reference

Sending
User Rating 5 (1 vote)
Tagged in :

Add Comment

Click here to post a comment

Send this to a friend