How to use IF-ELSE statements in c# visual studio | c# tutorial



How to use IF-ELSE statements in c# visual studio | c# tutorial

How to use IF-ELSE statements in c# visual studio | c# tutorial

In this C# tutorial you will learn about conditional statements which are used to run specific blocks of code, if certain conditions are met. Conditional Statements are used constantly in C# applications.
C# Conditions and If Statements
C# supports the usual logical conditions from mathematics
You can use these conditions to perform different actions for different decisions.

C# has the following conditional statements:
Use if to specify a block of code to be executed, if a specified condition is true
Use else to specify a block of code to be executed, if the same condition is false
Use else if to specify a new condition to test, if the first condition is false
Use switch to specify many alternative blocks of code to be executed

C# – if, else if, else Statements
C# provides many decision-making statements that help the flow of the C# program based on certain logical conditions. Here, you will learn about if, else if, else, and nested if else statements to control the flow based on the conditions.

C# includes the following flavors of if statements:
if statement
else-if statement
else statement
C# if Statement
The if statement contains a boolean condition followed by a single or multi-line code block to be executed. At runtime, if a boolean condition evaluates to true, then the code block will be executed

C# if, if…else, if…else if and Nested if Statement
In this video, we will learn how to use if, if…else, if…else if statement in C# to control the flow of our program’s execution.

Testing a condition is inevitable in programming. We will often face situations where we need to test conditions (whether it is true or false) to control the flow of program. These conditions may be affected by user’s input, time factor, current environment where the program is running, etc.

In this video, we’ll learn to test conditions using if statement in C#.
C# if (if-then) Statement
C# if-then statement will execute a block of code if the given condition is true. The syntax of if-then statement in C#
The boolean-expression will return either true or false.
If the boolean-expression returns true, the statements inside the body of if ( inside {…} ) will be executed.
If the boolean-expression returns false, the statements inside the body of if will be ignored.
The value of number is initialized to 2. So the expression number 5 is evaluated to true. Hence, the code inside the if block are executed. The code after the if statement will always be executed irrespective to the expression.
Now, change the value of number to something greater than 5, say 10.

C# if…else (if-then-else) Statement
The if statement in C# may have an optional else statement. The block of code inside the else statement will be executed if the expression is evaluated to false.
The syntax of if…else statement

Here, the value of number is initialized to 12. So the expression number 5 is evaluated to false. Hence, the code inside the else block are executed. The code after the if..else statement will always be executed irrespective to the expression.
Now, change the value of number to something less than 5, say 2

C# if…else if (if-then-else if) Statement
When we have only one condition to test, if-then and if-then-else statement works fine. But what if we have a multiple condition to test and execute one of the many block of code.
For such case, we can use if..else if statement in C#. The syntax for if…else if statement

Conditional Statements In C# | C# IF ELSE Statement | How to use if else Statements in c#
Conditional Statements In C# | C# IF ELSE Statement | How to use if else Statements in c#
Conditional Statements In C# | C# IF ELSE Statement | How to use if else Statements in c#
Conditional Statements In C# | C# IF ELSE Statement | How to use if else Statements in c#

Using if else statements in c#,how to use if else statements in c#,C# if else tutorial,c# if else statement,c# if statement,if else statements in c#,C# conditional statements,C# if statements,else if statements in C#,conditional statements in C#,conditional statement in C#,if else,else if,else,C# if else example,C# else if Example,C# else if Tutorial,c# if else statements,If-else statements in c#,C# – If else Statement

Comments are closed.