Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.

Similar presentations


Presentation on theme: "Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout."— Presentation transcript:

1 Programming Fundamentals

2 Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout

3 Today’s Lecture Expressions Boolean Data Type Data Type Conversion Operators

4 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.

5 Precedence () ^ *, / +, - Example expression: 2 * 4 / 4 + (6 + 6 / 3) ans???

6 Example containing Expression

7 OUTPUT

8 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.

9 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

10 Namespace Example

11 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.

12 Example

13 Variable Type Summary

14 Unsigned Data Types

15 Type Conversion

16 Automatic Conversions

17 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.

18 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.

19 Process of coversion 4 bytes These conversions take place invisibly

20 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?

21 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)

22 Example

23

24 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.

25 Example

26 OUTPUT

27 Example code

28 OUTPUT

29 Field width and setw Name:MadihaLiaqat Reg#:09-SE-99 Name:MadihaLiaqt Reg#:09-SE-99 Without setw manipulator

30 OUTPUT ??

31 The Remainder Operator This operator (also called the modulus operator) finds the remainder when one number is divided by another.

32 Arithmetic Assignment Operators

33

34 Increment Operators The ++ operator increments (adds 1 to) its argument. Normal way: Using arithmetic assignment operator Using Increment operator

35 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

36

37

38 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

39 OUTPUT ????

40 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).

41 Complete list of C++ relational operators

42 Example

43 OUTPUT

44 Example Expressions

45 Quiz

46 Assuming var1 starts with the value 20, what will the following code fragment print out? cout << var1--; cout << ++var1;

47 Questions????


Download ppt "Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout."

Similar presentations


Ads by Google