Sei sulla pagina 1di 16

1

Outline

CS 101- Introductinon to
Computing
Zawar Hussain

 2000 Prentice Hall, Inc. All rights reserved.


2

Variables
• Variable names
– Correspond to locations in the computer's memory
– Every variable has a name, a type, a size and a value
• int myVariable = 10; 4 bytes
– Whenever a new value is placed into a variable, it replaces
the previous value - it is destroyed Value Size ?
Type Name
– Reading variables from memory does not change them
• A visual representation

integer1 45

 2000 Prentice Hall, Inc. All rights reserved.


3

Variables

•A visual representation (continued)

integer1 45
integer1 45

integer2 72
integer2 72

sum 117

 2000 Prentice Hall, Inc. All rights reserved.


4

Variables
• Variables
– Location in memory where a value can be stored for use by a
program
– Must be declared with a name and a data type before they
can be used
– Some common data types are:
• int - integer numbers
• char - characters
• double - floating point numbers
– Example: int myvariable;
• Declares a variable named myvariable of type int
– Example: int variable1, variable2;
• Declares two variables, each of type int

 2000 Prentice Hall, Inc. All rights reserved.


5
1.20 Another Simple Program:
Adding Two Integers
• >> (stream extraction operator)
– When used with std::cin, waits for the user to input a
value and stores the value in the variable to the right of the
operator
– The user types a value, then presses the Enter (Return) key
to send the data to the computer
– Example:
int myVariable;
std::cin >> myVariable;
• Waits for user input, then stores input in myVariable
• = (assignment operator)
– Assigns value to a variable
– Binary operator (has two operands)
– Example:
sum = variable1 + variable2;
 2000 Prentice Hall, Inc. All rights reserved.
1 // Fig. 1.6: fig01_06.cpp 6
2 // Addition program Outline
3 #include <iostream>
4 1. Load <iostream>
5 int main()
6 { 2. main
7 int integer1, integer2, sum; // declaration
2.1 Initialize variables
8 integer1,
9 std::cout << "Enter first integer\n"; // prompt integer2,
Notice how std::cin is used to get userand sum
10 std::cin >> integer1; // read an integer
input. 2.2 Print "Enter
11 std::cout << "Enter second integer\n"; // prompt
12 std::cin >> integer2; // read an integer first integer"
13 sum = integer1 + integer2; // assignment of sum 2.2.1 Get input
14 std::cout << "Sum is " << sum << std::endl; // print sum
2.3 Print "Enter
15
second integer"
16 return 0; // std::endl flushes the buffer and
indicate that program ended successfully
2.3.1 Get input
17 } prints a newline.
2.4 Add variables and
Enter first integer put result into sum
45 Variables can be output using std::cout << variableName.
Enter second integer 2.5 Print "Sum is"
72
Sum is 117 2.5.1 Output sum
2.6 exit (return 0)

Program Output

 2000 Prentice Hall, Inc. All rights reserved.


7

1.22 Arithmetic
• Arithmetic calculations
– Use * for multiplication and / for division
– Integer division truncates remainder
• 7 / 5 evaluates to 1
– Modulus operator returns the remainder
• 7 % 5 evaluates to 2
• Operator precedence
– Some arithmetic operators act before others (i.e.,
multiplication before addition)
• Be sure to use parenthesis when needed
– Example: Find the average of three variables a, b and c
• Do not use: a + b + c / 3
• Use: (a + b + c ) / 3

 2000 Prentice Hall, Inc. All rights reserved.


8

1.22 Arithmetic
• Arithmetic operators:
C++ op era tion Arithmetic Alg eb ra ic C++ exp ression
op era tor exp ression
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y

Modulus % r mod s r % s

• Rules of operator precedence:


Operator(s) Operation(s) Order of evaluation (precedence)

() Parentheses Evaluated first. If the parentheses are nested, the


expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same level”
(i.e., not nested), they are evaluated left to right.
*, /, or % Multiplication Division Evaluated second. If there are several, they re
Modulus evaluated left to right.
+ or - Addition Evaluated last. If there are several, they are
Subtraction evaluated left to right.

 2000 Prentice Hall, Inc. All rights reserved.


9

Arithmetic Example
Step 1. y = 2 * 5 * 5 + 3 * 5 + 7; (Leftmost multiplication)
2 * 5 is 10

Step 2. y = 10 * 5 + 3 * 5 + 7; (Leftmost multiplication)


10 * 5 is 50

Step 3. y = 50 + 3 * 5 + 7; (Multiplication before addition)


3 * 5 is 15

Step 4. y = 50 + 15 + 7; (Leftmost addition)

50 + 15 is 65

Step 5. y = 65 + 7; (Last addition)


65 + 7 is 72

Step 6. y = 72; (Last operation—place 72 in y )

 2000 Prentice Hall, Inc. All rights reserved.


10
1.23 Decision Making: Equality and
Relational Operators
• using statements
– Eliminate the need to use the std:: prefix
– Allow us to write cout instead of std::cout
– To use the following functions without the std:: prefix,
write the following at the top of the program
using std::cout;
using std::cin;
using std::endl;

 2000 Prentice Hall, Inc. All rights reserved.


11
1.23 Decision Making: Equality and
Relational Operators
• if structure
– Test conditions truth or falsity. If condition met execute,
otherwise ignore
• Equality and relational operators
– Lower precedence than arithmetic operators

• Table of relational operators on next slide

 2000 Prentice Hall, Inc. All rights reserved.


12
1.23 Decision Making: Equality and
Relational Operators

Sta nd a rd a lg eb ra ic C++ eq ua lity Exa m p le Mea ning of


eq ua lity op era tor or or rela tiona l of C++ C++ c ond ition
rela tiona l op era tor op era tor c ond ition

Relational operators
> > x > y x is greater than y
< < x < y x is less than y

 >= x >= y x is greater than or equal to y

 <= x <= y x is less than or equal to y

Equality operators
= == x == y x is equal to y

 != x != y x is not equal to y

 2000 Prentice Hall, Inc. All rights reserved.


Conditional Operations

If <condition> then
operations for the then-part
Else
operations for the else-part

1. Evaluate <condition> expression to see


whether it is true or false.
2. If true, then execute operations in then-part
3. Otherwise, execute operations in else-part.
 2000 Prentice Hall, Inc. All rights reserved.
Conditions, or Boolean Expressions

• A condition is one whose value is true or false, for


example:
 3>2 is greater than (true)
 3=2 is equal to (false)
 A>2 is true if A’s value is greater
than 2 (at the time this is
executed), false otherwise.

 2000 Prentice Hall, Inc. All rights reserved.


Conditions may be compounded
E1 or E2
true if at least one of them is true; false otherwise.
E.g. 3 > 2 or 2 > 3 is true

E1 and E2
true if both are true; false otherwise
E.g. 3 > 2 and 2 > 3 is false

not E
true if E is false, false if E is true

 2000 Prentice Hall, Inc. All rights reserved.


Example
1. Get a value for A
2. If A = 0 then
3. Print the message, “The input is zero”
Else
4. Print the message, “The input is not zero”

1. Get a value for grade


2. If grade < 1 or grade > 9 then
3. Print the message, “Invalid grade”
Else
4. Set the value of total to (grade + total)

 2000 Prentice Hall, Inc. All rights reserved.

Potrebbero piacerti anche