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

Chapter 3

This document provides an overview of conditional statements in C#, including if/else statements and switch statements. It defines conditional statements as statements that can execute different code based on if a condition is true or false. It explains the syntax of if/else statements, including that an if statement can have an optional else if multiple conditions need to be checked, and an optional else that executes if all conditions are false. It also defines switch statements as selecting a code block to execute based on a match with different case values, providing an alternative to multiple if/else statements. It provides examples of the syntax and flow of if/else and switch statements in C#.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Chapter 3

This document provides an overview of conditional statements in C#, including if/else statements and switch statements. It defines conditional statements as statements that can execute different code based on if a condition is true or false. It explains the syntax of if/else statements, including that an if statement can have an optional else if multiple conditions need to be checked, and an optional else that executes if all conditions are false. It also defines switch statements as selecting a code block to execute based on a match with different case values, providing an alternative to multiple if/else statements. It provides examples of the syntax and flow of if/else and switch statements in C#.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

CHAPTER 3:

CONDITIONAL STATEMENTS IN C#
(MAKING DECISIONS)
Prepared by: Joshua M. Relatorres
OBJECTIVES:

• Understand the use of logical and relational operators (review)


• Learn and Apply conditional statements
• Evaluate conditional expression
• Understand logical operators (review)
TRUTH TABLE
TRUTH TABLE

A mathematical table used in logic—specifically in connection with


Boolean algebra, boolean functions, and propositional calculus—
which sets out the functional values of logical expressions on each of
their functional arguments, that is, for each combination of values
taken by their logical variables.
TRUTH TABLE
CONDITIONAL STATEMENT
CONDITIONAL STATEMENT

• A statement that can be executed based on a


condition.
• It perform different computations or actions
depending on whether a programmer-specified
Boolean condition evaluates to true or false.
• The statement is often a block of code.
CONDITIONAL STATEMENT

• An IF statement can be followed by an optional ELSE statement,


which executes when the boolean expression is false.
• If the boolean expression evaluates to true, then the if block of
code is executed, otherwise else block of code is executed.
CONDITIONAL STATEMENT FLOW DIAGRAM
CONDITIONAL STATEMENT SYNTAX
CONDITIONAL STATEMENT
CONDITIONAL STATEMENT RULES

An IF statement can be followed by an optional ELSE IF...ELSE statement,


which is very useful for testing various conditions using a single IF...ELSE IF
statement.
When using IF, ELSE IF, ELSE statements there are a few points to keep in
mind.
• An IF can have zero or one ELSE and it must come after any ELSE IFs.
• An IF can have zero to many ELSE IFs and they must come before the ELSE.
• Once an ELSE IF succeeds, none of the remaining ELSE IFs or ELSEs will be tested.
CONDITIONAL STATEMENT
NESTED IF SYSTAX
SWITCH STATEMENT
SWITCH STATEMENT

• A selection statement that chooses a single switch section to


execute from a list of candidates based on a pattern match with
the match expression.
• Often used as an alternative to an if-else construct if a single
expression is tested against three or more conditions.
SWITCH RULES

1. The expression used in a switch statement must have an integral or enumerated type or be of a class
type in which the class has a single conversion function to an integral or enumerated type.
2. You can have any number of case statements within a switch. Each case is followed by the value to be
compared to and a colon.
3. The constant expression for a case must be the same data type as the variable in the switch, and it
must be a constant or a literal.
4. When the variable being switched on is equal to a case, the statements following that case will execute
until a break statement is reached.
5. When a break statement is reached, the switch terminates, and the flow of control jumps to the next
line following the switch statement.
6. Not every case needs to contain a break. If no break appears, then it will raise a compile-time error.
7. A switch statement can have an optional default case, which must appear at the end of the switch. The
default case can be used for performing a task when none of the cases is true.
SWITCH FLOW DIAGRAM
SWITCH STATEMENTS SYNTAX
SWITCH SAMPLE CODE
THANKS YOU!

You might also like