lecture5%2Dpost

lecture5%2Dpost - Wednesday,October20th ReviewChallenge...

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

View Full Document Right Arrow Icon
Wednesday, October 20 th   Review Challenge A new data type:  bool “Short Circuiting” Introduction to Function Calls
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 Write a program that reverses a string… void main(void) { string s = “a man a plan a canal panama”; // write your code here to reverse the string cout << s; // prints “amanap lanac a nalp a nam a”; }
Background image of page 2
Boolean Variables: bool The  bool  data type is another C++ type just like  int float , or  short . bool  variables can hold only two values:  true  or  false int main(void) {    bool oldPerson;    int age;   cin >> age;   if (age > 33)     oldPerson = true;   else    oldPerson = false;    … } int main(void) {    bool bigger;    bigger =  10 > 3 ;    if (bigger == true)      cout << “10 is > than 3\n”; } oldPerson ??          age -44 17 17 false                     bigger ?? true
Background image of page 3

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

View Full DocumentRight Arrow Icon
More bool Examples int main(void) {    bool bigEars;    . ..    if ( bigEars == true )       cout << “U got big Ears!”;    if ( bigEars )        cout << “U got big Ears!”; } There are two different ways to check  if a boolean variable is  true . There are two different ways to check  if a boolean variable is  false . int main(void) {    bool bigEars;    . ..    if ( bigEars == false )       cout << “Small ears!”;    if (   bigEars  )        cout << “Small ears!”; }
Background image of page 4
More bool Examples #include <iostream> #include <cstdlib> int main(void) {     bool done = false;    int myNum = rand( );   // 0 to 65535    while (  done == false )    {       int guess;       cout << “Guess my #: “;       cin >> guess;       if ( guess == myNum )          done = true;    }    cout << “You got it!\n”; } Boolean variables are often  used to decide when to end a  loop.         done false      myNum 721 The  rand  function returns a random  integer between  0 and 65535 .   rand  is a function just  like  main  is.      guess -10 Guess my #: 721 721 721 == 721?? true true == false?? You got it!
Background image of page 5

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

View Full DocumentRight Arrow Icon
Short Circuiting if ( cute == true  ||  smart == true  ||  rich == true )     cout << “Hey, want to go on a date?”;       Consider the above expression… If you’re either  cute , or  smart , or  rich , then I’d like to go out  with you. Let’s say that I happen to know you’re  cute . bool cute =  true ;   Once I know you’re cute, I no longer need to waste time asking  the other two questions…
Background image of page 6
Short Circuiting if ( cute == true  ||  smart == true  ||  rich == true )     cout << “Hey, want to go on a date?”;       This is called 
Background image of page 7

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

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

This note was uploaded on 04/10/2012 for the course CS 31 taught by Professor Melkanoff during the Winter '00 term at UCLA.

Page1 / 31

lecture5%2Dpost - Wednesday,October20th ReviewChallenge...

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

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