lecture3%2Dpost

lecture3%2Dpost - Wednesday,October13th ReviewChallenge...

Info iconThis preview shows pages 1–9. 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
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;     and-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 < 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 
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

lecture3%2Dpost - Wednesday,October13th ReviewChallenge...

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