Another Simple Program: Adding Two Integers
Another Simple Program: Adding Two Integers This program uses the input stream object std::cin and the stream extrac<on operator, >>, to obtain two integers typed by a user at the keyboard, computes the sum of these values and outputs the result using std::cout. Note: << is called inser<on operator
Another Simple Program: Adding Two Variable declara0on: Integers number1, number2, sum are the names of variables A variable is a loca<on in the computer s memory where a value can be stored for use by a program. These declara<ons specify that the variables number1, number2, and sum are data of type int, meaning that these variables will hold integer values, i.e., whole numbers such as 7, 11, 0 and 31,914. All variables must be declared with a name and a data type before they can be used in a program. Several variables of the same type may be declared in one declara<on or in mul<ple declara<ons. We could have declared all three variables in one declara<on by using a comma-separated list as follows: int number1, number2, sum;!
Another Simple Program: Adding Two Integers Variable s name A series of characters consis<ng of leqers, digits and underscores ( _ ). Does NOT begin with a number. C++ is case sensi<ve i.e., uppercase and lowercase leqers are different, so a1 and A1 represent different variables. Must NOT be a special character e.g., &, %, $, Must NOT be a keyword. (What is a keyword?)
Another Simple Program: Adding Two Integers Variable s types We ll soon discuss the type float for specifying decimal numbers, and the type char for specifying character data. Decimal numbers are numbers with decimal points, such as 3.4, 0.0 and 11.19. A char variable may hold only a single lowercase leqer, a single uppercase leqer, a single digit or a single special character (e.g., $ or *). Types such as int, float, and char are called fundamental types. Fundamental-type names are keywords.
Another Simple Program: Adding Two Integers Placement of Variable Declara0ons Declara<ons of variables can be placed almost anywhere in a program, but they must appear before their corresponding variables are used in the program.
Another Simple Program: Adding Two Integers Obtaining the First Value from the User displays Enter first integer: followed by a space. This message is called a prompt because it directs the user to take a specific ac<on. uses the standard input stream object cin (of namespace std) and the stream extrac<on operator, >>, to obtain a value from the keyboard. Using the stream extrac<on operator with std::cin takes character input from the standard input stream, which is usually the keyboard.
Another Simple Program: Adding Two Integers Obtaining the Second Value from the User
Another Simple Program: Adding Two Integers Calcula0ng the Sum of the Values Input by the User This assignment statement adds the values of variables number1 and number2 and assigns the result to variable sum using the assignment operator =
Another Simple Program: Adding Two Displaying the Result Integers displays the character string Sum is followed by the numerical value of variable sum followed by std::endl a so-called stream manipulator. The name endl is an abbrevia<on for end line and belongs to namespace std. The std::endl stream manipulator outputs a newline.
Memory Concepts Variable names such as number1, number2 and sum actually correspond to loca<ons in the computer s memory. Every variable has a name, a type, a size and a value.
Arithme<c C++ provides the modulus operator, %, that yields the remainder amer integer division. The modulus operator can be used only with integer operands. The expression x % y yields the remainder amer x is divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2.
Examples, Arithme<c
Arithme<c Rules of Operator Precedence C++ applies the operators in arithme<c expressions in a precise order determined by these rules of operator precedence, which are generally the same as those in algebra: 1. Operators in expressions contained with in pairs of parentheses are evaluated first. Parentheses are said to be at the highest level of precedence. In cases of nested, or embedded, parentheses, such as (a*(b+c)), the operators in the innermost pair of parentheses are applied first. 2. Mul0plica0on, division, and modulus opera<ons are applied next. If an expression contains several mul<plica<on, division and modulus opera<ons, operators are applied from lem to right. 3. Addi0on and subtrac0on opera<ons are applied last. If an expression contains several addi<on and subtrac<on opera<ons, operators are applied from lem to right.
Examples Arithme<c 1: 2 + (2-2) * (2 + 2) / 2 =? 2: 3 + 3-3 * (3 / 3) =? 3: (4 + 4 4 * 4) / ( (4 + 4) * -1) =? 4: (22 % 4 1) / 2 + 1 / 2 =?
Modify the Program Write a program that takes three integers from the user and outputs the following opera<on:
Decision making (Equality and Rela<onal Operators) The if statement allows a program to take alterna<ve ac<on based on whether a condi<on is true or false. If the condi<on is true, the statement in the body of the if statement is executed. If the condi<on is false, the body statement is not executed. Condi<ons in if statements can be formed by using the equality operators and rela<onal operators.
Decision making (Equality and If statement Rela<onal Operators) if (condi<on){ } //do something
Decision making (Equality and Rela<onal Operators)
Decision making (Equality and Rela<onal Operators) Let s write a program that compares two integers!
Decision making (Equality and Rela<onal Operators) using namesapce std! is a direc<ve that enables a program to use all names in any standard C++ header such as <iostream>
Lab Assignment #1