0% found this document useful (0 votes)
28 views

Csharp Control Structures

The document discusses conditional statements in C#, including if, else if, else, and switch statements to control program flow based on evaluating conditions. It provides examples of using these conditional statements to check conditions and execute different blocks of code accordingly. Key concepts covered are Boolean expressions, break and default keywords in switch statements, and nesting conditional statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Csharp Control Structures

The document discusses conditional statements in C#, including if, else if, else, and switch statements to control program flow based on evaluating conditions. It provides examples of using these conditional statements to check conditions and execute different blocks of code accordingly. Key concepts covered are Boolean expressions, break and default keywords in switch statements, and nesting conditional statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

C# CONDITIONAL

STATEMENT
OBJECTIVES

At the end of this unit, students will be able to:


• Apply decision making using the conditional statements
• Identify the differences and uses of conditional statements
• Evaluate Boolean expressions.
• Write programs using conditional statements
INTRODUCTION:
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,
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# is:
if (expression)
{
// statements executed if expression is true
}
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
will be ignored.
IF STATEMENT EXAMPLE

When we run the program:


EXAMPLE
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.
When we run the program the output will be:

The expression number < 5 will return false, hence the code inside if block won't be
executed.
EXAMPLE
OUTPUT
IF…ELSE STATEMENT
Use the if..else statement to specify a block of code to be executed if the condition is False.
Syntax:
if (condition)
{
// block of code to be executed if the condition is True
}
else
{
// block of code to be executed if the condition is False
}

If the boolean-expression returns true, the first statements inside the body of if ( inside {...} )
will be executed.
If the boolean-expression returns false, the second statement inside the body will be executed.
IF…ELSE EXAMPLE

When we run the program


IF…ELSE EXAMPLE
• 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.


When we run the program the output will be:

• The expression number < 5 will return true, hence the code inside if
block will be executed.
IF…ELSE EXAMPLE
OUTPUT

Example explained
In the example above, time (20) is greater than 18, so the condition is False.
Because of this, we move on to the else condition and print to the screen "Good
evening". If the time was less than 18, the program would print "Good day".
THE ELSE IF STATEMENT
• Use the else if statement to specify a new condition if the first condition is
False.
Syntax
if (condition1)
{
// block of code to be executed if condition1 is True
}
else if (condition2)
{
// block of code to be executed if the condition1 is false and condition2 is True
}
else
{
// block of code to be executed if the condition1 is false and condition2 is False
}
IF…ELSE IF…EXAMPLE
OUTPUT

Example explained
In the example, time (22) is greater than 10, so
the first condition is False. The next condition,
in the else if statement, is also False, so we
move on to the else condition
since condition1 and condition2 is both False -
and print to the screen "Good evening".
However, if the time was 14, our program
would print "Good day."
NESTED IF...ELSE STATEMENT
An if...else statement can exist within another if...else statement. Such
statements are called nested if...else statement.
The general structure of nested if…else statement is:
NESTED IF..ELSE EXAMPLE
When we run the program, the
output will be:
C# SWITCH STATEMENTS

• Use the switch statement to select one of many code blocks to be executed.
Syntax:
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
break;
}

This is how it works:


The switch expression is evaluated once
The value of the expression is compared with the values of each case
If there is a match, the associated block of code is executed
CONTINUATION…
The break Keyword
• When C# reaches a break keyword, it breaks out of the switch block.
• This will stop the execution of more code and case testing inside the
block.
• When a match is found, and the job is done, it's time for a break.
There is no need for more testing.
• A break can save a lot of execution time because it "ignores" the
execution of all the rest of the code in the switch block.
The default Keyword
• The default keyword is optional and specifies some code to run if
there is no case match:
SWITCH EXAMPLE
OUTPUT
REFERENCES

• https://www.programiz.com/csharp-programming/if-else-statement
• https://introprogramming.info/english-intro-csharp-book/read-online/
chapter-5-conditional-statements/
• https://www.w3schools.com/cs/cs_conditions.asp

You might also like