Sunteți pe pagina 1din 15

Outline

CS 101- Introductinon to
Computing

Zawar Hussain

2000 Prentice Hall, Inc. All rights reserved.

1.20 Another Simple Program:


Adding Two Integers
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.

Primitive Data types


Name

Description

Size

Range

char

Character or small
integer

1 byte

signed: -128 to 127


unsigned: 0 to 255

short int (short)

Short Integer

2 bytes

signed: -32768 to 32767


unsigned: 0 to 65535

Int

Integer

4 bytes

signed: -2147483648 to 2147483647


unsigned: 0 to 4294967295

iong int (long)

Long integer

4 bytes

signed: -2147483648 to 2147483647


unsigned: 0 to 4294967295

bool

Boolean value. It can


take one of two values:
true or false

1 byte

true or false

float

Floating point number

4 bytes

+/- 3.4e +/- 38 (~7 digits)


(24 bit coefficient, 8 bit exponent)

double

Double precision
floating point number

8 bytes

+/- 1.7e +/- 308 (~15 digits)


(53 bit coefficient, 11 bit exponent)

long double

Long double precision


floating point number

16 bytes

+/- 1.2e +/- 4932 (~19 digits)

2000 Prentice Hall, Inc. All rights reserved.

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.

// Fig. 1.6: fig01_06.cpp

// Addition program

#include <iostream>

Outline
1. Load <iostream>

4
5

int main()

2. main
2.1 Initialize variables
integer1,
prompt
integer2,
Notice how std::cin is used
to get userand sum

int integer1, integer2, sum;

// declaration

std::cout << "Enter first integer\n";

//

10

std::cin >> integer1;

// read an integer

11

std::cout << "Enter second integer\n"; // prompt

12

std::cin >> integer2;

// read an integer

13

sum = integer1 + integer2;

// assignment of sum

14

std::cout << "Sum is " << sum << std::endl; // print sum

8
9

15
16

return 0;

//

17 }

Enter first integer


45
Enter second integer
72
Sum is 117

input.

2.2 Print "Enter


first integer"
2.2.1 Get input

2.3 Print "Enter


second integer"
indicate that program ended successfully
std::endl flushes the buffer and
2.3.1 Get input
prints a newline.
2.4 Add variables and
put result into sum
Variables can be output using std::cout << variableName.
2.5 Print "Sum is"
2.5.1 Output sum
2.6 exit (return 0)

Program Output
2000 Prentice Hall, Inc. All rights reserved.

1.21 Memory Concepts


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 Type
previous value - it Name
is destroyed
Value
Size
Reading variables from memory does not change them

A visual representation

integer1

2000 Prentice Hall, Inc. All rights reserved.

45

1.21 Memory Concepts


A visual representation (continued)

integer1

integer2

integer1

45

integer2

72

45

72

sum

2000 Prentice Hall, Inc. All rights reserved.

117

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.

1.22

Arithmetic

Arithmetic operators:
C++ operation Arithmetic
operator
+
Addition

Algebraic
expression
f+7

C++ expression

Subtraction
Multiplication

pc
bm

p - c
b * m

Division

x/y

x / y

Modulus

rmods

r % s

f + 7

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
Subtraction

2000 Prentice Hall, Inc. All rights reserved.

Evaluated last. If there are several, they are


evaluated left to right.

10

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;

2000 Prentice Hall, Inc. All rights reserved.

(Last operationplace 72 in y )

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
Standard algebraic
equality operator or
relational operator

C++ equality
or relational
operator

Example
of C++
condition

Meaning of
C++ condition

Relationaloperators

>

>

x>y

xisgreaterthany

<

<

x<y

xislessthany

>=

x>=y

xisgreaterthanorequaltoy

<=

x<=y

xislessthanorequaltoy

Equalityoperators

==

x==y

xisequaltoy

!=

x!=y

xisnotequaltoy

2000 Prentice Hall, Inc. All rights reserved.

13

14

2.6

The if/else Selection Structure


false

grade >= 60

print Failed

C++ code

if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";

2000 Prentice Hall, Inc. All rights reserved.

true
print Passed

15

2.6

The if/else Selection Structure


false

grade >= 60

print Failed

true
print Passed

Ternary conditional operator (?:)


Takes three arguments (condition, value if true, value if false)

Our pseudocode could be written:

cout << ( grade >= 60 ? Passed : Failed );

2000 Prentice Hall, Inc. All rights reserved.

S-ar putea să vă placă și