Lecture 9 Reminder: Programming Assignment 3 is due Wednesday by 4:30pm. Exam 1 is on Friday. Exactly like Prog. Assign. 2; no collaboration or help from the instructor. Log into Windows/ACENET. Start MSVS and open "HelloWorldProgram", then open MSVS again and create a new console project called "QuadraticRootsProgram" and rename "Program.cs" (and the class) to "QuadraticRoots.cs" Questions? Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1
Outline Flow of control Simple sequence Selection Conditions (boolean expressions) Equality, relational, logical operators Short-circuit evaluation If-statements Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 2
Flow of Control Flow of control determines the order in which lines of code are executed. So far, every line of code in a method is executed in the order they are written. This order is called simple sequence. Sometimes, we do not want to execute every statement. This requires a choice to be made based on a test of some values. This flow order is called selection. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 3
Conditions Tests to determine which choice to be made are called conditions and are boolean expressions that evaluate to true or false (i.e., a value of bool type). There are three kinds of operators that evaluate to boolean values: equality, relational, and logical operators. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 4
Equality Operators The equality operators correspond to the English phrases "is equal to" and "is not equal to". Their operator symbols are == and!=, respectively. == evaluates to true when the values of both operands have the same value; false otherwise.!= evaluates to true when the values of the operands are not the same value; false otherwise. The types of the operands must be the same Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 5
Equality Operator Examples int avalue = 100, bvalue = 1000; char cvalue = 'A'; string svalue = "CS 205"; bool testresult = false; testresult = avalue == 100; testresult = avalue!= bvalue; testresult = cvalue!= 'A'; testresult = svalue == "cs 205"; Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 6
Relational Operators The relational operators and their English equivalents are as follows: < "is less than" <= "is less than or equal to" > "is greater than" >= "is greater than or equal to" The types of the operands must be the same. These operators are not defined on the string type. The characters are ordered based on the underlying encoding. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 7
Relational Operator Examples testresult = avalue > 10; testresult = avalue <= bvalue; testresult = cvalue < 'Z'; testresult = cvalue >= 'a'; Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 8
Logical Operators Logical operators have boolean expressions as operands. They are logical negation ("not", symbol!), conditional logical conjunction ("and", symbol &&), and conditional logical disjunction ("or", symbol ). The evaluation of these operators can be represented using a truth table: p q!p p && q p q true true false true true true false false true false true true false true false false false false Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 9
Logical Operator Examples // test if avalue is between 1 and 100, inclusive testresult = (1 <= avalue) && (avalue <= 10); // test if avalue is between 1 and 100, exclusive testresult = (1 < avalue) && (avalue < 100); // test if cvalue is an uppercase letter testresult = ('A' <= cvalue) && (cvalue <= 'Z'); // test if cvalue is a 'y' or a 'Y' testresult = (cvalue == 'y') (cvalue == 'Y'); Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 10
Operator Precedence Just like the arithmetic operators, the equality, relational, and logical operators have precedence. Use ( )'s to override, or for clarity. Highest precedence: unary +, unary -,! (unary) *, / % (multiplicative) binary +, binary - (additive) <, <=, >, >= (relational) ==,!= (equality) && (conditional AND) (conditional OR) Lowest precedence: = (assignment) Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 11
Short-Circuit Evaluation Both && and have short-circuit evaluation. For &&, if either of the operands is false, the entire expression is false, so when the first operand evaluates to false, the second one is not evaluated. This is often used to prevent an invalid computation. For example, if we want to make sure not to divide by 0: (n > 0) && (totalscores/n > 70) Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 12
Short-Circuit Evaluation For, an expression is true if at least one of the operands is true, so if the first operand evaluates to true, the second one is not evaluated. This is often used when there a simple condition and a complex condition (perhaps encapsulated in a method call). For example, (n == 1) IsComplexCondition(x, y) Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 13
If-Statements Now that we have conditions, we can use them to make choices for flow of control. For example, suppose we want to compare two numbers, a and b, and determine the larger one. In English, we might say "If a is greater than b, then a is the larger number; otherwise b is the larger number". In C# code, we use an if-statement, to implement this. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 14
If-Statements An if-statement has: A condition test A body to execute when the test is true An optional body to execute when the test is false The syntax of an if-statement is: if (<condition>) { <body to execute when condition is true> } else // this part is optional { <body to execute when condition is false> } Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 15
If-Statement Example The C# code for our example of determining the larger of values a and b is: double larger; if (a > b) condition test { larger = a; executed when condition is true } else { larger = b; executed when condition is false } Console.WriteLine ("{0:F} is the larger number", larger); Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 16
If-Statement Example The "else" part of an if-statement is optional. Without the else part, the flow of control is to execute the if-body or skip it. For example, a wage earner is paid an hourly rate for up to 40 hours a week and an overtime rate that is 1.5 times the hourly rate for hours over 40. grosspay = hours * hourlyrate; // compute base pay if (hours > 40) // skip if condition is false { // compute and add overtime pay overtimepay = (hours 40) * hourlyrate * 1.5; grosspay = grosspay + overtimepay; } Console.WriteLine("Gross pay is {0:F2}", grosspay); Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 17
In-Class Exercise For a quadratic equation, ax 2 bxc=0 the formula for finding the roots of the equation is: x= b± b 2 4 ac 2 a The quantity b 2 4 ac is called the discriminant. When the discriminant is greater than 0, the equation has 2 real roots. When it is 0, there is only 1 real root. And when it is less than 0, there are no real roots. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 18
In-Class Exercise In the Main method of the QuadraticsRoot project, write code to do the following: Prompt and input the three coefficients for a quadratic equation. Compute the discriminant for the equation. If the discriminant is greater than 0, compute and display the two real roots If the discriminant is 0, display the one real root. If the discriminant is less than 0, display a message saying there are no real roots. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 19
In-Class Exercise Three example runs: Enter coefficient a: 1 Enter coefficient b: 2 Enter coefficient c: 3 The two real roots are 1.00 and 3.00 Enter coefficient a: 1 Enter coefficient b: 2 Enter coefficient c: 1 The one real root is 1.00 Enter coefficient a: 1 Enter coefficient b: 2 Enter coefficient c: 2 This equation has no real roots Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 20