C#

C# if else Statement

subscribe to codebuns.com

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.

An 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 elseif else block statement

if-else if-else flowchart

if elseif else flow chart

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 elseif else single statement

if 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 flowchart

if statement flow chart

Example:

if with a single statement

Run Demo

OUTPUT

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

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.

if condition

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

if…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 statement flow chart

Example:

If-else statement with Else execute

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

if…else if…else 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

if-elseif-else vertical flow chart

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

Example:

if-elseif-else

Example:

if-elseif-else example 2

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.

nested if statement

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# Reference | Microsoft Docs

Tagged in :