lecture3

lecture3 - Wednesday, October 13th Revi ew Chal l enge Var...

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

View Full Document Right Arrow Icon
Wednesday, October 13 th Review Challenge Variable Scoping Rules Control Structures The while loop The do-while loop More on ++ and --
Background image of page 1

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

View Full DocumentRight Arrow Icon
One last time…. Programming Language Inventor Or Serial Killer See if you can guess who uses a keyboard and who uses a chainsaw!
Background image of page 2
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?
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 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 declare variables in your main function, but you can also declare variables inside blocks enclosed by { and } . boogers 99 2 2 real_boogers 20 cout << real_boogers; // SYNTAX ERROR!
Background image of page 4
Control Structures We use control structures in our C++ programs to let us make decisions and repeat operations multiple times. 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
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 while Loop The while loop allows us to repeat (iterate) many time through a set of 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
Background image of page 6
The while Loop { do-this; etc… } Usage: while ( expression is true ) int count = 0; count < 2 cout <<count<<“\ n”; ++count; cout << “Done”; count 0 0 < 2? 0 < 2? 0 0 0 It’s YOUR job to ensure that your while loop doesn’t go on forever! Etc…
Background image of page 7

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

View Full DocumentRight Arrow Icon
The while Loop What will it print out if the user 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 the user 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?
Background image of page 8
#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)
Background image of page 9

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

View Full DocumentRight Arrow Icon
// 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. Is it correct? Hint: what happens if the user types in 1 ?
Background image of page 10
Be Careful With the While Loop int main(void) { int boogers; boogers = 1; while (boogers > 0) { cout << “Achoo!”; boogers--; } } ; What’s wrong with this program?
Background image of page 11

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

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

Page1 / 34

lecture3 - Wednesday, October 13th Revi ew Chal l enge Var...

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

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