WPU CS 2300 - Using C++ Arithmetic Operators and Control Structures

Unformatted text preview:

Using C++ Arithmetic Operators and Control StructuresObjectivesSlide 3C++ Binary Arithmetic OperatorsSlide 5Slide 6Slide 7Slide 8Shortcut Arithmetic OperatorsSlide 10Slide 11Slide 12Slide 13Output of Numberdemo2.cppEvaluating Boolean ExpressionsSlide 16SelectionThe if StatementSome Sample Selection Statements within a C++ ProgramSlide 20Slide 21Slide 22Slide 23Multiple Executable Statement in an if-elseSlide 25The switch StatementUsing the switch StatementSlide 28The if OperatorLogical AND and Logical ORSlide 31Slide 32Using the Logical ORSlide 34Slide 35A Typical Run of the Decisions.cpp ProgramThe while LoopSlide 38Slide 39Slide 40Slide 41The for StatementSlide 43Using Control Structures with Class Object FieldsA Program that Instantiates and Uses a BaseballPlayer ObjectSlide 46SummarySlide 48Using C++ Arithmetic Operators and Control StructuresObject-Oriented Programming Using C++Second Edition2Objectives•In this chapter, you will learn:•About C++ arithmetic operators•About shortcut arithmetic operators•How to evaluate boolean expressions•How to use the if and if-else statements•How to use the switch statement2Objectives•In this chapter, you will learn:•How to use the conditional operator•How to use the logical AND and the logical OR•How to use the while loop to repeat statements•How to use the for statement•How to use control structures with class object fields2C++ Binary Arithmetic Operators•Often after data values are input, you perform calculations with them•C++ provides five simple arithmetic operators for creating arithmetic expressions:–addition (+) – subtraction (-)–multiplication (*)– division (/)–modulus (%)•Each of these arithmetic operators is a binary operator; each takes two operands, one on each side of the operator, as in 12 + 9 or 16.2*1.5•The results of an arithmetic operation can be stored in memory2C++ Binary Arithmetic Operators2C++ Binary Arithmetic Operators•In Figure 2-2, each operation is assigned to a result variable of the correct type•The expression a + b has an integer result because both a and b are integers, not because their sum is stored in the intResult variable•If the program contained the statement doubleResult = a+b; the expression a+b would still have an integer value, but the value would be cast, or transformed, into a double when the sum is assigned to doubleResult2C++ Binary Arithmetic Operators•The automatic cast that occurs when you assign a value of one type to another is called an implicit cast•The modulus operator (%), which gives the remainder of integer division, can be used only with integers•When more than one arithmetic operator is included in an expression, then multiplication, division, and modulus operations always occur before addition or subtraction•Multiplication, division, and modulus are said to have higher precedence2C++ Binary Arithmetic Operators•You create a program that demonstrates some arithmetic operators used in C++ by following the instructions on pages 38 and 39 of the textbook2Shortcut Arithmetic Operators•C++ employs several shortcut operators•When you add two variable values and store the result in a third variable, the expression takes the form result= firstValue + secondValue•When you use an expression like this, both firstValue and secondValue retain their original values; only the result is altered•When you want to increase a value, the expression takes the form firstValue = firstValue + secondValue2Shortcut Arithmetic Operators•C++ provides the -= operator for subtracting one value from another, the *= operator for multiplying one value by another, and the /= operator for dividing one value by another•As with the += operator, you must not insert a space within the subtraction, multiplication, or division shortcut operators•The options shown in Figure 2-4 means replace the current value of count with the value that is 1 more than count, or simply increment count2Shortcut Arithmetic Operators•As you might expect, you can use two minus signs (--) before or after a variable to decrement it2Shortcut Arithmetic Operators•The prefix and postfix increment and decrement operators are examples of unary operators•Unary operators are those that require only one operand, such as num in the expression ++num•When an expression includes a prefix operator, the mathematical operation takes place before the expression is evaluated•When an expression includes a postfix operator, the mathematical operation takes place after the expression is evaluated2Shortcut Arithmetic Operators•The difference between the results produced by the prefix and postfix operators can be subtle, but the outcome of a program can vary greatly depending on which increment operator you use in an expression•Use the steps on pages 41 and 42 of the textbook to add increment operator statements to the Numberdemo.cpp program, so that you can become comfortable with the differences between prefix and postfix operators2Output of Numberdemo2.cpp2Evaluating Boolean Expressions•A boolean expression is one that evaluates as true or false•All false relational expressions are evaluated as 0•Thus, an expression such as 2>9 has the value 0•You can prove that 2>9 is evaluated as 0 by entering the statement code <<(2>9); into a C++ program•A 0 appears on output•All true relational expressions are evaluated as 1 •Thus, the expression 9>2 has the value 12Evaluating Boolean Expressions•The unary operator ! Means not, and essentially reverses the true/false value of an expression2Selection•Computer programs seem smart because of their ability to use selections or make decisions •C++ lets you perform selections in a number of ways:–The if statement–The switch statement–The if operator–Logical AND and Logical OR2The if Statement•Computer programs use the selection structure to choose one of two possible courses of action•The primary C++ selection structure statement is an if statement•The single-alternative if takes the form:if (boolean expression)statement;•If the expression in the parentheses is true, then the statement following the if executes•Do not inadvertently insert a semicolon prior to the end of the if statement2Some Sample Selection Statements within a C++ Program2The if Statement•If the execution of more than one statement depends on the selection, then the statements must be blocked with curly braces as shown in the code segment


View Full Document

WPU CS 2300 - Using C++ Arithmetic Operators and Control Structures

Download Using C++ Arithmetic Operators and Control Structures
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Using C++ Arithmetic Operators and Control Structures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Using C++ Arithmetic Operators and Control Structures 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?