create new tag
view all tags

Notes for Day 4

Notes on Section 2.3


  • Character, Boolean, String


  • The variable types we’ve seen so far are int and double. To this we will add char, bool, and string.
  • Using strings requires the following library command: #include. This is because the other types of variables are basic/core C++ variables, but strings are not.
  • To assign the value n to the character variable response, use response = ‘n’;
  • To assign the value true to the boolean variable finished, use finished = true;
  • To assign the value “Vladimir” to the string variable name, use name = "Vladimir";
  • For variables of type double, the arithmetic operators +, -, *, and / work as usual.
  • For variables of type int, the arithmetic operators +, -, and * work as usual.
  • For variables of type int, the arithmetic operator / produces a whole number solution.
  • For variables of type int, the arithmetic operator % produces the remainder after division.
  • Parentheses should be used to disambiguate arithmetic expressions. (E.g. (a+b)/(c+d) to divide a+b by c+d.)

Take Note

  • Take care when mixing variable types in assignments or comparisons.
  • Mixing variables of types int and double in arithmetic causes all numbers to be treated as type double.
  • Remember: 7/2 = = 3, and 14%3 = = 2.
  • You may include a space in a string, as in name = "Vladimir Francisco", but if you are taking input from the keyboard, the first space or new line will indicate the end of the string input. So if the command cin << name; got the input Vladimir Francisco from the keyboard, only the Vladimir would be assigned to the variable name.
  • We’ll learn much more about strings in Chapter 8 next month. Don’t try to do anything fancy with them for the moment.

notes on Section 2.4


Boolean Expression

Take Note

  • = (e.g. x+7 = y)
  • = (e.g. ans = 'n')
  • Also <, <=, >, >=
  • (x+7 = y) && (ans 'n') means that both conditions hold
  • (x+7 = y) || (ans 'n') means that either one condition or the other or both hold
  • !([expression]) means that the expression is false, not that it is true
  • Beware of if(x<y<z)…. This will produce a syntax error
  • Beware of if(x=12)…. This is even more dangerous, since it will not produce a syntax error. This sets the value of x to be 12, and returns “true”.
Topic revision: r1 - 2015-09-07 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback