204111 – Computer and Programming Section 451 Lab #3 – Conditional Statements Student ID Name Signature Sheet’s Owner Group partner 1. Boolean Expressions A boolean expression have only two possible values: true and false. In C# , boolean expressions can be written using one or more of the following symbols: Relation = 6= > ≥ < ≤ C# notation == != > >= < <= Example x == y x != y x > y x >= y x < y x <= y x x x x x x is is is is is is Meaning equal to y not equal to y greater than y greater than or equal to y less than y less than or equal to y Boolean expressions can be combined or modified to form a more complex expression using one of the following operators. • && combines two boolean expressions using the operator AND. For instance, the expression (x>10) &&(x<10) is true when x is between 1 and 10. • || combines two boolean expressions using the operator OR. For instance, the expression (x<1) ||(x>10) is true when x is less than 1 or greater than 10. • ! negates the truth value of a boolean expression. For example, !(x==1) is true when x is not equal to 1. Lesson 1.1: Let x, y and z be of type int and ch of type char. Describe the condition that makes each of the following boolean expressions true Expression x > 2 x%2 == 0 (x%5 == 0) (x%y == 0) ((x%y == 0) && (z%y ch == ’a’ ((ch >= ’a’) && (ch ((ch >= ’A’) && (ch ((ch >= ’0’) && (ch (ch != ’*’) !(ch == ’*’) Lab #3 Condition to be true true when x is greater than 2 true when x is an even number == 0)) <= ’z’)) <= ’Z’)) <= ’9’)) true if ch is a character between ‘0’ and ’9’ 1 of 9 204111 – Computer and Programming Section 451 2. if and if...else Statements if statement is a conditional statement that controls whether a specified statement should be executed, based on the given condition. There are two forms of usage, as follows. • Form 1: if statement the statement will be executed when the condition is true if ( condition ) statement ; // execute if condition == true In C# , a pair of braces ({}) are used to group multiple statements together, which is useful when we need more than one statement to be executed when the condition is true. if ( condition ) statement1 ; statement2 ; statement3 ; } { // execute if condition == true // execute if condition == true // execute if condition == true • Form 2: if...else statement The statement1 will be executed if the condition is true. If the condition is false, the statement2 will be executed instead. if ( condition ) statement1 ; // execute if condition == true else statement2 ; // execute if condition == false Similar to the above, multiple statements can be grouped together using braces ({}). if ( condition ) statementT1 ; statementT2 ; } else { statementF1 ; statementF2 ; } { // execute if condition == true // execute if condition == true // execute if condition == false // execute if condition == false Example 2.1: Consider the following pseudo-code if student’s score is greater than or equal to 60 Print "Passed" otherwise Print "Failed" which means “if the student’s score is greater than or equal to 60, show Passed ; otherwise show Failed.” Using this pseudo-code, we can write code in C# as follows: if (score >= 60) Console.WriteLine("Passed"); if (score < 60) Console.WriteLine("Failed"); Lab #3 2 of 9 204111 – Computer and Programming Section 451 or if (score >= 60) Console.WriteLine("Passed"); else Console.WriteLine("Failed"); Example 2.2: A C# program to determine whether the variable N is even or odd • Using an if statement 1 using System ; 2 class OddOrEvenC { 3 static void Main () { 4 int N ; 5 Console . Write ( " Please input N : " ); 6 N = int . Parse ( Console . ReadLine ()); 7 if ( N %2 == 0) 8 Console . WriteLine ( " {0} is Even Number " , N ); // true 9 if ( N %2 != 0) 10 Console . WriteLine ( " {0} is Odd Number " , N ); // true 11 } 12 } • Using an if...else statement 1 using System ; 2 class OddOrEvenC { 3 static void Main () { 4 int N ; 5 Console . Write ( " Please input N : " ); 6 N = int . Parse ( Console . ReadLine ()); 7 if ( N %2 == 0) 8 Console . WriteLine ( " {0} is Even Number " , N ); // true 9 else 10 Console . WriteLine ( " {0} is Odd Number " , N ); // false 11 } 12 } Lesson 2.1: Type the following program into SharpDevelop , then answer the questions 1 using System ; 2 class Lab321C { 3 static void Main () { 4 int N ; 5 N = int . Parse ( Console . ReadLine ()); 6 if ( N < 0) 7 Console . WriteLine ( " Negative Number " ); 8 else 9 Console . WriteLine ( " Positive Number " ); 10 } 11 } Lab #3 3 of 9 204111 – Computer and Programming Section 451 • Give three different values for the variable N to make the program display “Negative Number”. • Give three different values for the variable N to make the program display “Positive Number”. • If the user enters 0 to the program, what will be the result? • Modify the program so that it can also display “Zero Number” (in addition to “Positive Number” and “Negative Number”) if the user enters 0. Write the program in the box provided below. Lab #3 4 of 9 204111 – Computer and Programming Section 451 Lesson 2.2: Quadrant indicator +Y Q2 Q1 −X +X Q4 Q3 −Y The following incomplete C# program attempts to identify the quadrant of the input (x, y) coordinates. If the input coordinates happen to be on either X-axis or Y-axis, the program will display “I don’t know.” using System ; class Quadrant { static void Main () { Console . Write ( " Enter X : " ); int x = int . Parse ( Console . ReadLine ()); Console . Write ( " Enter Y : " ); int y = int . Parse ( Console . ReadLine ()); if ( ___ ( a ) ___ ) Console . WriteLine ( " ({0} ,{1}) is in Q1 . " , if ( ___ ( b ) ___ ) Console . WriteLine ( " ({0} ,{1}) is in Q2 . " , if ( ___ ( c ) ___ ) Console . WriteLine ( " ({0} ,{1}) is in Q3 . " , if ( ___ ( d ) ___ ) Console . WriteLine ( " ({0} ,{1}) is in Q4 . " , if ( ___ ( e ) ___ ) Console . WriteLine ( " I don ’ t know . " ); x , y ); x , y ); x , y ); x , y ); } } Sample outputs Please input X: -50 Please input Y: 10 (-50, 10) is in Q2. Please input X: 0 Please input Y: 50 I don’t know. Complete the program above by determining what should be put in the blanks marked (a)-(e). Lab #3 5 of 9 204111 – Computer and Programming Blank ___(a)___ Section 451 Boolean expression x > 0 && y > 0 ___(b)___ ___(c)___ ___(d)___ ___(e)___ Example 2.3: A cellular phone company has a promotion plan for its customers. The air time fee is calculated as follows • Each of the first two minutes costs 5 bahts (per minute) • Each of the remaining minutes costs 2 bahts (per minute) The program below will take the number of minutes from the user, and computes the total air time fee. using System ; class Cellphone { static void Main () { // Step 1: Take the number of minutes input , minutes Console . Write ( " Enter the number of minutes : " ); int minutes = int . Parse ( Console . ReadLine ()); // Step 2: Split the number of minutes into two parts , // the first two and the remaining int first , remaining ; if ( minutes > 2) { // Step 2.1: If the call takes more than two minutes , // then the first two minutes is used entirely and the // remaining is minutes subtracted by two first = 2; remaining = minutes - 2; } else { // Step 2.2: If the call takes less than 2 minutes , // these minutes are considered part of the first two first = minutes ; remaining = 0; } // Step 3: Compute the fee based on the number of minutes // during the first two minutes , and the number of minutes // after the first two minutes int fee = ( first *5) + ( remaining *2); Console . WriteLine ( " The air time fee is {0} bahts . " , fee ); } } Sample outputs Enter the number of minutes: 1 The air time fee is 5 bahts. Enter the number of minutes: 5 The air time fee is 16 bahts. Lab #3 6 of 9 204111 – Computer and Programming Section 451 3. Nested if Statements A nested if (or if...else) statement is an if statement that is part of another if statement. A program written in this form usually deals with multiple conditions. A general form of nested IFs is as follows: if (condition1) statement1; else if (condition2) statement2; else if (condition3) statement3; ... ... else statementN; From the above, statement1 is executed when condition1 is true. statement2 is executed when condition1 is false and condition2 is true. statement3 is executed when condition1 and condition2 are false, and condition3 is true. And so on. Therefore, statementN is executed when none of the conditions in the IF structure is true. Example 3.1: Using nested IFs Consider a program that will display a student’s grade based on his/her score, using the grading policy in the table. Condition point < 50 50 ≤ point < 60 60 ≤ point < 70 70 ≤ point < 80 point ≥ 80 Result Grade F Grade D Grade C Grade B Grade A A nested IF can be written in C# as follows: if ( point < 50) Console . WriteLine ( " Grade else if ( point < 60) Console . WriteLine ( " Grade else if ( point < 70) Console . WriteLine ( " Grade else if ( point < 80) Console . WriteLine ( " Grade else Console . WriteLine ( " Grade Lab #3 F " ); D " ); C " ); B " ); A " ); 7 of 9 204111 – Computer and Programming Section 451 Lesson 3.1: Body Mass Index (BMI) The BMI exercise done in class did not give the complete classification. The complete list is shown below: BMI BM I < 18.5 18.5 ≤ BM I < 25 25 ≤ BM I < 30 BM I ≥ 30 Interpretation Underweight Normal Overweight Obese Complete the following BMI calculator program by filling in appropriate boolean expressions in the provided blanks. using System ; class BMICalc { static void Main () { Console . Write ( " Enter your weight ( in kg ): " ); double w = double . Parse ( Console . ReadLine ()); Console . Write ( " Enter your height ( in m ): " ); double h = double . Parse ( Console . ReadLine ()); double bmi = w /( h * h ); Console . WriteLine ( " Your BMI is {0: f2 }. " , bmi ); if ( ___ ( a ) ___ ) Console . WriteLine ( " You else if ( ___ ( b ) ___ ) Console . WriteLine ( " You else if ( ___ ( c ) ___ ) Console . WriteLine ( " You else Console . WriteLine ( " You are underweight . " ); are normal . " ); are overweight . " ); are obese . " ); } } Sample outputs: Enter your weight: 65 Enter your height: 1.75 Your BMI is 21.22. You are normal. Enter your weight: 100 Enter your height: 1.60 Your BMI is 39.06. You are obese. Blank ___(a)___ Boolean expression ___(b)___ ___(c)___ Lab #3 8 of 9 204111 – Computer and Programming Section 451 4. Programming Exercises Task 1: Integer identifier Write a C# program to determine whether the input number is an integer. (Hint: Use the method Math.Round()) Sample outputs: Please input N: 1.5 1.5 is not an integer. Please input N: 99 99 is an integer. Copy the program into the box below: Lab #3 9 of 9 204111 Computers and Programming Task 2: Equation Solution Solver Section 451 Write a C# program to compute f(x) according to the following conditions: ⎧2x + 2, ⎪ f (x) = ⎨ 3x 2 , ⎪ 4 x 3 − 5, ⎩ x ≤ 10 10 < x ≤ 15 x > 15 Sample outputs Please input x: 5 f(5) = 12 Please input x: 12 f(12) = 432 Please input x: 18 f(18) = 23323 Copy the program into the box below: Lab #3 204111 Computers and Programming Task 3: Odd Number Identifier and Summation Computation Section 451 Write a C# program that accepts 5 positive integers. Then, the program identifies and prints out all the odd numbers and the summation of them. Sample outputs: Please input 5 positive integers: 1 2 3 4 5 Odd numbers: 1 3 5 Summation of odd numbers: 9 Please input 5 positive integers: 2 4 6 8 10 Odd numbers: Summation of odd numbers: 0 Copy the program into the box below: Lab #3
© Copyright 2025 Paperzz