0% found this document useful (0 votes)
12 views11 pages

Selection Construct Csharp

Uploaded by

Madhuri Patel
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)
12 views11 pages

Selection Construct Csharp

Uploaded by

Madhuri Patel
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/ 11

SELECTION

STATEMENTS IN C#
SELECTION CONSTRUCT OR
STATEMENT
• Is a programming construct supported by C# that controls the flow of a
program.
• Executes a particular block of statements based on a boolean condition,
which is an expression returning true or false.
• Is referred to as a decision-making construct.
• Allow you to take logical decisions about executing different blocks of a
program to achieve the required logical output.
SELECTION CONSTRUCT OR
STATEMENT
• C# supports the following decision-making constructs:
• if…else
• if…else…if
• Nested if
• switch…case
• Nested Switch Case
THE IF STATEMENT

• The if statement allows you to execute a block of statements after


evaluating the specified logical condition.
• The if statement starts with the if keyword and is followed by the
condition.
• If the condition evaluates to true, the block of statements following the
if statement is executed.
• If the condition evaluates to false, the block of statements following the
if statement is ignored and the statement after the block is executed.
THE IF STATEMENT
• The following figure displays an example of the if construct:
THE IF-ELSE STATEMENT

• In some situations, it is required to define an action for a false condition


by using an if...else construct.
• The if...else construct starts with the if block followed by an else block
and the else block starts with the else keyword followed by a block of
statements.
• If the condition specified in the if statement evaluates to false, the
statements in the else block are executed.
THE IF-ELSE-IF STATEMENT
• The if…else…if construct allows you to check multiple conditions to
execute a different block of code for each condition.
• It is also referred to as if-else–if ladder.
• The construct starts with the if statement followed by multiple else if
statements followed by an optional else block.
• The conditions specified in the if…else…if construct are evaluated
sequentially.
• The execution starts from the if statement. If a condition evaluates to
false, the condition specified in the following else…if statement is
evaluated.
NESTED IF CONSTRUCT
• The nested if construct consists of multiple if statements.
• The nested if construct starts with the if statement, which is called the
outer if statement, and contains multiple if statements, which are called
inner if statements.
• In the nested if construct, the outer if condition controls the execution
of the inner if statements. The compiler executes the inner if
statements only if the condition in the outer if statement is true.
• In addition, each inner if statement is executed only if the condition in
its previous inner if statement is true.
SWITCH…CASE CONSTRUCT

• A program is difficult to comprehend when there are too many if


statements representing multiple selection constructs.
• To avoid using multiple if statements, in certain cases, the switch…case
approach can be used as an alternative.
• The switch…case statement is used when a variable needs to be
compared against different values.
SWITCH…CASE CONSTRUCT

• In C#, the flow of execution from one case statement is not


allowed to continue to the next case statement and is referred to
as the ‘no-fall-through’ rule of C#.
NESTED-SWITCH…CASE
CONSTRUCT

• C# allows the switch…case construct to be nested. That is, a case


block of a switch…case construct can contain another switch…case
construct.
• Also, the case constants of the inner switch…case construct can
have values that are identical to the case constants of the outer
construct.

You might also like