Presentation is loading. Please wait.

Presentation is loading. Please wait.

Wednesday, 10/30/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/30/02  QUESTIONS??  Today:  Finish discussion of random numbers  Logical operators.

Similar presentations


Presentation on theme: "Wednesday, 10/30/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/30/02  QUESTIONS??  Today:  Finish discussion of random numbers  Logical operators."— Presentation transcript:

1 Wednesday, 10/30/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/30/02  QUESTIONS??  Today:  Finish discussion of random numbers  Logical operators and bool objects  For-loops  Reading: Chap. 7 through 7.4  Exercises: p. 178 #4, 10, 11  New files/handouts: FileWordCount.cpp

2 Wednesday, 10/30/02, Slide #2 Random probabilities and random integers in a range  Random probabilities, i.e., random fractional values between 0 and 1  double(rand()) / RAND_MAX  Random integers in a range, i.e., random whole numbers from a low value to a high value  rand() % (hi - lo + 1) + lo  Utility file RandomUtils.h contains precoded functions to do these tasks:  void InitRand();  int RandLoHi(int lo, int hi);  double RandProb();

3 Wednesday, 10/30/02, Slide #3 Compound Expressions: Logical Operators  Logical operators:  AND: &&  OR: ||  NOT: !  Warning: &, | are also operators (bitwise and, or)  Examples:  if ((Ans == ‘Y’) || (Ans == ‘y’)) cout << “YES!”;  if ((x >= y) && (x >= z)) cout << “x is biggest.”;  if (!(Num % 3 == 0)) cout << “Not a multiple of 3”;  Question: Give a legal C++ expression saying 90  Score  100

4 Wednesday, 10/30/02, Slide #4 Operator Precedence Revisited  Precedence of operators (from highest to lowest)  Parentheses (, )  Unary operators -, ++, --, !  Multiplicative operators *, /, %  Additive operators +, -  Relational ordering, =  Relational equality ==, !=  Logical and &&  Logical or ||  Assignment =

5 Wednesday, 10/30/02, Slide #5 Operator Precedence Revisited  Examples 5 != 6 || 7 <= 3 (5 != 6) || (7 <= 3) 5 * 15 + 4 == 13 && 12 < 19 || !(3 < 0)

6 Wednesday, 10/30/02, Slide #6 Short-circuit evaluation  SHORT-CIRCUIT EVALUATION: As with other operators, C++ will evaluate logical operators with the same precedence from left to right. But, it will stop evaluating as soon as the boolean value of the full expression is determined. Example 1: Example 2: int x = 5; float y = - 3.14; if (x > 0 || x == -1) if (y > 0 && sqrt(y) 0 || x == -1) if (y > 0 && sqrt(y) < 10) cout << “HI”; cout << “BYE”; cout << “HI”; cout << “BYE”;

7 Wednesday, 10/30/02, Slide #7 The type bool  The latest C++ standard includes a type bool  This type has two possible values: true or false  bool is actually an integer type: false corresponds to 0, and true to a non- zero value  Examples: bool P = true; bool Q = false; bool R = true; bool S = P && Q; bool T = !Q || false; bool U = !(R && !Q);

8 Wednesday, 10/30/02, Slide #8 Using bool objects  Objects of type bool can be used to help readability of code and simplify expressions:  cin >> Guess; //should be 0 or 1 bool ValidData = (Guess == 0) || (Guess == 1) if (ValidData) { //Continue with program...... }

9 Wednesday, 10/30/02, Slide #9 The for-loop  The for-loop is a looping structure equivalent in capability to the while-loop, that builds into the structure:  Initialization  Loop condition  Updating for (Initialization statement; Loop condition; Update statement) { Loop body }

10 Wednesday, 10/30/02, Slide #10 For-loops for counting  For-loops are a very straightforward structure to use when you want simply to repeat an action a predetermined number of times: for (int i = 1; i <= 500; ++ i) {int FlipNum = RandLoHi(0,1); if (FlipNum == 0) ++HCount; else //FlipNum == 1 ++TCount; }

11 Wednesday, 10/30/02, Slide #11 Nesting for-loops  As with while loops, we can nest for-loops. The inner loop must do all its repetitions for each iteration of the outer loop  Example: Write a nested for-loop to draw a box of stars with numRows rows and numCols columns

12 Wednesday, 10/30/02, Slide #12 Other uses of for-loops  This for-loop determines if a string S is a palindrome (same forwards and backwards):  Here is a for-loop that counts all the “words” (strings separated by white space) in a file (see FileWordCount.cpp): for (int count = 0; inFile >> word; ++count); //No body needed!! bool Same = true; int N = S. size(); for (int i = 0; Same && i < N/2; ++i) {Same = (S[i] == S[N-1-i]); }


Download ppt "Wednesday, 10/30/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/30/02  QUESTIONS??  Today:  Finish discussion of random numbers  Logical operators."

Similar presentations


Ads by Google