lecture4 - Monday,October18 ReviewChallenge...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Monday, October 18 th Review Challenge More Control Structures ||  and  The  for  loop The  switch  statement break  and  continue Nested loops
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Review Challenge: It Prints What? #include <iostream> using namespace std; int main(void) { int drinks; cout << How many cokes? cin >> drinks; while (drinks-- > 0) { if (drinks % 2 == 0) cout << Gulp\n ; else cout << Chug\n ; } cout << BARF!\n ; return(0); } drinks 3 2 How many cokes? 3 3 > 0 2 % 2  Gulp 2 > 0 1 1 % 2  Chug 1 > 0 0 0 % 2  Gulp 0 > 0 -1 BARF!
Background image of page 2
To check more than one thing in an if statement , use , || for OR :   Usage : do-this-if-all-are-true; Or : if ( expression1 || expression2 || …) do-this-if-either-is-true; You can also use || and && with loops…
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
int main(void) { int cokes, nachos; cout << “Enter number of cokes and nachos: "; cin >> cokes >> nachos; if ( cokes > 5 || nachos > 5 ) cout << "barf!\n"; if ( ) cout << "yum!\n"; } Most often, you will see   and  ||  used inside  if statements . Here are some examples:
Background image of page 4
int main(void) { int cokes, nachos; cout << “Enter number of cokes and nachos: "; cin >> cokes >> nachos; while ( cokes > 0 || nachos > 0 ) { if (cokes > 0) cout << “Gulp\n”; if (nachos > 0) cout << “Crunch\n”; cokes--; nachos--; } } However, you can also use   and  ||  with  while loops do while loops , etc. ..
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The following sentence is ambiguous: I fart if I eat rice or beans and broccoli. It might mean: I fart if I eat rice    OR I fart if I eat beans AND I also eat  broccoli Or it could be mean: I fart if I eat rice OR beans    AND I also eat broccoli    cout << “I fart\n”; Similarly, this  if statement  is ambiguous:
Background image of page 6
In order to make your logic more clear, you can use parentheses:    cout << “I fart\n”; (                                       )    cout << “I fart\n”; I fart if I eat rice OR beans    AND I also eat broccoli I fart if I eat rice    OR I fart if I eat beans AND I also eat  broccoli (                                              )
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
But how will C++ interpret your statement if you leave out the  parentheses?     cout << “I fart\n”; In C++ the 
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 38

lecture4 - Monday,October18 ReviewChallenge...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online