{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture5 - Wednesday,October20th ReviewChallenge...

Info icon This 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
Image of page 1

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

View Full Document Right 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”; }
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
Image of page 3

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

View Full Document Right 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!”; }
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!
Image of page 5

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

View Full Document Right 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…
Image of page 6
Short Circuiting if ( cute == true  ||  smart == true  ||  rich == true )     cout << “Hey, want to go on a date?”;       This is called  short-circuiting .   In such an if statement, C++ will evaluate the expressions from  left-to-right…   The moment C++ finds a condition that satisfies the if  statement, it will skip the rest of the cases and continue with  the next statement.
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern