You are on page 1of 27

STD511 C# Programming

Control Structures: Selection


Boolean Type
The boolean Type in
a program you need to compare
two values, such as whether i is greater than j. C#
provides six comparison operators (also known as
relational operators) that can be used to compare
two values. The result of the comparison is a
Boolean value: true or false.

boolean v;

boolean b = (1 > 2);

2
Comparison Operators
Java Mathematics Name Example Result
Operator Symbol (radius is 5)
< < less than < 0
radius false
<= ≤ less than or equal to <= 0
radius false
> > greater than radius > 0 true
>= ≥ greater than or equal to radius >= 0 true
== = equal to radius == 0 false
!= ≠ not equal to radius != 0 true

3
One-way if
Statements if (radius >= 0)
{
area = radius * radius * PI;
if (boolean-expression) Console.WriteLine("The
statement(s); area"
{}
+ " for the circle of radius "
} + radius + " is " + area);

false false
Boolean (radius >= 0)
Expression

true true

Statement(s) area = radius * radius * PI;


Console.WriteLine("The area for the circle of "
+ "radius " + radius + " is " + area);

(A) (B)
4
Note

if (i > 0) {
if i > 0
{
Console.WriteLine("i is positive"); Console.WriteLine("i is positive");
} }
(b) Correct
(a) Wrong

if (i > 0) { if (i > 0)
Console.WriteLine("i is positive"); Equivalent Console.WriteLine("i is positive");
}

(a) (b)

5
Examples

1)Write a program that prompts the user to enter an integer. If the


number is a multiple of 5, print Hi Five. If the number is
divisible by 2, print Hi Even.

2)Write a program that prompts the user to enter two numbers x


and y and finds x/y if the second number is non zero.

6
Common
Errors
Adding a semicolon at the end of an if clause is a common
mistake.
if (radius >= 0); Wrong
{
area = radius*radius*PI;
Console.WriteLine(
"The area for the circle of radius "
+ radius + " is " + area);
}

7
Note
if (even == true) Equivalent if (even)
Console.WriteLine( Console.WriteLine(
"It is even."); "It is even.");

(a) (b)

8
The Two-way if Statement
if (boolean-expression)
{
statement(s)-for-the-true-case;
}
else
{
statement(s)-for-the-false-case;
}

true false
Boolean
Expression

Statement(s) for the true case Statement(s) for the false case

9
if-else Example
if (radius >= 0)
{ area = radius * * 3.14159;
radius
Console.WriteLine("The area for the “
+ “circle of + radius +
radius " " is " +
} area);
else

{ Console.WriteLine("Negative input");
}

10
Exercise
• Draw a flowchart
s and write a program
to read an integer and decides weather
it is even or odd.

• Draw a flowchart and write a program


to read an integer and decides weather
it is positive or negative.
Multi-Way if
Statements
if (score >= 90.0) if (score >= 90.0)
grade = 'A'; grade = 'A';
else else if (score >= 80.0)
if (score >= 80.0) Equivalent grade = 'B';
grade = 'B'; else if (score >= 70.0)
else grade = 'C';
if (score >= else if (score >= 60.0)
70.0) grade = grade = 'D';
'C'; else
else if (score >= 60.0) grade = 'F';
grade = 'D';
else
grade = 'F';

12
Multi-Way if Statements

false
score >= 90
false
true score >=
80 fals
grade = 'A' true score >= e
70 fals
grade = rue score >= e
'B' 60
grade = true
'C'
grade = 'D'

grade =
'F'
Multi-Way if-else Statements

1) Draw the flowchart for the previous multi-way


if statement.

2) Write a C# program to read the name and score of a


student and prints the name and the grade.

3) Write a C# program to read the name, salary and number


of service years of an employee and increase the salary as
follows:
Service years Salary increase
less than 10 20%
10-20 30%
more than 20 40% 14
Problem: Body Mass Index
Body Mass Index (BMI) is a measure of health on
weight. It can be calculated by taking your weight in
kilograms and dividing by the square of your height
in meters. The interpretation of BMI for people 16
years or older is as follows:
BMI Interpretation
  Below 18.5 Underweight
18.5-24.9 Normal
25.0-29.9 Overweight
Above 30.0 Obese
• Write a program to read the weight and height and
calculate the BMI and prints the BMI
interpretation.
Logical Operators
Operator Name
! not
&& and
|| or
^ exclusive or

16
Example

A program that checks whether a number is divisible by 2 and 3,


whether a number is divisible by 2 or 3, and whether a number
is divisible by 2 or 3 but not both:

17
Example
Console.WriteLine("Is " + number + " divisible by 2 and 3? "
+
((number % 2 == 0) && (number % 3 == 0)));

Console.WriteLine("Is " + number + " divisible by 2 or 3? "


+ ((number % 2 == 0) || (number % 3 == 0)));

Console.WriteLine("Is " + number +


" divisible by 2 or 3, but not both? " +
((number % 2 == 0) ^ (number % 3 == 0)));
18
Problem: Determining Leap Year?

This program first prompts the user to enter


a year as an int value and checks if it is a
leap year.
A year is a leap year if it is divisible by 4
but not by 100, or it is divisible by 400.

19
switch Statement
switch (switch-expression)
{
case value1: statement(s)1;
break;
case value2: statement(s)2;
break;

case valueN: statement(s)N;
break;
default: statement(s)-for-
default;
}

20
switch Statement

sta tu s is 0
C o m p ut e ta x fo r sin gle file rs b rea k

sta tu s is 1
C o m p ut e ta x fo r m a r ried join t ly o r q u a lifyi n g w i d o w (e r) b rea k

sta tu s is 2
C o m p ut e ta x fo r m a r ried fili n g sep a ra te ly b re a k

sta tu s is 3
C o m p ut e ta x fo r h e a d o f h o u se h old b rea k

d e fa u lt
D efa u lt a c tion s
switch
Statement
switch (status)
{
case 0: compute taxes for single
filers; break;
case 1: compute taxes for married file
jointly; break;
case 2: compute taxes for married file
separately; break;
case 3: compute taxes for head of
household; break;
default: Console.WriteLine("Errors: invalid
status");
System.exit(1); 22
switch statement -
Examples
1) Draw a flowchart and write a C# program to read the
name and score of a student and prints the name and
the grade. Use switch.

2) Write a C# program to read the name, salary and


accommodation class of an employee and calculates
the net salary after deducting the accommodation cost
Acc. class
follows: cost
A $500
B $400
C $300

23
Conditional Operator

if (x > 0)
y = 1;
else
y = -1;

is
equivale
nt to

y = (x >
0) ? 1 :
-1;
24

(boolea
Conditional Operator
if (num % 2 == 0)
Console.WriteLine(num + “is even”);
else
Console.WriteLine(num + “is odd”);

Console.WriteLine( (n
um % 2 == 0)? num + “is even” :
num + “is odd”);

25
Problem: A Simple Math Learning
Tool

This example creates a program to let a first grader practice


additions. The program randomly generates two single-digit
integers number1 and number2 and displays a question such as
“What is 7 + 9?” to the student. After the student types the answer,
the program displays a message to indicate whether the answer is
true or false.

26
References
• C# Programming for Absolute Beginners;
Radek Vystavel.
• https://www.geeksforgeeks.org/csharp-progr
amming-language/
• https://docs.microsoft.com/en-us/dotnet/csh
arp/

You might also like