Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.

Similar presentations


Presentation on theme: "1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194."— Presentation transcript:

1 1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194

2 2 9/25/06CS150 Introduction to Computer Science 1 Exam Review 1. unsigned int bigNumber = 4000000000; char singleChar = ‘A’; 3. value = value / (5 % 3 ); value -= 4 + z; // value = 11 4. String is not a keyword: This means it is not part of the C++ language. In order to use the string data type, the string library must be included using #include

3 3 9/25/06CS150 Introduction to Computer Science 1 Exam Review 5. bool bLessThan100; cout << “Please enter... : “<<; cin >> value; // be careful of value == 100 bLessThan100 = ( value < 100 ); if( bLessThan100 ) { cout << “Value is < 100\n”; } else { cout << “Value is not < 100\n”; }

4 4 9/25/06CS150 Introduction to Computer Science 1 Exam Review 6. z = static_cast (x) / y; 7. #include “stdafx.h” #include using namespace std; int main() { int radius = 16; const double PI = 3.14159; area = PI * radius * radius; cout << setprecision(2) << fixed; cout << “The radius is “ << radius; cout << “ the area is “ << area << endl; return 0; }

5 5 9/25/06CS150 Introduction to Computer Science 1 Logical Operators  Logical operators allow us to join expressions in a conditional expression LogicalOp expression  How many logical operators can you name?  Where do you think these fit into the precedence hierarchy with respect to other operators?

6 6 9/25/06CS150 Introduction to Computer Science 1 Precedence (page 1125) Precedence Operators (Highest to Lowest) - (unary negation) * / % - + > < == != = += -= *= /= %= Relational Operators Assignment Operators Arithmetic Operators

7 7 9/25/06CS150 Introduction to Computer Science 1 Logical Operators  How are these used? int x = 99, y = -9; if ( expression LogicalOp expression ) { // statements } Practice: Set y = 42 if x is greater than 100 or y is less than 0; otherwise set x = 42.

8 8 9/25/06CS150 Introduction to Computer Science 1 exit()  To terminate a program we can use the exit(int status) function o This is a function, not part of the language  #include o The status is returned to the operating system to denote program success or failure  Success: 0  Failure: non-zero

9 9 9/25/06CS150 Introduction to Computer Science 1 Practice  Write a program that will ask the user for two integers. Display both integers to the screen if they are each greater than 1000 and terminate the program with exit() otherwise. Use exactly one if/else #include using namespace std; int main() {

10 10 9/25/06CS150 Introduction to Computer Science 1 Practice  Are these two code snippets equivalent? int x, y; if ( x > y ) { x += y; } if ( y < x) { y += x; } int x, y; if ( x > y ) { x += y; } else { y += x; }

11 11 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs if ( x > y ) { } else { if ( x == 9 ) { } else { }  The second if is only executed if the first if conditional is false  Note the indentation of the inner if  There may be code between the { with the first else and the second if

12 12 9/25/06CS150 Introduction to Computer Science 1 Using nested ifs …  Write a snippet of code that will: o add y to x if x > y o add x to y if y > x o add 1 to x if x == y int x, y; if (

13 13 9/25/06CS150 Introduction to Computer Science 1 C++ Shortcut if ( x > y ) { // do A } else { if ( x == 9 ) { // do B } else { // do C } if ( x > y ) { // do A } else if ( x == 9 ) { // do B } else { // do C }

14 14 9/25/06CS150 Introduction to Computer Science 1 Using nested ifs …  Write a snippet of code that will: o add y to x if x > y o add x to y if y > x o add 1 to x if x == y int x, y; if (

15 15 9/25/06CS150 Introduction to Computer Science 1 Chained If/Else statements if ( x > y ) { // do A } else if ( x == 9 ) { // do B } else if ( y > 1 ) { // do C } else { }

16 16 9/25/06CS150 Introduction to Computer Science 1 Using nested ifs …  Write a snippet of code that will do the following: o add y to x if x == y o add x to y if y > x o add 1 to x if (2 * y) == x int x, y; if (

17 17 9/25/06CS150 Introduction to Computer Science 1 Exercise  Write a snippet of code that will print the letter grade (A,B,C,D,F) of a student’s exam and set a bool flag to track if it is a passing grade. int examScore; // from 0 to 100 bool bPassingGrade; if (


Download ppt "1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194."

Similar presentations


Ads by Google