1 / 53

Programming Fundamentals

Programming Fundamentals. 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).

farhani
Télécharger la présentation

Programming Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ProgrammingFundamentals

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

  3. OUTPUT

  4. Example code

  5. OUTPUT

  6. Field width and setw Without setw manipulator

  7. OUTPUT

  8. Variable Type Summary

  9. Unsigned Data Types

  10. Type Conversion

  11. Automatic Conversions

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

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

  14. Process of coversion 4 bytes These conversions take place invisibly

  15. 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?

  16. 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. • We have four specific casting operators: dynamic_cast <new_type> (expression)reinterpret_cast <new_type> (expression)static_cast <new_type> (expression)const_cast <new_type> (expression)

  17. Example

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

  19. Arithmetic Assignment Operators

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

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

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

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

  24. Complete list of C++ relational operators

  25. Example

  26. OUTPUT

  27. Example Expressions

  28. Loops Loops cause a section of your program to be repeated a certain number of times. The repetition continues while a condition is true a certain number of times. When the condition becomes false, the loop ends and control passes to the statements following the loop.

  29. Kinds of Loop • There are three kinds of loops in C++: • for loop, • while loop, • do loop.

  30. The for Loop The for loop executes a section of code a fixed number of times. It’s usually (although not always) used when you know, before entering the loop, how many times you want to execute the code.

  31. Example

  32. OUTPUT

  33. Example

  34. OUTPUT

  35. How does this work?

  36. Explanations The for statement controls the loop. It consists of the keyword for, followed by parentheses that contain three expressions separated by semicolons:

  37. These three expressions are the initialization expression, the test expression,and the increment expression. These three expressions usually (but not always) involve the same variable, which we call the loop variable.

  38. Explanation The body of the loop is the code to be executed each time through the loop. In our example the loop body consists of a single statement:

  39. Contdd. . . . This statement prints out the square of j, followed by two spaces. The square is found by multiplying j by itself. As the loop executes, j goes through the sequence 0, 1, 2, 3, and so on up to 14; so the squares of these numbers are displayed—0, 1, 4, 9, up to 196.

  40. The Initialization Expression The initialization expression is executed only once, when the loop first starts. It gives the loop variable an initial value. In our example it sets j to 0.

  41. The Test Expression The test expression usually involves a relational operator. It is evaluated each time through the loop, just before the body of the loop is executed. It determines whether the loop will be executed again. If the test expression is true, the loop is executed one more time. If it’s false, then loop ends, and control passes to the statements following the loop.

  42. Contdd. . . . In our example the statement is executed following the completion of the loop.

  43. Increment /Decrement Expression • The increment expression changes the value of the loop variable, often by incrementing it. • It is always executed at the end of the loop, after the loop body has been executed. • How Many Times? • In the figure shown in previous slides, • The first time j=0, This is ensured in the initialization expression. • The last time through the loop, j is 14. This is determined by the test expression j<15.

  44. Contdd. . . . • When j becomes 15, the loop terminates; the loop body is not executed when j has this value. • Next slide shows a flow chart for loop operation. • The arrangement shown is commonly used to do something a fixed number of times: • start at 0, • use a test expression with the less-than operator and a value equal to the desired number of iterations • increment the loop variable after each iteration.

More Related