0% found this document useful (0 votes)
11 views23 pages

Session 5 - Conditional Statements 26102023 033207pm

The document discusses different types of conditional statements including IF, IF-ELSE, nested IF-ELSE, AND operator usage with IF, and switch-case statements. Syntax examples are provided for each in C# language.

Uploaded by

ayesha naz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views23 pages

Session 5 - Conditional Statements 26102023 033207pm

The document discusses different types of conditional statements including IF, IF-ELSE, nested IF-ELSE, AND operator usage with IF, and switch-case statements. Syntax examples are provided for each in C# language.

Uploaded by

ayesha naz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Conditional Statements

SESSION 5
Explain IF statement
Explain IF…ELSE selection construct
Explain multiple selection statements
Explain nested IF…ELSE statements
Explain case construct
A programmer may come
Start Step Step
across a condition in the
program, where the path of
Step Step Step
execution can branch into two
or more options.
Step Step Stop

Such constructs are referred Step

to as programming, selection,
conditional, or branching Step
constructs. Step Step
 The IF construct is a basic selection construct.

 Consider an example where the customer is given a


discount if purchases of over $100 are made.

 Each time a customer is billed, a part of the code


has to check to see if the bill amount exceeds $100.

 If it does exceed the amount, then it must deduct


10% of the total amount, otherwise nothing must
be deducted.
The pseudocode for the scenario will be as
follows:
IF customer purchases items worth more than $100
Give 10% discount
The general form of an IF statement or construct
is as follows:

IF condition

Statements Body of the IF Construct


END IF
The example uses the IF construct to find
whether a number is even or not.

BEGIN
INPUT number
rem = number MOD 2
IF rem=0
Display “Number is even”
END IF
END
A flowchart for the
pseudocode is shown in
the figure.
The syntax for the IF statement in C# language
is as follows:

if (condition)
{
Statements;
}
 The example shows the pseudocode that would be written in C#

public static void main (String[] args)


{
int number, rem;
Console.WriteLine(“Please enter a number: ”);
Number = Convert.ToInt32(Console.ReadLine());

rem=number%2;
if(rem==0)
{
Console.WriteLine(“Even Number”);
}
}
 The IF…ELSE statement enables a programmer to make a
single comparison, and then execute the steps depending
on whether the result of the comparison is true or false.
 The general form of the IF…ELSE statement is as follows:

IF condition
Statement set1
ELSE
Statement set2
END IF
 The syntax for the IF…ELSE construct in C#
language is given as follows:

if(condition)
{
statement set1;
}
else
{
statement set2;
}
 A more efficient code for the even number using the
IF…ELSE statement is shown in the following
example.
BEGIN
INPUT number
rem=number MOD 2
IF rem=0
DISPLAY “Even Number”
ELSE
DISPLAY “Odd Number”
END IF
END
 The flowchart for the pseudocode is shown:
 The AND statement can be used in conjunction with
the IF statement for more than one condition.

 To classify a supplier as a Most Valuable Supplier


(MVS), the organization must check that the supplier
has been with them for the last 10 years.

 And has done a total business of more than $500000.

 These two conditions must be satisfied to consider a


supplier as a MVS.
 The example shows the pseudocode for this scenario.

BEGIN
INPUT YearsWithUs
INPUT BizDone
IF YearsWithUs >= 10 AND BizDone >= 500000
DISPLAY “Classified as an MVS”
ELSE
DISPLAY “A little more effort required”
END IF
END
The example shows the pseudocode that
would be written in C#.
/* C# snippet depicting the AND operator in IF */
if(YearsWithUs >= 10 && BizDone >= 500000)
{
Console.WriteLine(“Classified as an MVS”);
}
else
{
Console.WriteLine(“A little more effort required”);
}
Another way to combine two conditions
without using the AND operator, is by using
nested IF…ELSE statements.

A nested IF is an IF statement written inside


another IF statement.
 Consider the earlier example to recognize the MVS status of a supplier
rewritten using nested IF.
BEGIN
INPUT YearsWithUS
INPUT BizDone
IF YearsWithUs >= 10
IF BizDone >= 500000
DISPLAY “Classified as an MVS”
ELSE
DISPLAY “A little more effort required”
END IF
ELSE
DISPLAY “A little more effort required”
END IF
END
 The flowchart for the pseudocode is shown in the figure.
 The DO CASE…END CASE construct
is used when a variable is to be
successively compared against
different values.
 The DO CASE is known as ‘Switch
Case’ in C#.
 The syntax in C will be as follows:

switch (expression)
{
case const-expr:
statement set;
break;
case const-expr:
statement set;
break;
default
statement set;
}
A Switch Statement
switch (ch) {
case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'u': case 'U':
Console.WriteLine(ch + " is a vowel" );
break;
default:
Console.WriteLine(ch + " is not a vowel" );
}

You might also like