lecture3

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

Info iconThis preview shows pages 1–11. 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 do-while 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
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 2
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 3

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

View Full DocumentRight Arrow Icon
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 4
The while Loop The while loop 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
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 { do-this; etc… } Usage:while ( expression is true ) int count = 0; count < 2 cout <<count<<“\ n”; ++count; cout << “Done”; count 0 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 6
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 7

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

View Full DocumentRight Arrow Icon
#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 8
// 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 9

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

View Full DocumentRight Arrow Icon
If we use a variable to count in our while loop, this is called Counter- controlled Repetition .
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/20/2009 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 32

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

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

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