lecture4%2Dpost

lecture4%2Dpost - Monday,October18 ReviewChallenge |and...

Info icon This 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
Image of page 1

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

View Full Document Right 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!
Image of page 2
&& and || To check more than one thing in an if statement , use && for AND , || for OR :   Usage : if ( expression1 && expression2 && …) do-this-if-all-are-true; Or : if ( expression1 || expression2 || …) do-this-if-either-is-true; You can also use || and && with loops…
Image of page 3

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

View Full Document Right Arrow Icon
&& and || Example 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 ( (cokes < 3 && nachos < 3) || cokes+nachos <= 5 ) cout << "yum!\n"; } Most often, you will see  &&  and  ||  used inside  if statements . Here are some examples:
Image of page 4
&& and || Example 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...
Image of page 5

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

View Full Document Right Arrow Icon
&& and || Precedence 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 if (  eat_rice == 1  ||  eat_beans == 1  &&  eat_broccoli == 1  )    cout << “I fart\n”; Similarly, this  if statement  is ambiguous:
Image of page 6
&& and || Precedence In order to make your logic more clear, you can use parentheses: if (  eat_rice == 1  ||  eat_beans == 1  &&  eat_broccoli == 1  )    cout << “I fart\n”; (                                       ) if (  eat_rice == 1  ||  eat_beans == 1  &&  eat_broccoli == 1  )    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 (                                              )
Image of page 7

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

View Full Document Right Arrow Icon
&& and || Precedence But how will C++ interpret your statement if you leave out the  parentheses?  if (  eat_rice == 1  ||  eat_beans == 1  &&  eat_broccoli == 1  )    cout << “I fart\n”; In C++ the  AND &&  always has higher precedence then the  OR  || .  So if you leave out the parenthesis, C++ assumes you mean: if (  eat_rice == 1  ||  eat_beans == 1  &&  eat_broccoli == 1  )    cout << “I fart\n”; (                                             ) It’s easy to forget this, so always use parens to be safe.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern