cs31 lecture 4

Called inferred x examples int foo 2 float bar 32

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: port all basic types as rhs float, double, int, char, … Also with qualifier x The user doesn’t need to specify the type of the rhs, it is The inferred by the compiler and the good << operator is called inferred x Examples: int foo = 2; float bar = 3.2; std::cout << “foo is: “ << foo << “ and bar is: “ << bar << std::endl; std::cout 11 Some observations x Variables and more generally expressions can be printed Variables out out x For floating point values, not all digits are printed out There are commands to set the precision It doesn’t mean the number has been truncated x For complex, user-defined types, must define the operator For << << 12 Console input x Useful to get some input from the user, through the Useful keyboard (usually) keyboard x Principle: “push” the standard input into a variable x Example: int foo; std::cin >> foo; float bar; float std::cin >> bar; std::cin 13 Some observations x “Conversion” happens when the input does not match the Conversion” type type Example: int foo; std::cin >> foo; std::cout << foo << std::endl; Giving 123.45 as input on the keyboard leads to printing 123 x If the input is “meaningless”, then no assignment is If performed performed Example: input “foo” to the above program leaves the value of foo Example: unchanged unchanged 14 Complete example 15 Control structure: if-then-else x Syntax in C++: IIf (<condition>) f <instructions1> <instructions1> [else [else <instructions2>] <instructions2>] x Instructions are usually a block, that is { … } x <condition> is an expression When it evaluates to 0 or ‘false’, then <instructions1> is not When executed, and if there is a else, then <instructions2> is executed executed, When it evaluates to any value not 0, or ‘true’, then <instructions1> When is executed is 16 More on conditions x Can use standard operators for comparison of values < or > or <= or >= or == Be careful: = is assignment, == is comparison 12 < 12 is a false expression 12 < 13 is a true expression 12 is a non-0 expression (hence “true”) x Conjunction/disjunction can be expressed with logical Conjunction/disjunction operators operators && for logical and || for logical or 17 Complete example 2 18...
View Full Document

Ask a homework question - tutors are online