Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.

Similar presentations


Presentation on theme: "1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement."— Presentation transcript:

1 1 CS 1430: Programming in C++

2 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement

3 3 IF - Else if (cond) statement1 else statement2 //Next statement if (cond) { statement11 statement12... } else { statement21 statement22... } //Next statement

4 4 IF – Else if (cond1) statement block1 else if (cond2) statement block2 else statement block3 //Next statement

5 5 Example int score; char grade; cin >> score; if (score >= 90) grade = 'A'; else if (score >= 80) grade = 'B'; else if (score >= 70) grade = 'C'; else if (score >= 60) grade = 'D'; else // NO if here! grade = 'F'; cout << “Your Grade: ” << grade; // The order is crucial: semantics!

6 6 Different Order int score; char grade; cin >> score; if (score >= 80) grade = ‘B'; else if (score >= 90) grade = ‘A'; else if (score >= 70) grade = 'C'; else if (score >= 60) grade = 'D'; else // NO if here! grade = 'F'; cout << “Your Grade: ” << grade; // You never get ‘A’!

7 7 Different Way if (score >= 80 && score < 90) grade = ‘B’; if (score >= 90) grade = ‘A’; if (score >= 70 && score < 80) grade = ‘C’; if (score >= 60 && score < 70) grade = ‘D’; if (score < 60) grade = ‘F’; // Is it correct? // Can the statements be in different order? // Which is better? // if – else if – else if (score >= 90) grade = 'A'; else if (score >= 80) grade = 'B'; else if (score >= 70) grade = 'C'; else if (score >= 60) grade = 'D'; else // NO if here! grade = 'F'; // Do it this way! // Magic numbers!

8 8 // Declare constants to avoid magic numbers const float MAX_SCORE = 100.0; //change to 100 for final const float MIN_SCORE = 0.0; const float A_PERNT = 0.9; const float B_PERNT = 0.8; const float C_PERNT = 0.7; const float D_PERNT = 0.6; const char GRADE_A = ‘A’; const char GRADE_B = ‘B’; const char GRADE_C = ‘C’; const char GRADE_D = ‘D’; const char GRADE_F = ‘F’; if (score >= MAX_SCORE * A_PERNT) grade = GRADE_A; else if (score >= MAX_SCORE * B_PERNT) grade = GRADE_B; else if (score >= MAX_SCORE * C_PERNT) grade = GRADE_C; else if (score >= MAX_SCORE * D_PERNT) grade = GRADE_D; else grade = GRADE_F;

9 9 char ch1, ch2, ch3; cin >> ch1 >> ch2 >> ch3; if (ch1 == ch2) if (ch2 == ch3) cout << "All characters are the same."; else cout << "First two are the same."; if (ch1 == ch2) if (ch2 == ch3) cout << "All characters are the same."; else cout << "First two are NOT the same."; // Do we get “NOT the same” when ch1 != ch2? // NO! // else is paired with the last if // Indentation does not matter Nested IF

10 10 // Use braces to change the pairing if (ch1 == ch2) { if (ch2 == ch3) cout << "All initials are the same."; else cout << "First two are the same."; } if (ch1 == ch2) { if (ch2 == ch3) cout << "All initials are the same."; } else cout << "First two are NOT the same.";

11 11 if (ch1 == ch2) { if (ch2 == ch3) cout << "All initials are the same."; else cout << "First two are the same."; } else { if (ch2 == ch3) cout << "Last two are the same."; else if (ch1 == ch3) cout << "First and last are the same."; else cout << "All initials are different."; } // Next statement

12 12 char ch1, ch2, ch3; cin >> ch1 >> ch2 >> ch3; if (ch1 == ch2 && ch2 == ch3) cout << "All characters are the same."; if (ch1 == ch2 || ch2 == ch3 || ch1 == ch3) cout << “At least two characters are the same."; if (ch1 != ch2 && ch2 != ch3 && ch1 != ch3) cout << “The three characters are different."; if (ch1 != ch2 || ch2 != ch3 || ch1 != ch3) cout << “At least two characters are different."; if (ch1 && ch2 == ch3) // NO! if (ch1 == ch3) && (ch2 == ch3) // NO! if ((ch1 == ch3) && (ch2 == ch3)) // Yes! if (ch1 || ch2 == ch3) // NO! if (ch1 == ch3 || ch2 == ch3) // YES! Multiple Conditions

13 13 Arithmetic Expression and Logical (Boolean) Expression if (num2 == 0) cout << endl << "Cannot divide by zero!"; else { quotient = float(num1) / num2; cout << endl << "The quotient is " << quotient; }

14 14 Truth Table Cond1 Cond2 Cond1 && Cond2 Cond1 || Cond2 !Cond1 T F F F T T T F F T F F T T T F F F T T Value of Arithmetic Expression : a number Value of Logical Expression : true or false

15 15 Evaluate Logical Expression int num1 = 5, num2 = 7, num3 = 12; Logical Expression Value num1 + num2 > num3 ? (num1 + num2) > num3 (num3 > num2) && (num1 % num2 == 2) ? (num3 <= num2) || (num1 % num2 != 2) ? !((num3 > num2) && (num1 % num2 == 2)) ? (!(num3 > num2) || !(num1 % num2 == 2)) ?

16 16 Evaluate Logical Expression Logical Expression Same As This? !(cond1 && cond2) (!cond1 || !cond2) !(cond1 || cond2) (!cond1 && !cond2) DeMorgan’s Law

17 17 The Area of a Circle A = πr 2 const float PI = 3.14; float area, radius; area = PI * radius * radius; area = PI * pow(radius, 2);

18 18 The Area of a Circle const float PI = 3.14; float area, radius; cin >> radius; // Checking input if (radius > 0) { area = PI * radius * radius; cout << “The area is ” << area << ‘.’; } else cout << “Radius is zero or negative.”;

19 19 Example int inputValue; float result; char op; // ‘c’ for cube, ‘s’ for sqare, ‘q’ for sqare root, and any other chars are invalid cout << “Enter an integer and an operator: ”; cin >> inputValue >> operator; // Checking input if (op == ‘c’ || op == ‘C’) { } else if (op == ‘S’ || op == ‘s’) { } else if (op == ‘q’ || op == ‘Q’) { } else cout << “Invalid operator.”;

20 20... if (op == ‘c’ || op == ‘C’) { result = pow(inputValue, 3); cout << “The cube of ” << inputValue << “ is ” << result << ‘.’; } else if (op == ‘S’ || op == ‘s’) { result = pow(inputValue, 2); cout << “The sqare of ” << inputValue << “ is ” << result << ‘.’; } else if (op == ‘q’ || op == ‘Q’) { if (inputValue < 0) cout << “The input value is negative!”; else { result = sqrt(inputValue); cout << “The sqare root of ” << inputValue << “ is ” << result << ‘.’; } } else cout << “Invalid operator.”;

21 21 int main() { int inputValue; float result; char op; cout << “Enter an integer and an operator: ”; cin >> inputValue >> operator; if (op == ‘c’ || op == ‘C’) { result = pow(inputValue, 3); cout << “The cube of ” << inputValue << “ is ” << result << ‘.’; } else if (op == ‘S’ || op == ‘s’) { result = pow(inputValue, 2); cout << “The sqare of ” << inputValue << “ is ” << result << ‘.’; } else if (op == ‘q’ || op == ‘Q’) { if (inputValue < 0) cout << “The input value is negative!”; else { result = sqrt(inputValue); cout << “The sqare root of ” << inputValue << “ is ” << result << ‘.’; } } else cout << “Invalid operator.”; return 0; }

22 22 int main() { int inputValue; float result; string op; cout << “Enter an integer and an operator: ”; cin >> inputValue >> operator; if (op == “cube”) { result = pow(inputValue, 3); cout << “The cube of ” << inputValue << “ is ” << result << ‘.’; } else if (op == “square”) { result = pow(inputValue, 2); cout << “The sqare of ” << inputValue << “ is ” << result << ‘.’; } else if (op == “root”) { if (inputValue < 0) cout << “The input value is negative!”; else { result = sqrt(inputValue); cout << “The sqare root of ” << inputValue << “ is ” << result << ‘.’; } else cout << “Invalid operator.”; return 0; }

23 23 int main() { int inputValue; float result; string op; cout << “Enter an integer and an operator: ”; cin >> inputValue >> operator; if (op == “cube”) { result = pow(inputValue, CUBE); cout << “The cube of ” << inputValue << “ is ” << result << ‘.’; } else if (op == “square”) { result = pow(inputValue, SQUARE); cout << “The sqare of ” << inputValue << “ is ” << result << ‘.’; } else if (op == “root”) { if (inputValue < 0) cout << “The input value is negative!”; else { result = sqrt(inputValue); cout << “The sqare root of ” << inputValue << “ is ” << result << ‘.’; } else cout << “Invalid operator.”; return 0; }

24 24 #include using namespace std; int main() { int inputValue; float result; string op; cout << “Enter an integer and an operator: ”; cin >> inputValue >> operator; if (op == “cube”) { } else if (op == “square”) { } else if (op == “root”) { } else cout << “Invalid operator.”; return 0; }

25 Lab Assistants 25

26 26 Schedule Quiz2-1 (1 point) Due today by 10 pm Lab1 (3 points) Grace Time: Tuesday by 5 pm Quiz2-2 (2 Points) Wednesday in Class Prog 1 Due Next Monday

27 Grader You can submit to the grader multiple times. We grade your last submission. After you complete Labs before the due/grace time, DO NOT submit it again after the due/grace time. Keep the email with 0 differences. Email me if you did it by accident. 27

28 Lab Access Room 009, 110 and 206 in Ullrich Weekdays: –7 am – 11 pm Weekends –9 am – 10 pm West side door Scan your ID card Report to me and ITS Helpdesk if not working 28

29 UWP Email Check UWP email at least once a day Email me with any issues 29

30 Last Day to Drop without a ‘W’ 30 Drop fee begins and Withdrawn is noted on transcript Wednesday, September 16 Am I ready for CS143? Should I Go to CS113?


Download ppt "1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement."

Similar presentations


Ads by Google