lecture4

lecture4 - Monday, October 18 th Review Challenge More...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Monday, October 18 th Review Challenge More Control Structures || and && The for loop The switch statement break and continue Nested loops 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 % 2 Gulp 0 > 0-1 BARF! && 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 && 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: && 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... && 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: && 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 ( ) && 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...
View Full Document

This note was uploaded on 11/07/2009 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 38

lecture4 - Monday, October 18 th Review Challenge More...

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