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

C#. Lecture No. 2

This document is a lecture on decision-making structures in C# programming, covering various types of statements such as if, if...else, nested if, switch, and nested switch statements. It provides syntax explanations and multiple examples for each type of statement to illustrate their usage in determining program flow based on conditions. The document is intended for students learning C# in their third class.

Uploaded by

yousif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C#. Lecture No. 2

This document is a lecture on decision-making structures in C# programming, covering various types of statements such as if, if...else, nested if, switch, and nested switch statements. It provides syntax explanations and multiple examples for each type of statement to illustrate their usage in determining program flow based on conditions. The document is intended for students learning C# in their third class.

Uploaded by

yousif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

C#

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

decision-making structure found in most of the programming languages:

• 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

statement (after the closing curly brace) will be executed.


Example:

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

static void Main(string[] args)


{
int number1, number2;
Console.WriteLine(" Enter the value of number1 and number2");
Console.Write("The value of number1: ");
number1 = int.Parse(Console.ReadLine());
Console.Write("The value of number2: ");
number2 = int.Parse(Console.ReadLine());
if (number1 > number2)
Console.WriteLine("The largest value is number1");
Console.ReadLine();}

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

expression is false. The syntax of an if...else statement in C# is:

If the Boolean expression evaluates to true, then the if block of code will be executed otherwise

else block of code will be executed. Flow Diagram:


Example

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.

static void Main(string[] args)


{
int number;
Console.WriteLine("Enter The Number of the day\n");
number = int.Parse(Console.ReadLine());
if (number == 1) Console.WriteLine("Saturday");
else if (number == 2) Console.WriteLine("Sunday");
else if (number == 3) Console.WriteLine("Monday");
else if (number == 4) Console.WriteLine("Tuesday");
else if (number == 5) Console.WriteLine("Wednesday");
else if (number == 6) Console.WriteLine("Thursday");
else if (number == 7) Console.WriteLine("Friday");
else Console.WriteLine("Invalid day number");
Console.ReadLine(); }

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

static void Main(string[] args)


{
int number1, number2, number3;
Console.WriteLine("Enter The value of Number1");
number1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter The value of Number2");
number2 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter The value of Number3");
number3 = int.Parse(Console.ReadLine());
if (number1 > number2)
{
if (number1 > number3)
Console.WriteLine("The largest one is Number1:" + number1);
else
Console.WriteLine("The largest one is Number3:" + number3);
}
else if (number2 > number3)
Console.WriteLine("The largest one is Number2:" + number2);
else
Console.WriteLine("The largest one is Number3:" + number3);
Console.ReadLine();}

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

syntax for a nested switch statement is as follows:

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.

static void Main(string[] args)


{
char c1, c2;
Console.WriteLine("Enter the first character of the day name in capital");
c1 = char.Parse(Console.ReadLine());
switch (c1)
{
case 'S':
Console.WriteLine("Enter the secund character of the day name");
c2 = char.Parse(Console.ReadLine());
switch (c2)
{
case 'U': Console.WriteLine("Sunday"); break;
case 'A': Console.WriteLine("Saterday"); break;
default: Console.WriteLine("Error"); break;
}
break;
case 'M': Console.WriteLine("Monday"); break;
case 'T':
Console.WriteLine("Enter the secund character of the day name");
c2 = char.Parse(Console.ReadLine());
switch (c2)
{
case 'U': Console.WriteLine("Tuesday"); break;
case 'H': Console.WriteLine("Thursday"); break;
default: Console.WriteLine("Error"); break;
}
break;
case 'W': Console.WriteLine("Wednesday"); break;
case 'F': Console.WriteLine("Friday"); break;
default: Console.WriteLine("Error"); break;
}
Console.ReadLine();}

Example 15: write a C# program to read a character, and print the month of the year, otherwise
print Error using switch.

You might also like