{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture3

# lecture3 - We sday Octobe 13th dne r vie halle Re w C nge...

This preview shows pages 1–10. Sign up to view the full content.

Wednesday, October 13 th Review Challenge Variable Scoping Rules Control Structures The while loop The do-while loop More on ++ and --

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

View Full Document
Review Challenge int main(void) { int eyes; std::cin >> eyes; if (eyes > 3) { if (eyes < 10) cout << “A spider!\n”; else cout << “A freak!\n”; } else cout << “A typical animal.”; } What does the following program print out if the user enters 10? How about 3 and 6?
int main(void) { int boogers; cout << "Enter number of boogers: "; cin >> boogers; if (boogers < 3) { int real_boogers = 10*boogers; cout << “I think you really have “ << real_boogers << “ boogers!\n”; } cout << “Go blow your nose!\n”; } Variable Scoping Rules Not only can you declarevariables in your main function, but you can also declarevariables insideblocks enclosed by { and } . boogers 99 2 2 real_boogers 20 cout << real_boogers; // SYNTAX ERROR!

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

View Full Document
Control Structures Weusecontrol structures in our C++ programs to let us makedecisions and repeat operations multipletimes. IF student is asleep THEN put shaving cream on __ their head ELSE teach normally WHILE carey has hairs left complain about lecture complain about homework
The while Loop Thewhileloop allows us to repeat (iterate) many times through a set o logic. Usage:while( expression is true ) do-this-statement; No semi! { do-this; and-this; etc… } int count = 0; count < 2 cout <<count<<“\ n”; ++count; cout << “Done”; count 0 0 < 2? 0 1 1 < 2? 1 2 2 < 2? Done

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

View Full Document
The while Loop { do-this; and-this; } Usage:while( expression is true ) int count = 0; count < 2 cout <<count<<“\ n”; ++count; cout << “Done”; count 0 0 < 2? 0 < 2? 0 < 2? 0 0 0 It’s YOUR job to ensure that your while loop doesn’t go on forever! Etc…
The while Loop What will it print out if theuser types 2? #include <iostream> using namespace std; int main(void) { int stars; cout<<"How many stars? "; cin >> stars; while (stars != 0) { cout << "*"; --stars; } cout << “\n”; } stars -76 How many stars? 2 2 2 != 0? * 1 1 != 0? * 0 0 != 0? Questions: 1. What if theuser types in a negative number? 2. How can we fix this program for that case? 3. What’s the minimum number of times the body of our loop will execute?

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

View Full Document
#include <iostream> using namespace std; int main(void) { int stars, count; cout<<"How many stars? "; cin >> stars; count = 0; while (count < stars) { cout << "*"; ++count; } cout << "\n"; } The while Loop Here’s yet another version of the while loop! This one uses two variables! stars 171 count 66 How many stars? 2 2 0 0 < 2 * 1 1 < 2 * 2 2 < 2 Questions: 1. What would happen if we changed our loop to: while(count <= stars)
// print out squares #include <iostream> using namespace std; int main(void) { int number, cur; cout << "Gimme a number: "; cin >> number; cur = 1; cout << “Squares of 1 to " << number << endl; while (cur < number) { cout << cur*cur << endl; cur += 1; } return(0); } This program asks the user to enter a number and then prints out the square of each number between 1 and the number.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 32

lecture3 - We sday Octobe 13th dne r vie halle Re w C nge...

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

View Full Document
Ask a homework question - tutors are online