SlideShare una empresa de Scribd logo
1 de 141
Descargar para leer sin conexión
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 2
C++ Basics
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 3
Overview
2.1 Variables and Assignments
2.2 Input and Output
2.3 Data Types and Expressions
2.4 Simple Flow of Control
2.5 Program Style
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
2.1
Variables and Assignments
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 Variables are like small blackboards
 We can write a number on them
 We can change the number
 We can erase the number
 C++ variables are names for memory locations
 We can write a value in them
 We can change the value stored there
 We cannot erase the memory location

Some value is always there
Slide 2- 5
Display 2.1
Variables and Assignments
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.1 (1/2)
Slide 2- 6
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 7
Identifiers
 Variables names are called identifiers
 Choosing variable names
 Use meaningful names that represent data to
be stored
 First character must be

a letter

the underscore character
 Remaining characters must be

letters

numbers

underscore character
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 8
Keywords
 Keywords (also called reserved words)
 Are used by the C++ language
 Must be used as they are defined in
the programming language
 Cannot be used as identifiers
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Declaring Variables (Part 1)
 Before use, variables must be declared
 Tells the compiler the type of data to store
Examples: int number_of_bars;
double one_weight, total_weight;
 int is an abbreviation for integer.

could store 3, 102, 3211, -456, etc.

number_of_bars is of type integer
 double represents numbers with a fractional
component

could store 1.34, 4.0, -345.6, etc.

one_weight and total_weight are both of type double
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 10
Declaring Variables (Part 2)
 Immediately prior to use
int main()
{
…
int sum;
sum = score1 + score 2;
…
return 0;
}
 At the beginning
int main()
{
int sum;
…
sum = score1 +
score2;
…
return 0;
}
Two locations for variable declarations
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 11
Declaring Variables (Part 3)
 Declaration syntax:
 Type_name Variable_1 , Variable_2, . . . ;
 Declaration Examples:
 double average, m_score, total_score;
 double moon_distance;
 int age, num_students;
 int cars_waiting;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 12
Assignment Statements
 An assignment statement changes the value of a variable
 total_weight = one_weight + number_of_bars;

total_weight is set to the sum one_weight + number_of_bars
 Assignment statements end with a semi-colon
 The single variable to be changed is always on the left
of the assignment operator ‘=‘
 On the right of the assignment operator can be

Constants -- age = 21;

Variables -- my_cost = your_cost;

Expressions -- circumference = diameter * 3.14159;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 13
Assignment Statements and Algebra
 The ‘=‘ operator in C++ is not an equal sign
 The following statement cannot be true in
algebra

number_of_bars = number_of_bars + 3;
 In C++ it means the new value of
number_of_bars
is the previous value of number_of_bars plus 3
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 14
Initializing Variables
 Declaring a variable does not give it a value
 Giving a variable its first value is initializing the variable
 Variables are initialized in assignment statements
double mpg; // declare the variable
mpg = 26.3; // initialize the variable
 Declaration and initialization can be combined
using two methods
 Method 1
double mpg = 26.3, area = 0.0 , volume;
 Method 2
double mpg(26.3), area(0.0), volume;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 15
Section 2.1 Conclusion
 Can you
 Declare and initialize two integers variables to zero?
The variables are named feet and inches.
 Declare and initialize two variables, one int and one
double?
Both should be initialized to the appropriate form of 5.
 Give good variable names for identifiers to store

the speed of an automobile?

an hourly pay rate?

the highest score on an exam?
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
2.2
Input and Output
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 # include <iostream.h>
 int main()
 {
 cout << "Hii C++ " ;
 return 0;
 }
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
• /// add two numbers
• # include <iostream.h>
• main()
• {
• int num1 , num2;
• cout << "the first number:n " ;
• cin >> num1;
• cout << " the second number:n";
• cin >> num2;
• cout << "the Value is: " << num1+num2;
• return 0;
• }
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
• //const int number=14 ;
• //Arithmetic Operations
• // + - * / %
• //Relation Operator < <= > >= ==
• // i=i+2; = i+=2 ;
• //Conditional Expressions
• //if (a > b )
• max = a ;
• if ( b < a)
• max = b ;
• if ( b == a)
• max = a = b;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Increment and Decrement Operators
• Increment and Decrement Operators
• a = ++b ; if b=6
• b=a=7
• a = b ++ ;
• B=7
• A=6
• cout << "Hellow C++";
• cout << a ;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 cout << "Please: " << a << b << "Hellow" ;
 cout << "Hellow" << endl << "World" ;
 Output
 Hellow
 World
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 t
 . n
 . r
 a
 b
 using namespace std;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
comments
• int a=0 /* the compiler
• cannot read thie*/
• #include <iostream>
• using namespace std;
• int main()
• {
• cout << "Hellow Worldn I am a programmer " <<
endl;
• return 0;
• }
• out
• Hellow Worlad
• I am a programmer
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Reserved words in C++
• asm, auto, bool, break, case, catch, char, class, const,
• const_cast, continue, default, delete, do, double,
• dynamic_cast, else, enum, explicit, extern, false, float, for,
• friend, goto, if, inline, int, long, mutable, namespace, new,
• operator, private, protected, public, register,
• reinterpret_cast, return, short, signed, sizeof, static,
• static_cast, struct, switch, template, this, throw, true, try,
• typedef, typeid, typename, union, unsigned, using, virtual,
• void, volatile, wchar_t
• Logic operation:
• and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq,
• xor, xor_eq
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Variables
 a = 4 ;
 b = 1 ;
 a = a + 3 ;
 result = a + b
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
• #include<iostream.h>
• main()
• { int z,x;
• for(z=1;z<=5;z++)
• {
• cout<<"*n" ;
• for(x=0;x<=z-1;x++)
• cout<<" " ;
• }}
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
• // my second program in C++ Hello World! I'm a C++
program
• #include <iostream>
• using namespace std;
• int main ()
• {
• cout <<
• "Hello World! ";
• cout <<
• "I'm a C++ program";
• return 0;
• }
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
• // operating with variables 16
• #include <iostream>
• using namespace std;
• int main ()
• {
• // declaring variables:
• int a, b;
• int result;
• // process:
• a = 10;
• b = 4;
• a = a + 2;
• result = a + b;
• // print out the result:
• cout << result;
• // terminate the program:
• return 0;
• }
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
• // my first string
• #include <iostream>
• #include <string>
• using namespace std;
• int main ()
• {
• string name;
• cout<<"enter your name: ";
• cin>>name;
• cout << "Hello "<<name<<endl;
• return 0;}
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
• // my second string
• #include <iostream>
• #include <string>
• using namespace std;
• int main ()
• {
• string firstName,lastName,fullName;
• cout<<"enter your first name: ";
• cin>>firstName;
• cout<<"enter your last name: ";
• cin>>lastName;
• fullName=firstName+" "+lastName;
• cout<<"Hello "<<fullName<<endl;
• if(firstName<lastName)
• cout<<firstName<<endl;
• return 0;}
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 //write a program to output:- 12345678910
 #include <iostream>
 using namespace std;
 Int main()
 {
 int a ;
 for(a=1;a<=10;a++)
 cout<<a;
 cout<<endl;
 system("pause");
 return 0;
 }
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
convert the seconds to minutes and hours
 //write a program to like 00:00:00 :-
 #include <iostream>
 using namespace std;

 int main ()
 {
 int second,hours=3600,minutes=60;
 cout<<"Enter the seconds ";
 cin>> second;
 hours=second/hours;
 minutes=second/minutes;
 second=second%60;
 cout<<hours<<":"<<minutes%60<<":"<<second<<endl;
 return 0;
 }
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 //write a program to input 3 integers and determine which of them is biggest:-
 #include <iostream>
 using namespace std;

 int main ()
 {
 int a,b,c;
 cout<<"enter a , b and c one after another"<<endl;
 cin>>a>>b>>c;
 if ((a>b)&&(a>c)) //take care of (( )) important.
 cout<<"first number is bigger="<<a<<endl;
 if ((b>a)&&(b>c))
 cout<<"the second number is bigger="<<b<<endl;
 if ((c>a)&&(c>b))
 cout<<"the last number is bigger="<<c<<endl;
 if((a==c)&&(c==b))
 cout<<"all the numbers are equal"<<endl;
 return 0;
 }
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 //write a program to input 3 integers and determine which of them is biggest:-
 #include <iostream>
 using namespace std;

 int main ()
 {
 int a,b,c;
 cout<<"enter a , b and c one after another"<<endl;
 cin>>a>>b>>c;
 if ((a>b)&&(a>c)) //take care of (( )) important.
 cout<<"first number is bigger="<<a<<endl;
 if ((b>a)&&(b>c))
 cout<<"the second number is bigger="<<b<<endl;
 if ((c>a)&&(c>b))
 cout<<"the last number is bigger="<<c<<endl;
 if((a==c)&&(c==b))
 cout<<"all the numbers are equal"<<endl;
 return 0;
 }
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 /*/ //write a program to enter a letter and determine the letter is small or capital:-
 #include <iostream>
 using namespace std;

 int main ()
 {
 char letter;
 cout<<"Enter a letter:";
 cin >> letter;
 if(letter >= 'A')
 if(letter <= 'Z')
 cout<<"You entered a capital letter.";
 else
 cout<<"You entered a small letter.";
 system("pause");
 return 0;
 }
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 #include <iostream>
 using namespace std;

 int main ()
 {
 float a,b,c,x;
 cout<<"enter a , b and c one after another"<<endl;
 cin>>a>>b>>c;
 x=(3+4*a*b)/(2*c);
 cout<<"x="<<x<<endl;
 system("pause");
 return 0;
 }
 }
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Find error
#include <iostream>
using namespace std;
int main ()
{
float a = 1 , b = 0 ;
unsigned int result;
a = a - 6;
result = A + b;
cout << result;
}
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Printing value
• cout<< "Islam is a way of life" ;
• // print Islam is a way of life on screen
• cout<< 120 ;
• // print number 120 on screen
• cout<< x ;
• // print the conte
• cout<< "Hello" ; // Hellont of variable x on
screen
• cout<< Hello ; // Hello
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
• Cou t<< "The peace upon you, " << "I am " << "a C++ sentence. " ;
• The peace upon you, I am a C++ sentence.
• cout<<"I am "<< age << " years old and my zipcode is "<< zipcode ;
• cin >> a >> b ;
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".n";
return 0;
}
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Write program
 const float pi = 3.14159265 ;
 const char tab = 't' ;
 #define
 #define pi 3.14159265
 #define NewLine 'n'
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
#include<iostream>
using namespace std;
#define pi 3.14159265
int main()
{
float R ,r ;
r = 0 ;//initial to 0
cout<<"enter circle radius plz: ";
cin>> r ;
cout<< "circle area = "
<< 0.5 * pi * r * r << endl;
return 0 ;}
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
#include<iostream>
using namespace std;
const double pi = 3.14159265;
int main()
{
float r ;
r = 0 ;//initial to 0
cout<<"enter circle radius plz: ";
cin>> r ;
cout<< "circle area = "
<< 0.5 * pi * r * r << endl;
return 0 ;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 int a , b ; // a : ? b : ?
 a = 10 ; // a : 10 b : ?
 b = 4 ; // a : 10 b : 4
 a = b ; // a : 4 b : 4
 b = 7 ; // a : 4 b : 7
 a = 2 + ( b = 5 ) ; // 1) b = 5 ; 2) a = 2 + b ; 3) a =
7 ;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 b = 3;
 a = ++b;
 // a = 4 , b = 4
 Or
 b = 3;
 a = b++ ;
 // a = 3 , b = 4
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 45
Input and Output
 A data stream is a sequence of data
 Typically in the form of characters or numbers
 An input stream is data for the program to use
 Typically originates

at the keyboard

at a file
 An output stream is the program’s output
 Destination is typically

the monitor

a file
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 46
Output using cout
 cout is an output stream sending data to the monitor
 The insertion operator "<<" inserts data into cout
 Example:
cout << number_of_bars << " candy barsn";
 This line sends two items to the monitor

The value of number_of_bars

The quoted string of characters " candy barsn"
 Notice the space before the ‘c’ in candy
 The ‘n’ causes a new line to be started following the ‘s’ in bars

A new insertion operator is used for each item of output
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 47
Examples Using cout
 This produces the same result as the previous sample
cout << number_of_bars ;
cout << " candy barsn";
 Here arithmetic is performed in the cout statement
cout << "Total cost is $" << (price + tax);
 Quoted strings are enclosed in double quotes ("Walter")
 Don’t use two single quotes (')
 A blank space can also be inserted with
cout << " " ;
if there are no strings in which a space is desired as
in " candy barsn"
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 48
Include Directives
 Include Directives add library files to our programs
 To make the definitions of the cin and cout available to
the program:
#include <iostream>
 Using Directives include a collection of defined names
 To make the names cin and cout available to our program:
using namespace std;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 49
Escape Sequences
 Escape sequences tell the compiler to treat characters
in a special way
 '' is the escape character
 To create a newline in output use
n – cout << "n";
or the newer alternative
cout << endl;
 Other escape sequences:
t -- a tab
 -- a backslash character
" -- a quote character
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 50
Formatting Real Numbers
 Real numbers (type double) produce a variety of outputs
double price = 78.5;
cout << "The price is $" << price << endl;
 The output could be any of these:
The price is $78.5
The price is $78.500000
The price is $7.850000e01
 The most unlikely output is:
The price is $78.50
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 51
Showing Decimal Places
 cout includes tools to specify the output of type double
 To specify fixed point notation
 setf(ios::fixed)
 To specify that the decimal point will always be shown
 setf(ios::showpoint)
 To specify that two decimal places will always be shown
 precision(2)
 Example: cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The price is "
<< price << endl;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 52
Input Using cin
 cin is an input stream bringing data from the keyboard
 The extraction operator (>>) removes data to be used
 Example:
cout << "Enter the number of bars in a packagen";
cout << " and the weight in ounces of one bar.n";
cin >> number_of_bars;
cin >> one_weight;
 This code prompts the user to enter data then
reads two data items from cin
 The first value read is stored in number_of_bars
 The second value read is stored in one_weight
 Data is separated by spaces when entered
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 53
Reading Data From cin
 Multiple data items are separated by spaces
 Data is not read until the enter key is pressed
 Allows user to make corrections
 Example:
cin >> v1 >> v2 >> v3;
 Requires three space separated values
 User might type
34 45 12 <enter key>
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 54
Designing Input and Output
 Prompt the user for input that is desired
 cout statements provide instructions
cout << "Enter your age: ";
cin >> age;

Notice the absence of a new line before using cin
 Echo the input by displaying what was read
 Gives the user a chance to verify data
cout << age << " was entered." << endl;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 55
Section 2.2 Conclusion
 Can you
 write an input statement to place a
value in the variable the_number?
 Write the output statement to prompt for
the value to store in the_number?
 Write an output statement that produces a
newline?
 Format output of rational numbers to show
4 decimal places?
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
2.3
Data Types and Expressions
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 57
Data Types and Expressions
 2 and 2.0 are not the same number
 A whole number such as 2 is of type int
 A real number such as 2.0 is of type double
 Numbers of type int are stored as exact values
 Numbers of type double may be stored as approximate
values due to limitations on number of significant
digits that can be represented
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 58
Writing Integer constants
 Type int does not contain decimal points

Examples: 34 45 1 89
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 59
Writing Double Constants
 Type double can be written in two ways
 Simple form must include a decimal point

Examples: 34.1 23.0034 1.0 89.9
 Floating Point Notation (Scientific Notation)

Examples: 3.41e1 means 34.1
3.67e17 means
367000000000000000.0
5.89e-6 means 0.00000589
 Number left of e does not require a decimal point
 Exponent cannot contain a decimal point
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 Various number types have different memory
requirements
 More precision requires more bytes of memory
 Very large numbers require more bytes of
memory
 Very small numbers require more bytes of
memory
Slide 2- 60
Display 2.2
Other Number Types
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.2
Slide 2- 61
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 62
Integer types
 long or long int (often 4 bytes)
 Equivalent forms to declare very large integers
long big_total;
long int big_total;
 short or short int (often 2 bytes)
 Equivalent forms to declare smaller integers
short small_total;
short int small_total;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 63
Floating point types
 long double (often 10 bytes)
 Declares floating point numbers with up to
19 significant digits
long double big_number;
 float (often 4 bytes)
 Declares floating point numbers with up to
7 significant digits
float not_so_big_number;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 64
Type char
 Computers process character data too
 char
 Short for character
 Can be any single character from the keyboard
 To declare a variable of type char:
char letter;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 65
char constants
 Character constants are enclosed in single quotes
char letter = 'a';
 Strings of characters, even if only one character
is enclosed in double quotes
 "a" is a string of characters containing one character
 'a' is a value of type character
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 cin skips blanks and line breaks looking for data
 The following reads two characters but skips
any space that might be between
char symbol1, symbol2;
cin >> symbol1 >> symbol2;
 User normally separate data items by spaces
J D
 Results are the same if the data is not separated
by spaces
JD
Slide 2- 66
Display 2.3
Reading Character Data
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.3
Slide 2- 67
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 68
Type string
 string is a class, different from the primitive data types
discussed so far
 Difference is discussed in Chapter 8
 Use double quotes around the text to store into the
string variable
 Requires the following be added to the top of your
program:
#include <string>
 To declare a variable of type string:
string name = "Apu Nahasapeemapetilon";
Display 2.4
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.4
Slide 2- 69
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 70
Type Compatibilities
 In general store values in variables of the
same type
 This is a type mismatch:
int int_variable;
int_variable = 2.99;
 If your compiler allows this, int_variable will
most likely contain the value 2, not 2.99
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 71
int  double (part 1)
 Variables of type double should not be assigned
to variables of type int
int int_variable;
double double_variable;
double_variable = 2.00;
int_variable = double_variable;
 If allowed, int_variable contains 2, not 2.00
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 72
int  double (part 2)
 Integer values can normally be stored in
variables of type double
double double_variable;
double_variable = 2;
 double_variable will contain 2.0
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 73
char   int
 The following actions are possible but generally not
recommended!
 It is possible to store char values in integer
variables
int value = 'A';
value will contain an integer representing 'A'
 It is possible to store int values in char
variables
char letter = 65;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 74
bool   int
 The following actions are possible but generally
not recommended!
 Values of type bool can be assigned to int
variables
 True is stored as 1
 False is stored as 0
 Values of type int can be assigned to bool
variables
 Any non-zero integer is stored as true
 Zero is stored as false
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 75
Arithmetic
 Arithmetic is performed with operators
 + for addition
 - for subtraction
 * for multiplication
 / for division
 Example: storing a product in the variable
total_weight
total_weight = one_weight * number_of_bars;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 76
Results of Operators
 Arithmetic operators can be used with any
numeric type
 An operand is a number or variable
used by the operator
 Result of an operator depends on the types
of operands
 If both operands are int, the result is int
 If one or both operands are double, the result
is double
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 77
Division of Doubles
 Division with at least one operator of type double
produces the expected results.
double divisor, dividend, quotient;
divisor = 3;
dividend = 5;
quotient = dividend / divisor;
 quotient = 1.6666…
 Result is the same if either dividend or divisor is
of type int
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 78
Division of Integers
 Be careful with the division operator!
 int / int produces an integer result
(true for variables or numeric constants)
int dividend, divisor, quotient;
dividend = 5;
divisor = 3;
quotient = dividend / divisor;
 The value of quotient is 1, not 1.666…
 Integer division does not round the result, the
fractional part is discarded!
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 % operator gives the remainder from integer
division
 int dividend, divisor, remainder;
dividend = 5;
divisor = 3;
remainder = dividend % divisor;
The value of remainder is 2
Slide 2- 79
Display 2.5
Integer Remainders
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.5
Slide 2- 80
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 Use spacing to make expressions readable
 Which is easier to read?
x+y*z or x + y * z
 Precedence rules for operators are the same as
used in your algebra classes
 Use parentheses to alter the order of operations
x + y * z ( y is multiplied by z first)
(x + y) * z ( x and y are added first)
Slide 2- 81
Display 2.6
Arithmetic Expressions
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.6
Slide 2- 82
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 83
Operator Shorthand
 Some expressions occur so often that C++
contains to shorthand operators for them
 All arithmetic operators can be used this way
 += count = count + 2; becomes
count += 2;
 *= bonus = bonus * 2; becomes
bonus *= 2;
 /= time = time / rush_factor; becomes
time /= rush_factor;
 %= remainder = remainder % (cnt1+ cnt2); becomes
remainder %= (cnt1 + cnt2);
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
2.4
Simple Flow of Control
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 85
Simple Flow of Control
 Flow of control
 The order in which statements are executed
 Branch
 Lets program choose between two alternatives
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 86
Branch Example
 To calculate hourly wages there are two choices
 Regular time ( up to 40 hours)

gross_pay = rate * hours;
 Overtime ( over 40 hours)

gross_pay = rate * 40 + 1.5 * rate * (hours - 40);
 The program must choose which of these
expressions to use
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 87
Designing the Branch
 Decide if (hours >40) is true
 If it is true, then use
gross_pay = rate * 40 + 1.5 * rate * (hours -
40);
 If it is not true, then use
gross_pay = rate * hours;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 if-else statement is used in C++ to perform a
branch
if (hours > 40)
gross_pay = rate * 40 + 1.5 * rate * (hours - 40);
else
gross_pay = rate * hours;
Slide 2- 88
Display 2.7
Display 2.8
Implementing the Branch
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.7
Slide 2- 89
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.8
Slide 2- 90
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 Boolean expressions are expressions that are
either true or false
 comparison operators such as '>' (greater than)
are used to compare variables and/or numbers
 (hours > 40) Including the parentheses, is the
boolean expression from the wages example
 A few of the comparison operators that use two
symbols (No spaces allowed between the symbols!)

>= greater than or equal to

!= not equal or inequality

= = equal or equivalent
Slide 2- 91
Display 2.9
Boolean Expressions
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.9
Slide 2- 92
NextBack
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 93
if-else Flow Control (1)
 if (boolean expression)
true statement
else
false statement
 When the boolean expression is true
 Only the true statement is executed
 When the boolean expression is false
 Only the false statement is executed
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 94
if-else Flow Control (2)
 if (boolean expression)
{
true statements
}
else
{
false statements
}
 When the boolean expression is true
 Only the true statements enclosed in { } are executed
 When the boolean expression is false
 Only the false statements enclosed in { } are executed
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 95
AND
 Boolean expressions can be combined into
more complex expressions with
 && -- The AND operator

True if both expressions are true
 Syntax: (Comparison_1) && (Comparison_2)
 Example: if ( (2 < x) && (x < 7) )
 True only if x is between 2 and 7
 Inside parentheses are optional but enhance meaning
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 96
OR
 | | -- The OR operator (no space!)
 True if either or both expressions are true
 Syntax: (Comparison_1) | | (Comparison_2)
 Example: if ( ( x = = 1) | | ( x = = y) )
 True if x contains 1
 True if x contains the same value as y
 True if both comparisons are true
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 97
NOT
 ! -- negates any boolean expression
 !( x < y)

True if x is NOT less than y
 !(x = = y)

True if x is NOT equal to y
 ! Operator can make expressions difficult to
understand…use only when appropriate
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 98
Inequalities
 Be careful translating inequalities to C++
 if x < y < z translates as
if ( ( x < y ) && ( y < z ) )
NOT
if ( x < y < z )
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 99
Pitfall: Using = or ==
 ' = ' is the assignment operator
 Used to assign values to variables
 Example: x = 3;
 '= = ' is the equality operator
 Used to compare values
 Example: if ( x == 3)
 The compiler will accept this error:
if (x = 3)
but stores 3 in x instead of comparing x and 3
 Since the result is 3 (non-zero), the expression is true
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 A compound statement is more than one
statement enclosed in { }
 Branches of if-else statements often need to
execute more that one statement
 Example: if (boolean expression)
{
true statements
}
else
{
false statements
}
Slide 2- 100
Display 2.10
Compound Statements
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.10
Slide 2- 101
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 102
Branches Conclusion
 Can you
 Write an if-else statement that outputs the word
High if the value of the variable score is greater
than 100 and Low if the value of score is at most
100? The variables are of type int.
 Write an if-else statement that outputs the word
Warning provided that either the value of the variable
temperature is greater than or equal to 100, or the
of the variable pressure is greater than or equal to
200, or both. Otherwise, the if_else sttement outputs
the word OK. The variables are of type int.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 When an action must be repeated, a loop is used
 C++ includes several ways to create loops
 We start with the while-loop
 Example: while (count_down > 0)
{
cout << "Hello ";
count_down -= 1;
}
 Output: Hello Hello Hello
when count_down starts at 3
Slide 2- 103
Display 2.11
Simple Loops
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.11
Slide 2- 104
NextBack
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 105
While Loop Operation
 First, the boolean expression is evaluated
 If false, the program skips to the line following the
while loop
 If true, the body of the loop is executed

During execution, some item from the boolean expression
is changed
 After executing the loop body, the boolean
expression is checked again repeating the process
until the expression becomes false
 A while loop might not execute at all if the
boolean expression is false on the first check
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 while (boolean expression is true)
{
statements to repeat
}
 Semi-colons are used only to end the
statements
within the loop
 While (boolean expression is true)
statement to repeat
Slide 2- 106
Display 2.12
while Loop Syntax
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.12
Slide 2- 107
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 A variation of the while loop.
 A do-while loop is always executed at least once
 The body of the loop is first executed
 The boolean expression is checked after the body
has been executed
 Syntax: do
{
statements to repeat
} while (boolean_expression);
Slide 2- 108
Display 2.13
Display 2.14
do-while loop
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.13
Slide 2- 109
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.14
Slide 2- 110
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 111
Increment/Decrement
 Unary operators require only one operand
 + in front of a number such as +5
 - in front of a number such as -5
 ++ increment operator
 Adds 1 to the value of a variable
x ++;
is equivalent to x = x + 1;
 -- decrement operator
 Subtracts 1 from the value of a variable
x --;
is equivalent to x = x – 1;
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 Bank charge card balance of $50
 2% per month interest
 How many months without payments before
your balance exceeds $100
 After 1 month: $50 + 2% of $50 = $51
 After 2 months: $51 + 2% of $51 = $52.02
 After 3 months: $52.02 + 2% of $52.02 …
Slide 2- 112
Display 2.15
Sample Program
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.15
Slide 2- 113
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 114
Infinite Loops
 Loops that never stop are infinite loops
 The loop body should contain a line that will
eventually cause the boolean expression to
become false
 Example: Print the odd numbers less than 12
x = 1;
while (x != 12)
{
cout << x << endl;
x = x + 2;
}
 Better to use this comparison: while ( x < 12)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 115
Section 2.4 Conclusion
 Can you
 Show the output of this code if x is of type int?
x = 10;
while ( x > 0)
{
cout << x << endl;
x = x – 3;
}
 Show the output of the previous code using the
comparison x < 0 instead of x > 0?
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
2.5
Program Style
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 117
Program Style
 A program written with attention to style
 is easier to read
 easier to correct
 easier to change
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 118
Program Style - Indenting
 Items considered a group should look like a
group
 Skip lines between logical groups of statements
 Indent statements within statements
if (x = = 0)
statement;
 Braces {} create groups
 Indent within braces to make the group clear
 Braces placed on separate lines are easier to locate
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 119
Program Style - Comments
 // is the symbol for a single line comment
 Comments are explanatory notes for the programmer
 All text on the line following // is ignored by the
compiler
 Example: //calculate regular wages
gross_pay = rate * hours;
 /* and */ enclose multiple line comments
 Example: /* This is a comment that spans
multiple lines without a
comment symbol on the middle line
*/
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 120
Program Style - Constants
 Number constants have no mnemonic value
 Number constants used throughout a program
are difficult to find and change when needed
 Constants
 Allow us to name number constants so they have
meaning
 Allow us to change all occurrences simply by
changing the value of the constant
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 const is the keyword to declare a constant
 Example:
const int WINDOW_COUNT = 10;
declares a constant named WINDOW_COUNT
 Its value cannot be changed by the program
like a
variable
 It is common to name constants with all
capitals
Slide 2- 121
Display 2.16
Constants
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.16
Slide 2- 122
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 123
Section 2.5 Conclusion
 Can you
 Create a named constant of type double?
 Determine if a program can modify the value of a
constant?
 Describe the benefits of comments?
 Explain why indenting is important in a program?
 Explain why blank lines are important in a program?
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 124
Chapter 2 -- End
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.1 (1/2)
Slide 2- 125
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.1
(2 /2)
Slide 2- 126
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.2
Slide 2- 127
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.3
Slide 2- 128
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.4
Slide 2- 129
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.5
Slide 2- 130
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.6
Slide 2- 131
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.7
Slide 2- 132
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.8
Slide 2- 133
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.9
Slide 2- 134
NextBack
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.10
Slide 2- 135
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.11
Slide 2- 136
NextBack
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.12
Slide 2- 137
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.13
Slide 2- 138
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.14
Slide 2- 139
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.15
Slide 2- 140
Back Next
Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Display 2.16
Slide 2- 141
Back Next

Más contenido relacionado

La actualidad más candente

Property-Based Testing
Property-Based TestingProperty-Based Testing
Property-Based TestingShai Geva
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2Abdul Haseeb
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTsKevlin Henney
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3Abdul Haseeb
 
Python workshop session 6
Python workshop session 6Python workshop session 6
Python workshop session 6Abdul Haseeb
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++Ilio Catallo
 
Whats new in ES2019
Whats new in ES2019Whats new in ES2019
Whats new in ES2019chayanikaa
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7Megha V
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Chris Richardson
 

La actualidad más candente (20)

Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
 
Property-Based Testing
Property-Based TestingProperty-Based Testing
Property-Based Testing
 
Computer Programming- Lecture 7
Computer Programming- Lecture 7Computer Programming- Lecture 7
Computer Programming- Lecture 7
 
Computer Programming- Lecture 4
Computer Programming- Lecture 4Computer Programming- Lecture 4
Computer Programming- Lecture 4
 
Chapter1c
Chapter1cChapter1c
Chapter1c
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
 
Cbse marking scheme 2006 2011
Cbse marking scheme 2006  2011Cbse marking scheme 2006  2011
Cbse marking scheme 2006 2011
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
Qno 1 (a)
Qno 1 (a)Qno 1 (a)
Qno 1 (a)
 
Python workshop session 6
Python workshop session 6Python workshop session 6
Python workshop session 6
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
 
Whats new in ES2019
Whats new in ES2019Whats new in ES2019
Whats new in ES2019
 
Icom4015 lecture4-f16
Icom4015 lecture4-f16Icom4015 lecture4-f16
Icom4015 lecture4-f16
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
 

Destacado (20)

Savitch ch 04
Savitch ch 04Savitch ch 04
Savitch ch 04
 
Savitch Ch 15
Savitch Ch 15Savitch Ch 15
Savitch Ch 15
 
Savitch Ch 14
Savitch Ch 14Savitch Ch 14
Savitch Ch 14
 
Savitch Ch 10
Savitch Ch 10Savitch Ch 10
Savitch Ch 10
 
Savitch Ch 12
Savitch Ch 12Savitch Ch 12
Savitch Ch 12
 
Savitch Ch 08
Savitch Ch 08Savitch Ch 08
Savitch Ch 08
 
Savitch c++ ppt figs ch1
Savitch c++ ppt figs ch1Savitch c++ ppt figs ch1
Savitch c++ ppt figs ch1
 
Savitch Ch 02
Savitch Ch 02Savitch Ch 02
Savitch Ch 02
 
Savitch Ch 11
Savitch Ch 11Savitch Ch 11
Savitch Ch 11
 
Savitch Ch 18
Savitch Ch 18Savitch Ch 18
Savitch Ch 18
 
Savitch Ch 17
Savitch Ch 17Savitch Ch 17
Savitch Ch 17
 
Savitch ch 16
Savitch ch 16Savitch ch 16
Savitch ch 16
 
Savitch Ch 03
Savitch Ch 03Savitch Ch 03
Savitch Ch 03
 
Savitch Ch 06
Savitch Ch 06Savitch Ch 06
Savitch Ch 06
 
Savitch Ch 13
Savitch Ch 13Savitch Ch 13
Savitch Ch 13
 
Savitch ch 01
Savitch ch 01Savitch ch 01
Savitch ch 01
 
Savitch Ch 01
Savitch Ch 01Savitch Ch 01
Savitch Ch 01
 
Savitch Ch 07
Savitch Ch 07Savitch Ch 07
Savitch Ch 07
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
 
Savitch Ch 05
Savitch Ch 05Savitch Ch 05
Savitch Ch 05
 

Similar a Savitch ch 022 (20)

CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
Savitch Ch 02
Savitch Ch 02Savitch Ch 02
Savitch Ch 02
 
901131 examples
901131 examples901131 examples
901131 examples
 
Savitch ch 02
Savitch ch 02Savitch ch 02
Savitch ch 02
 
02slide_accessible.pptx
02slide_accessible.pptx02slide_accessible.pptx
02slide_accessible.pptx
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
intro to c
intro to cintro to c
intro to c
 
C Introduction
C IntroductionC Introduction
C Introduction
 
Copyright © 2012 Pearson Addison-Wesley. All rights reser.docx
Copyright © 2012 Pearson Addison-Wesley.  All rights reser.docxCopyright © 2012 Pearson Addison-Wesley.  All rights reser.docx
Copyright © 2012 Pearson Addison-Wesley. All rights reser.docx
 
Prog1-L2.pptx
Prog1-L2.pptxProg1-L2.pptx
Prog1-L2.pptx
 
C Operators
C OperatorsC Operators
C Operators
 
chap1cpp3rd.ppt
chap1cpp3rd.pptchap1cpp3rd.ppt
chap1cpp3rd.ppt
 
C Intro.ppt
C Intro.pptC Intro.ppt
C Intro.ppt
 
Introduction to C++ lecture ************
Introduction to C++ lecture ************Introduction to C++ lecture ************
Introduction to C++ lecture ************
 
introductory concepts
introductory conceptsintroductory concepts
introductory concepts
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 
Cpa lecture (theory) 02_
Cpa lecture (theory) 02_Cpa lecture (theory) 02_
Cpa lecture (theory) 02_
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 

Último

How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineCeline George
 
physiotherapy in Acne condition.....pptx
physiotherapy in Acne condition.....pptxphysiotherapy in Acne condition.....pptx
physiotherapy in Acne condition.....pptxAneriPatwari
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxryandux83rd
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...Nguyen Thanh Tu Collection
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEPART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEMISSRITIMABIOLOGYEXP
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
The Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian CongressThe Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian CongressMaria Paula Aroca
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 

Último (20)

Chi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical VariableChi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical Variable
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command Line
 
Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,
 
physiotherapy in Acne condition.....pptx
physiotherapy in Acne condition.....pptxphysiotherapy in Acne condition.....pptx
physiotherapy in Acne condition.....pptx
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 
CARNAVAL COM MAGIA E EUFORIA _
CARNAVAL COM MAGIA E EUFORIA            _CARNAVAL COM MAGIA E EUFORIA            _
CARNAVAL COM MAGIA E EUFORIA _
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptx
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEPART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
The Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian CongressThe Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian Congress
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 

Savitch ch 022

  • 1.
  • 2. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics
  • 3. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 3 Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style
  • 4. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2.1 Variables and Assignments
  • 5. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  Variables are like small blackboards  We can write a number on them  We can change the number  We can erase the number  C++ variables are names for memory locations  We can write a value in them  We can change the value stored there  We cannot erase the memory location  Some value is always there Slide 2- 5 Display 2.1 Variables and Assignments
  • 6. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.1 (1/2) Slide 2- 6 Back Next
  • 7. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 7 Identifiers  Variables names are called identifiers  Choosing variable names  Use meaningful names that represent data to be stored  First character must be  a letter  the underscore character  Remaining characters must be  letters  numbers  underscore character
  • 8. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 8 Keywords  Keywords (also called reserved words)  Are used by the C++ language  Must be used as they are defined in the programming language  Cannot be used as identifiers
  • 9. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Declaring Variables (Part 1)  Before use, variables must be declared  Tells the compiler the type of data to store Examples: int number_of_bars; double one_weight, total_weight;  int is an abbreviation for integer.  could store 3, 102, 3211, -456, etc.  number_of_bars is of type integer  double represents numbers with a fractional component  could store 1.34, 4.0, -345.6, etc.  one_weight and total_weight are both of type double
  • 10. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 10 Declaring Variables (Part 2)  Immediately prior to use int main() { … int sum; sum = score1 + score 2; … return 0; }  At the beginning int main() { int sum; … sum = score1 + score2; … return 0; } Two locations for variable declarations
  • 11. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 11 Declaring Variables (Part 3)  Declaration syntax:  Type_name Variable_1 , Variable_2, . . . ;  Declaration Examples:  double average, m_score, total_score;  double moon_distance;  int age, num_students;  int cars_waiting;
  • 12. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 12 Assignment Statements  An assignment statement changes the value of a variable  total_weight = one_weight + number_of_bars;  total_weight is set to the sum one_weight + number_of_bars  Assignment statements end with a semi-colon  The single variable to be changed is always on the left of the assignment operator ‘=‘  On the right of the assignment operator can be  Constants -- age = 21;  Variables -- my_cost = your_cost;  Expressions -- circumference = diameter * 3.14159;
  • 13. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 13 Assignment Statements and Algebra  The ‘=‘ operator in C++ is not an equal sign  The following statement cannot be true in algebra  number_of_bars = number_of_bars + 3;  In C++ it means the new value of number_of_bars is the previous value of number_of_bars plus 3
  • 14. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 14 Initializing Variables  Declaring a variable does not give it a value  Giving a variable its first value is initializing the variable  Variables are initialized in assignment statements double mpg; // declare the variable mpg = 26.3; // initialize the variable  Declaration and initialization can be combined using two methods  Method 1 double mpg = 26.3, area = 0.0 , volume;  Method 2 double mpg(26.3), area(0.0), volume;
  • 15. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 15 Section 2.1 Conclusion  Can you  Declare and initialize two integers variables to zero? The variables are named feet and inches.  Declare and initialize two variables, one int and one double? Both should be initialized to the appropriate form of 5.  Give good variable names for identifiers to store  the speed of an automobile?  an hourly pay rate?  the highest score on an exam?
  • 16. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2.2 Input and Output
  • 17. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  # include <iostream.h>  int main()  {  cout << "Hii C++ " ;  return 0;  }
  • 18. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. • /// add two numbers • # include <iostream.h> • main() • { • int num1 , num2; • cout << "the first number:n " ; • cin >> num1; • cout << " the second number:n"; • cin >> num2; • cout << "the Value is: " << num1+num2; • return 0; • }
  • 19. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. • //const int number=14 ; • //Arithmetic Operations • // + - * / % • //Relation Operator < <= > >= == • // i=i+2; = i+=2 ; • //Conditional Expressions • //if (a > b ) • max = a ; • if ( b < a) • max = b ; • if ( b == a) • max = a = b;
  • 20. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Increment and Decrement Operators • Increment and Decrement Operators • a = ++b ; if b=6 • b=a=7 • a = b ++ ; • B=7 • A=6 • cout << "Hellow C++"; • cout << a ;
  • 21. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  cout << "Please: " << a << b << "Hellow" ;  cout << "Hellow" << endl << "World" ;  Output  Hellow  World
  • 22. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  t  . n  . r  a  b  using namespace std;
  • 23. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. comments • int a=0 /* the compiler • cannot read thie*/ • #include <iostream> • using namespace std; • int main() • { • cout << "Hellow Worldn I am a programmer " << endl; • return 0; • } • out • Hellow Worlad • I am a programmer
  • 24. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Reserved words in C++ • asm, auto, bool, break, case, catch, char, class, const, • const_cast, continue, default, delete, do, double, • dynamic_cast, else, enum, explicit, extern, false, float, for, • friend, goto, if, inline, int, long, mutable, namespace, new, • operator, private, protected, public, register, • reinterpret_cast, return, short, signed, sizeof, static, • static_cast, struct, switch, template, this, throw, true, try, • typedef, typeid, typename, union, unsigned, using, virtual, • void, volatile, wchar_t • Logic operation: • and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, • xor, xor_eq
  • 25. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Variables  a = 4 ;  b = 1 ;  a = a + 3 ;  result = a + b
  • 26. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. • #include<iostream.h> • main() • { int z,x; • for(z=1;z<=5;z++) • { • cout<<"*n" ; • for(x=0;x<=z-1;x++) • cout<<" " ; • }}
  • 27. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. • // my second program in C++ Hello World! I'm a C++ program • #include <iostream> • using namespace std; • int main () • { • cout << • "Hello World! "; • cout << • "I'm a C++ program"; • return 0; • }
  • 28. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. • // operating with variables 16 • #include <iostream> • using namespace std; • int main () • { • // declaring variables: • int a, b; • int result; • // process: • a = 10; • b = 4; • a = a + 2; • result = a + b; • // print out the result: • cout << result; • // terminate the program: • return 0; • }
  • 29. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. • // my first string • #include <iostream> • #include <string> • using namespace std; • int main () • { • string name; • cout<<"enter your name: "; • cin>>name; • cout << "Hello "<<name<<endl; • return 0;}
  • 30. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. • // my second string • #include <iostream> • #include <string> • using namespace std; • int main () • { • string firstName,lastName,fullName; • cout<<"enter your first name: "; • cin>>firstName; • cout<<"enter your last name: "; • cin>>lastName; • fullName=firstName+" "+lastName; • cout<<"Hello "<<fullName<<endl; • if(firstName<lastName) • cout<<firstName<<endl; • return 0;}
  • 31. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  //write a program to output:- 12345678910  #include <iostream>  using namespace std;  Int main()  {  int a ;  for(a=1;a<=10;a++)  cout<<a;  cout<<endl;  system("pause");  return 0;  }
  • 32. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. convert the seconds to minutes and hours  //write a program to like 00:00:00 :-  #include <iostream>  using namespace std;   int main ()  {  int second,hours=3600,minutes=60;  cout<<"Enter the seconds ";  cin>> second;  hours=second/hours;  minutes=second/minutes;  second=second%60;  cout<<hours<<":"<<minutes%60<<":"<<second<<endl;  return 0;  }
  • 33. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  //write a program to input 3 integers and determine which of them is biggest:-  #include <iostream>  using namespace std;   int main ()  {  int a,b,c;  cout<<"enter a , b and c one after another"<<endl;  cin>>a>>b>>c;  if ((a>b)&&(a>c)) //take care of (( )) important.  cout<<"first number is bigger="<<a<<endl;  if ((b>a)&&(b>c))  cout<<"the second number is bigger="<<b<<endl;  if ((c>a)&&(c>b))  cout<<"the last number is bigger="<<c<<endl;  if((a==c)&&(c==b))  cout<<"all the numbers are equal"<<endl;  return 0;  }
  • 34. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  //write a program to input 3 integers and determine which of them is biggest:-  #include <iostream>  using namespace std;   int main ()  {  int a,b,c;  cout<<"enter a , b and c one after another"<<endl;  cin>>a>>b>>c;  if ((a>b)&&(a>c)) //take care of (( )) important.  cout<<"first number is bigger="<<a<<endl;  if ((b>a)&&(b>c))  cout<<"the second number is bigger="<<b<<endl;  if ((c>a)&&(c>b))  cout<<"the last number is bigger="<<c<<endl;  if((a==c)&&(c==b))  cout<<"all the numbers are equal"<<endl;  return 0;  }
  • 35. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  /*/ //write a program to enter a letter and determine the letter is small or capital:-  #include <iostream>  using namespace std;   int main ()  {  char letter;  cout<<"Enter a letter:";  cin >> letter;  if(letter >= 'A')  if(letter <= 'Z')  cout<<"You entered a capital letter.";  else  cout<<"You entered a small letter.";  system("pause");  return 0;  }
  • 36. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  #include <iostream>  using namespace std;   int main ()  {  float a,b,c,x;  cout<<"enter a , b and c one after another"<<endl;  cin>>a>>b>>c;  x=(3+4*a*b)/(2*c);  cout<<"x="<<x<<endl;  system("pause");  return 0;  }  }
  • 37. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Find error #include <iostream> using namespace std; int main () { float a = 1 , b = 0 ; unsigned int result; a = a - 6; result = A + b; cout << result; }
  • 38. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Printing value • cout<< "Islam is a way of life" ; • // print Islam is a way of life on screen • cout<< 120 ; • // print number 120 on screen • cout<< x ; • // print the conte • cout<< "Hello" ; // Hellont of variable x on screen • cout<< Hello ; // Hello
  • 39. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. • Cou t<< "The peace upon you, " << "I am " << "a C++ sentence. " ; • The peace upon you, I am a C++ sentence. • cout<<"I am "<< age << " years old and my zipcode is "<< zipcode ; • cin >> a >> b ; #include <iostream> using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".n"; return 0; }
  • 40. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Write program  const float pi = 3.14159265 ;  const char tab = 't' ;  #define  #define pi 3.14159265  #define NewLine 'n'
  • 41. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. #include<iostream> using namespace std; #define pi 3.14159265 int main() { float R ,r ; r = 0 ;//initial to 0 cout<<"enter circle radius plz: "; cin>> r ; cout<< "circle area = " << 0.5 * pi * r * r << endl; return 0 ;}
  • 42. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. #include<iostream> using namespace std; const double pi = 3.14159265; int main() { float r ; r = 0 ;//initial to 0 cout<<"enter circle radius plz: "; cin>> r ; cout<< "circle area = " << 0.5 * pi * r * r << endl; return 0 ;
  • 43. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  int a , b ; // a : ? b : ?  a = 10 ; // a : 10 b : ?  b = 4 ; // a : 10 b : 4  a = b ; // a : 4 b : 4  b = 7 ; // a : 4 b : 7  a = 2 + ( b = 5 ) ; // 1) b = 5 ; 2) a = 2 + b ; 3) a = 7 ;
  • 44. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  b = 3;  a = ++b;  // a = 4 , b = 4  Or  b = 3;  a = b++ ;  // a = 3 , b = 4
  • 45. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 45 Input and Output  A data stream is a sequence of data  Typically in the form of characters or numbers  An input stream is data for the program to use  Typically originates  at the keyboard  at a file  An output stream is the program’s output  Destination is typically  the monitor  a file
  • 46. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 46 Output using cout  cout is an output stream sending data to the monitor  The insertion operator "<<" inserts data into cout  Example: cout << number_of_bars << " candy barsn";  This line sends two items to the monitor  The value of number_of_bars  The quoted string of characters " candy barsn"  Notice the space before the ‘c’ in candy  The ‘n’ causes a new line to be started following the ‘s’ in bars  A new insertion operator is used for each item of output
  • 47. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 47 Examples Using cout  This produces the same result as the previous sample cout << number_of_bars ; cout << " candy barsn";  Here arithmetic is performed in the cout statement cout << "Total cost is $" << (price + tax);  Quoted strings are enclosed in double quotes ("Walter")  Don’t use two single quotes (')  A blank space can also be inserted with cout << " " ; if there are no strings in which a space is desired as in " candy barsn"
  • 48. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 48 Include Directives  Include Directives add library files to our programs  To make the definitions of the cin and cout available to the program: #include <iostream>  Using Directives include a collection of defined names  To make the names cin and cout available to our program: using namespace std;
  • 49. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 49 Escape Sequences  Escape sequences tell the compiler to treat characters in a special way  '' is the escape character  To create a newline in output use n – cout << "n"; or the newer alternative cout << endl;  Other escape sequences: t -- a tab -- a backslash character " -- a quote character
  • 50. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 50 Formatting Real Numbers  Real numbers (type double) produce a variety of outputs double price = 78.5; cout << "The price is $" << price << endl;  The output could be any of these: The price is $78.5 The price is $78.500000 The price is $7.850000e01  The most unlikely output is: The price is $78.50
  • 51. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 51 Showing Decimal Places  cout includes tools to specify the output of type double  To specify fixed point notation  setf(ios::fixed)  To specify that the decimal point will always be shown  setf(ios::showpoint)  To specify that two decimal places will always be shown  precision(2)  Example: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "The price is " << price << endl;
  • 52. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 52 Input Using cin  cin is an input stream bringing data from the keyboard  The extraction operator (>>) removes data to be used  Example: cout << "Enter the number of bars in a packagen"; cout << " and the weight in ounces of one bar.n"; cin >> number_of_bars; cin >> one_weight;  This code prompts the user to enter data then reads two data items from cin  The first value read is stored in number_of_bars  The second value read is stored in one_weight  Data is separated by spaces when entered
  • 53. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 53 Reading Data From cin  Multiple data items are separated by spaces  Data is not read until the enter key is pressed  Allows user to make corrections  Example: cin >> v1 >> v2 >> v3;  Requires three space separated values  User might type 34 45 12 <enter key>
  • 54. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 54 Designing Input and Output  Prompt the user for input that is desired  cout statements provide instructions cout << "Enter your age: "; cin >> age;  Notice the absence of a new line before using cin  Echo the input by displaying what was read  Gives the user a chance to verify data cout << age << " was entered." << endl;
  • 55. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 55 Section 2.2 Conclusion  Can you  write an input statement to place a value in the variable the_number?  Write the output statement to prompt for the value to store in the_number?  Write an output statement that produces a newline?  Format output of rational numbers to show 4 decimal places?
  • 56. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2.3 Data Types and Expressions
  • 57. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 57 Data Types and Expressions  2 and 2.0 are not the same number  A whole number such as 2 is of type int  A real number such as 2.0 is of type double  Numbers of type int are stored as exact values  Numbers of type double may be stored as approximate values due to limitations on number of significant digits that can be represented
  • 58. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 58 Writing Integer constants  Type int does not contain decimal points  Examples: 34 45 1 89
  • 59. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 59 Writing Double Constants  Type double can be written in two ways  Simple form must include a decimal point  Examples: 34.1 23.0034 1.0 89.9  Floating Point Notation (Scientific Notation)  Examples: 3.41e1 means 34.1 3.67e17 means 367000000000000000.0 5.89e-6 means 0.00000589  Number left of e does not require a decimal point  Exponent cannot contain a decimal point
  • 60. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  Various number types have different memory requirements  More precision requires more bytes of memory  Very large numbers require more bytes of memory  Very small numbers require more bytes of memory Slide 2- 60 Display 2.2 Other Number Types
  • 61. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.2 Slide 2- 61 Back Next
  • 62. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 62 Integer types  long or long int (often 4 bytes)  Equivalent forms to declare very large integers long big_total; long int big_total;  short or short int (often 2 bytes)  Equivalent forms to declare smaller integers short small_total; short int small_total;
  • 63. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 63 Floating point types  long double (often 10 bytes)  Declares floating point numbers with up to 19 significant digits long double big_number;  float (often 4 bytes)  Declares floating point numbers with up to 7 significant digits float not_so_big_number;
  • 64. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 64 Type char  Computers process character data too  char  Short for character  Can be any single character from the keyboard  To declare a variable of type char: char letter;
  • 65. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 65 char constants  Character constants are enclosed in single quotes char letter = 'a';  Strings of characters, even if only one character is enclosed in double quotes  "a" is a string of characters containing one character  'a' is a value of type character
  • 66. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  cin skips blanks and line breaks looking for data  The following reads two characters but skips any space that might be between char symbol1, symbol2; cin >> symbol1 >> symbol2;  User normally separate data items by spaces J D  Results are the same if the data is not separated by spaces JD Slide 2- 66 Display 2.3 Reading Character Data
  • 67. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.3 Slide 2- 67 Back Next
  • 68. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 68 Type string  string is a class, different from the primitive data types discussed so far  Difference is discussed in Chapter 8  Use double quotes around the text to store into the string variable  Requires the following be added to the top of your program: #include <string>  To declare a variable of type string: string name = "Apu Nahasapeemapetilon"; Display 2.4
  • 69. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.4 Slide 2- 69 Back Next
  • 70. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 70 Type Compatibilities  In general store values in variables of the same type  This is a type mismatch: int int_variable; int_variable = 2.99;  If your compiler allows this, int_variable will most likely contain the value 2, not 2.99
  • 71. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 71 int  double (part 1)  Variables of type double should not be assigned to variables of type int int int_variable; double double_variable; double_variable = 2.00; int_variable = double_variable;  If allowed, int_variable contains 2, not 2.00
  • 72. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 72 int  double (part 2)  Integer values can normally be stored in variables of type double double double_variable; double_variable = 2;  double_variable will contain 2.0
  • 73. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 73 char   int  The following actions are possible but generally not recommended!  It is possible to store char values in integer variables int value = 'A'; value will contain an integer representing 'A'  It is possible to store int values in char variables char letter = 65;
  • 74. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 74 bool   int  The following actions are possible but generally not recommended!  Values of type bool can be assigned to int variables  True is stored as 1  False is stored as 0  Values of type int can be assigned to bool variables  Any non-zero integer is stored as true  Zero is stored as false
  • 75. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 75 Arithmetic  Arithmetic is performed with operators  + for addition  - for subtraction  * for multiplication  / for division  Example: storing a product in the variable total_weight total_weight = one_weight * number_of_bars;
  • 76. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 76 Results of Operators  Arithmetic operators can be used with any numeric type  An operand is a number or variable used by the operator  Result of an operator depends on the types of operands  If both operands are int, the result is int  If one or both operands are double, the result is double
  • 77. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 77 Division of Doubles  Division with at least one operator of type double produces the expected results. double divisor, dividend, quotient; divisor = 3; dividend = 5; quotient = dividend / divisor;  quotient = 1.6666…  Result is the same if either dividend or divisor is of type int
  • 78. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 78 Division of Integers  Be careful with the division operator!  int / int produces an integer result (true for variables or numeric constants) int dividend, divisor, quotient; dividend = 5; divisor = 3; quotient = dividend / divisor;  The value of quotient is 1, not 1.666…  Integer division does not round the result, the fractional part is discarded!
  • 79. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  % operator gives the remainder from integer division  int dividend, divisor, remainder; dividend = 5; divisor = 3; remainder = dividend % divisor; The value of remainder is 2 Slide 2- 79 Display 2.5 Integer Remainders
  • 80. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.5 Slide 2- 80 Back Next
  • 81. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  Use spacing to make expressions readable  Which is easier to read? x+y*z or x + y * z  Precedence rules for operators are the same as used in your algebra classes  Use parentheses to alter the order of operations x + y * z ( y is multiplied by z first) (x + y) * z ( x and y are added first) Slide 2- 81 Display 2.6 Arithmetic Expressions
  • 82. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.6 Slide 2- 82 Back Next
  • 83. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 83 Operator Shorthand  Some expressions occur so often that C++ contains to shorthand operators for them  All arithmetic operators can be used this way  += count = count + 2; becomes count += 2;  *= bonus = bonus * 2; becomes bonus *= 2;  /= time = time / rush_factor; becomes time /= rush_factor;  %= remainder = remainder % (cnt1+ cnt2); becomes remainder %= (cnt1 + cnt2);
  • 84. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2.4 Simple Flow of Control
  • 85. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 85 Simple Flow of Control  Flow of control  The order in which statements are executed  Branch  Lets program choose between two alternatives
  • 86. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 86 Branch Example  To calculate hourly wages there are two choices  Regular time ( up to 40 hours)  gross_pay = rate * hours;  Overtime ( over 40 hours)  gross_pay = rate * 40 + 1.5 * rate * (hours - 40);  The program must choose which of these expressions to use
  • 87. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 87 Designing the Branch  Decide if (hours >40) is true  If it is true, then use gross_pay = rate * 40 + 1.5 * rate * (hours - 40);  If it is not true, then use gross_pay = rate * hours;
  • 88. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  if-else statement is used in C++ to perform a branch if (hours > 40) gross_pay = rate * 40 + 1.5 * rate * (hours - 40); else gross_pay = rate * hours; Slide 2- 88 Display 2.7 Display 2.8 Implementing the Branch
  • 89. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.7 Slide 2- 89 Back Next
  • 90. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.8 Slide 2- 90 Back Next
  • 91. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  Boolean expressions are expressions that are either true or false  comparison operators such as '>' (greater than) are used to compare variables and/or numbers  (hours > 40) Including the parentheses, is the boolean expression from the wages example  A few of the comparison operators that use two symbols (No spaces allowed between the symbols!)  >= greater than or equal to  != not equal or inequality  = = equal or equivalent Slide 2- 91 Display 2.9 Boolean Expressions
  • 92. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.9 Slide 2- 92 NextBack
  • 93. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 93 if-else Flow Control (1)  if (boolean expression) true statement else false statement  When the boolean expression is true  Only the true statement is executed  When the boolean expression is false  Only the false statement is executed
  • 94. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 94 if-else Flow Control (2)  if (boolean expression) { true statements } else { false statements }  When the boolean expression is true  Only the true statements enclosed in { } are executed  When the boolean expression is false  Only the false statements enclosed in { } are executed
  • 95. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 95 AND  Boolean expressions can be combined into more complex expressions with  && -- The AND operator  True if both expressions are true  Syntax: (Comparison_1) && (Comparison_2)  Example: if ( (2 < x) && (x < 7) )  True only if x is between 2 and 7  Inside parentheses are optional but enhance meaning
  • 96. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 96 OR  | | -- The OR operator (no space!)  True if either or both expressions are true  Syntax: (Comparison_1) | | (Comparison_2)  Example: if ( ( x = = 1) | | ( x = = y) )  True if x contains 1  True if x contains the same value as y  True if both comparisons are true
  • 97. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 97 NOT  ! -- negates any boolean expression  !( x < y)  True if x is NOT less than y  !(x = = y)  True if x is NOT equal to y  ! Operator can make expressions difficult to understand…use only when appropriate
  • 98. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 98 Inequalities  Be careful translating inequalities to C++  if x < y < z translates as if ( ( x < y ) && ( y < z ) ) NOT if ( x < y < z )
  • 99. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 99 Pitfall: Using = or ==  ' = ' is the assignment operator  Used to assign values to variables  Example: x = 3;  '= = ' is the equality operator  Used to compare values  Example: if ( x == 3)  The compiler will accept this error: if (x = 3) but stores 3 in x instead of comparing x and 3  Since the result is 3 (non-zero), the expression is true
  • 100. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  A compound statement is more than one statement enclosed in { }  Branches of if-else statements often need to execute more that one statement  Example: if (boolean expression) { true statements } else { false statements } Slide 2- 100 Display 2.10 Compound Statements
  • 101. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.10 Slide 2- 101 Back Next
  • 102. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 102 Branches Conclusion  Can you  Write an if-else statement that outputs the word High if the value of the variable score is greater than 100 and Low if the value of score is at most 100? The variables are of type int.  Write an if-else statement that outputs the word Warning provided that either the value of the variable temperature is greater than or equal to 100, or the of the variable pressure is greater than or equal to 200, or both. Otherwise, the if_else sttement outputs the word OK. The variables are of type int.
  • 103. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  When an action must be repeated, a loop is used  C++ includes several ways to create loops  We start with the while-loop  Example: while (count_down > 0) { cout << "Hello "; count_down -= 1; }  Output: Hello Hello Hello when count_down starts at 3 Slide 2- 103 Display 2.11 Simple Loops
  • 104. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.11 Slide 2- 104 NextBack
  • 105. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 105 While Loop Operation  First, the boolean expression is evaluated  If false, the program skips to the line following the while loop  If true, the body of the loop is executed  During execution, some item from the boolean expression is changed  After executing the loop body, the boolean expression is checked again repeating the process until the expression becomes false  A while loop might not execute at all if the boolean expression is false on the first check
  • 106. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  while (boolean expression is true) { statements to repeat }  Semi-colons are used only to end the statements within the loop  While (boolean expression is true) statement to repeat Slide 2- 106 Display 2.12 while Loop Syntax
  • 107. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.12 Slide 2- 107 Back Next
  • 108. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  A variation of the while loop.  A do-while loop is always executed at least once  The body of the loop is first executed  The boolean expression is checked after the body has been executed  Syntax: do { statements to repeat } while (boolean_expression); Slide 2- 108 Display 2.13 Display 2.14 do-while loop
  • 109. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.13 Slide 2- 109 Back Next
  • 110. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.14 Slide 2- 110 Back Next
  • 111. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 111 Increment/Decrement  Unary operators require only one operand  + in front of a number such as +5  - in front of a number such as -5  ++ increment operator  Adds 1 to the value of a variable x ++; is equivalent to x = x + 1;  -- decrement operator  Subtracts 1 from the value of a variable x --; is equivalent to x = x – 1;
  • 112. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  Bank charge card balance of $50  2% per month interest  How many months without payments before your balance exceeds $100  After 1 month: $50 + 2% of $50 = $51  After 2 months: $51 + 2% of $51 = $52.02  After 3 months: $52.02 + 2% of $52.02 … Slide 2- 112 Display 2.15 Sample Program
  • 113. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.15 Slide 2- 113 Back Next
  • 114. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 114 Infinite Loops  Loops that never stop are infinite loops  The loop body should contain a line that will eventually cause the boolean expression to become false  Example: Print the odd numbers less than 12 x = 1; while (x != 12) { cout << x << endl; x = x + 2; }  Better to use this comparison: while ( x < 12)
  • 115. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 115 Section 2.4 Conclusion  Can you  Show the output of this code if x is of type int? x = 10; while ( x > 0) { cout << x << endl; x = x – 3; }  Show the output of the previous code using the comparison x < 0 instead of x > 0?
  • 116. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2.5 Program Style
  • 117. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 117 Program Style  A program written with attention to style  is easier to read  easier to correct  easier to change
  • 118. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 118 Program Style - Indenting  Items considered a group should look like a group  Skip lines between logical groups of statements  Indent statements within statements if (x = = 0) statement;  Braces {} create groups  Indent within braces to make the group clear  Braces placed on separate lines are easier to locate
  • 119. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 119 Program Style - Comments  // is the symbol for a single line comment  Comments are explanatory notes for the programmer  All text on the line following // is ignored by the compiler  Example: //calculate regular wages gross_pay = rate * hours;  /* and */ enclose multiple line comments  Example: /* This is a comment that spans multiple lines without a comment symbol on the middle line */
  • 120. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 120 Program Style - Constants  Number constants have no mnemonic value  Number constants used throughout a program are difficult to find and change when needed  Constants  Allow us to name number constants so they have meaning  Allow us to change all occurrences simply by changing the value of the constant
  • 121. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.  const is the keyword to declare a constant  Example: const int WINDOW_COUNT = 10; declares a constant named WINDOW_COUNT  Its value cannot be changed by the program like a variable  It is common to name constants with all capitals Slide 2- 121 Display 2.16 Constants
  • 122. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.16 Slide 2- 122 Back Next
  • 123. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 123 Section 2.5 Conclusion  Can you  Create a named constant of type double?  Determine if a program can modify the value of a constant?  Describe the benefits of comments?  Explain why indenting is important in a program?  Explain why blank lines are important in a program?
  • 124. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Slide 2- 124 Chapter 2 -- End
  • 125. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.1 (1/2) Slide 2- 125 Back Next
  • 126. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.1 (2 /2) Slide 2- 126 Back Next
  • 127. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.2 Slide 2- 127 Back Next
  • 128. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.3 Slide 2- 128 Back Next
  • 129. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.4 Slide 2- 129 Back Next
  • 130. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.5 Slide 2- 130 Back Next
  • 131. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.6 Slide 2- 131 Back Next
  • 132. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.7 Slide 2- 132 Back Next
  • 133. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.8 Slide 2- 133 Back Next
  • 134. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.9 Slide 2- 134 NextBack
  • 135. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.10 Slide 2- 135 Back Next
  • 136. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.11 Slide 2- 136 NextBack
  • 137. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.12 Slide 2- 137 Back Next
  • 138. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.13 Slide 2- 138 Back Next
  • 139. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.14 Slide 2- 139 Back Next
  • 140. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.15 Slide 2- 140 Back Next
  • 141. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Display 2.16 Slide 2- 141 Back Next