Programming Fundamentals
Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout
Today’s Lecture Expressions Boolean Data Type Data Type Conversion Operators
Expressions Any arrangement of variables, constants, and operators that specifies a computation is called an expression. For example, alpha+12 and (alpha-37)*beta/2 are expressions. When the computations specified in the expression are performed, the result is usually a value. Thus if alpha is 7,the first expression shown has the value 19.
Precedence () ^ *, / +, - Example expression: 2 * 4 / 4 + (6 + 6 / 3) ans???
Example containing Expression
OUTPUT
The using Directive A namespace is a part of the program in which certain names are recognized; outside of the namespace they’re unknown. The directive says that all the program statements that follow are within the std namespace.
The using Directive Various program components such as cout are declared within this namespace. We can use it the other way, For example like this without using directive
Namespace Example
Type bool Variables of type bool can have only two possible values: true and false Type bool is most commonly used to hold the results of comparisons. For example: Is var1 less than var2? If so, a bool value is given the value true; If not, it’s given the value false.
Example
Variable Type Summary
Unsigned Data Types
Type Conversion
Automatic Conversions
When two operands of different types are encountered in the same expression, the lower-type variable is converted to the type of the higher-type variable.
Automatic Conversions In our example, the int value of count is converted to type float and stored in a temporary variable before being multiplied by the float variable avgWeight. The result (still of type float) is then converted to double so that it can be assigned to the double variable totalWeight.
Process of coversion 4 bytes These conversions take place invisibly
Casts In C++ Cast applies to data conversions specified by the programmer, as opposed to the automatic data conversions. Casts are also called type casts. What are casts for?
Casts Sometimes a programmer needs to convert a value from one type to another in a situation where the compiler will not do it automatically or without complaining. Use the following casting operator: static_cast (expression)
Example
The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where n is the argument to setw(n). The value is right justified within the field.
Example
OUTPUT
Example code
OUTPUT
Field width and setw Name:MadihaLiaqat Reg#:09-SE-99 Name:MadihaLiaqt Reg#:09-SE-99 Without setw manipulator
OUTPUT ??
The Remainder Operator This operator (also called the modulus operator) finds the remainder when one number is divided by another.
Arithmetic Assignment Operators
Increment Operators The ++ operator increments (adds 1 to) its argument. Normal way: Using arithmetic assignment operator Using Increment operator
Prefix and Postfix the increment operator can be used in two ways: Prefix meaning that the operator precedes the variable Postfix meaning that the operator follows the variable Example See next slide
The Decrement (--) Operator The decrement operator, --, behaves very much like the increment operator, except that it subtracts 1 from its operand. It too can be used in both prefix and postfix forms. Example count-- --count
OUTPUT ????
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as char, int, and float or they can be user- defined classes. The comparison involves such relationships as equal to, less than, and greater than. The result of the comparison is true or false; for example, either two values are equal (true), or they’re not (false).
Complete list of C++ relational operators
Example
OUTPUT
Example Expressions
Quiz
Assuming var1 starts with the value 20, what will the following code fragment print out? cout << var1--; cout << ++var1;
Questions????