Professional Documents
Culture Documents
Chapter2introtoc 2B 2bpart2
Chapter2introtoc 2B 2bpart2
(PART 2)
Reference:
D.S. Malik. (2009). C++ Programming: From Problem Analysis to Program Design. 4th Ed.. Course
Technology, Cengage Learning. Boston, USA.
Outline
PART 1 PART 2
1. The Basic of a C++ 5. Expression
Program. 6. Input Type Conversion(Casting)
2. Data Types 7. Input
3. String Types 8.Output
4. Arithmetic Operators, 9.Increment and decrement
Operator Precedence, operators
Decision Making 10.Pre-Processor Directives
•Sem 1 20202021 •2
5. Expressions
•Sem 1 20202021 •3
5. Expressions (cont’)
EXAMPLE 2.4
2+3*5
3+x–y/7
x + 2 * (y – z) + 18
•Sem 1 20202021 •4
5. Expressions (cont’)
EXAMPLE 2.5
•Sem 1 20202021 •5
5. Expressions (cont’)
EXAMPLE 2.6
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
•Sem 1 20202021 •6
5. Expressions (cont’)
TWO rules apply when evaluating mixed expressions:
1. When evaluating an operator in a mixed expression:
a) If the operator has the same types of operands, the
operator is evaluated according to the type of the
operands. Integer operands yield an integer result, f-point
operands yield a f-point result.
•Sem 1 20202021 •7
5. Expressions (cont’)
TWO rules apply when evaluating mixed expressions: (cont’)
•Sem 1 20202021 •8
5. Expressions (cont’)
EXAMPLE 2.7
3 / 2 + 5.5 = 6.5
Rule applied
Rule 1(a): 3 / 2 = 1 (integer division)
1 + 5.5
Rule 1(b): 1.0 + 5.5
6.5
•Sem 1 20202021 •9
5. Expressions (cont’)
EXAMPLE 2.8
4 * 3 + 7 / 5 – 25.5 = -12.5
Rule applied
Rule 1(a): 4 * 3 = 12
Rule 1(a): 7 / 5 = 1 (integer division)
Rule 1(a): 12 + 1 = 13
13 – 25.5
Rule 1(b): 13.0 - 25.5
-12.5
•Sem 1 20202021 •10
5. Type Conversion
Produce
static_cast<dataTypeName> (expression)
E.g.:
static_cast<int> (7.9)
•acceptable (int) 7.9
•Sem 1 20202021 •13
6. Type Conversion (cont’)
EXAMPLE 2.9:
Expression Evaluates to
static_cast<int> (7.9) 7
static_cast<double> (8+2) 10
static_cast<double> (15/2) 7
static_cast<double> (15)/2 7.5
2 types of data:
1. Constant : - Data is fixed throughout program
execution.
2. Variables:- Data is modified during program
execution.
General Format:
Example:
const double PI = 3.14;
const double DISKAUN = 0.80;
const int NO_OF_STUDENT = 100;
const char FLAG =1;
const double INTEREST_RATE=15.75;
Example:
#define PI 3.14
#define DISKAUN 0.80
#define NO_OF_STUDENT 100
#define FLAG 1
#define INTEREST_RATE 15.75
Example:
variable = expression;
Example:
num1 = 4;
Cyl_area = 2*PI*r*(r+h);
initial_name = ‘Z’;
Quick Review : Suppose that n1, n2, and n3 are int variables
and the following segment codes are executed in sequence.
Example:
8. Output (cont’)
Example Code:
expression
manipulator
•Sem 1 20202021 •28
8. Output (cont’)
Another Code:
Q1:
cout << “ Welcome to 2BMy Bank.\n Enter your password”;
Q2:
cout << “ Welcome to 2BMy Bank.”;
cout << “\n Enter your password”;
Quick Review: Predict the output for the following segment code.
Q3:
cout << “ Welcome to 2BMy Bank.”;
cout << endl << “Enter your password”;
Q4:
cout << “ Welcome to 2BMy Bank.”<< endl << “Enter your password” <<;
#include<fstream>
#include<string>
// Read input from a keyboard then
// display it on a screen. #include<iostream>
#include<iostream> using namespace std;
#include<string> void main()
using namespace std;
{
void main() string a;
{ ifstream ip; //declares as input file
string a;
ip.open(“C:\\res.txt”);
cout<<“Enter a string:”;
cin>>a; //read frm keyboard
ip>>a; //read from file; res.txt
cout<<a; //display on screen
cout<<a; //display it on screen
cout<<endl;
} ip.close();
}
•Sem 1 20202021 •34
9. Increment and Decrement Operators
Increment operator ++
Increases the value of a variable by 1.
Decrement operator --
Decreases the value of a variable by 1.
Example1: Example2:
int x, y; int x, y;
x = 5; x=5;
y=x++ ; y = 6;
y = --y + ++x;
Q1: Q2:
int x, y, z; int x, y, z;
x = 5; x = 5;
y = --x + x++ ; y = x++ + --x ;
z = x; z = x;
#include <headerFileName>
Example: #include <iostream>
#include <cmath>
#include <string>
#include <cstring>
•Sem 1 20202021 •41
10. Preprocessor Directives (cont’)
Reserved words: using and namespace
used to access identifiers in C++ header files.
#include <iostream>
void main( )
{ //begin of main function
char name[25];
std::cout << "Please enter your name>>";
std::cin >> name;
std::cout << std::endl;
#include <iostream>
using namespace std;
void main( )
{//begin of main function
char name[25];
cout << "Please enter your name>>";
cin >> name;
cout << endl;
Fix the
error
Write the code The output of the program
Execute
Program
Compile
Build