C#. Lecture No. 2
C#. Lecture No. 2
LECTURE NO. 2
Prepared by
Dr.Arkan
Dr. Yousif A. Hamad
Khaleel
3ed Class
C# Decision Making
• if statement
• if...else statement
• nested if statements
• switch statement
• nested switch statements
• C# Decision Making
Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the
program, along with a statement or statements to be executed if the condition is determined to be true, and optionally,
other statements to be executed if the condition is determined to be false. Following is the general from of a typical
• if statement
An if statement consists of a Boolean expression followed by one or more statements.
The syntax of an if statement in C# is:
If the Boolean expression evaluates to true, then the block of code inside the if statement will be
executed. If Boolean expression evaluates to false, then the first set of code after the end of the if
When the above code is compiled and executed, it produces the following result:
Example 1: write a C# program to read only two numbers and print the largest value of it
Example 2: write a C# program to read only two numbers and print the even value of it
Example 3: write a C# program to read only two numbers and print the positive value of it
• if...else statement
An if statement can be followed by an optional else statement, which executes when the Boolean
If the Boolean expression evaluates to true, then the if block of code will be executed otherwise
When the above code is compiled and executed, it produces the following result:
Example 4: Write a C# program to read a number and check if its positive or negative.
static void Main(string[] args)
{
int number1;
Console.WriteLine("Enter the value of number1");
Console.Write("The value of number ");
number1 = int.Parse(Console.ReadLine());
if (number1 >= 0)
Console.WriteLine("Number1 is positive");
else
Console.WriteLine("Number1 is Negative");
Console.ReadLine();}
Example 5: Write a C# program to read a number and check if its odd print odd and add it to 4
otherwise print even and decrement it by 3.
Example 6: write a C# program to read a student degree, and check if its degree greater than or
equal to 50, then print pass, otherwise print fail.
• if...else if...else statement
An if statement can be followed by an optional else if...else statement, which is very useful to test
various conditions using single if...else if statement.
When using if, else if, else statements there are few points to keep in mind.
• An if can have zero or one else's and it must come after any else ifs.
• An if can have zero to many else if's and they must come before the else.
• Once an else if succeeds, none of the remaining else if's or else's will be tested.
The syntax of an if...else if...else statement in C# is:
Example
When the above code is compiled and executed, it produces the following result:
Example 7: write a C# program to read a number, and print the day of the week, if the number is
greater the 7 print Error.
Example 8: write a C# program to read a number, and print the month of the year, if the number is
greater the 12 print Error.
Example 9: write a C# program to compute the value of z according to the following equations:
𝑥+1 ∶𝑥 >0
Z={ 𝑥 ∗= 2 ∶ 𝑥 < 0
++𝑥 ∶𝑥 =0
• nested if statements
It is always legal in C# to nest if-else statements, which means you can use one if or else if
statement inside another if or else if statement(s). The syntax for a nested if statement is as
follows:
You can nest else if...else in the similar way as you have nested if statement.
Example 10: Write C# program to find a largest value among three numbers
Example 11: Write C# program to read two integer numbers and read the operation to perform on
these numbers using switch.
Example 12: write a C# program to read a number, and print the day of the week, if the number is
greater the 7 print Error.
• switch statement
A switch statement allows a variable to be tested for equality against a list of
values.
o When a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.
o Not every case needs to contain a break. If no break appears, the flow of control
will fall through to subsequent cases until a break is reached.
o 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. No break is needed in the default case.
Example
When the above code is compiled and executed, it produces the following result:
Example 13: write a C# program to read a number, and print the day of the week, if the number is
greater the 7 print Error using switch.
• nested switch statements
It is possible to have a switch as part of the statement sequence of an outer switch. Even if the
case constants of the inner and outer switch contain common values, no conflicts will arise. The
Example
When the above code is compiled and executed, it produces the following result:
Example 14: write a C# program to read a character, and print the day of the week, otherwise print
Error using switch.
Example 15: write a C# program to read a character, and print the month of the year, otherwise
print Error using switch.