SlideShare a Scribd company logo
Computer System & Programming
Instructor: Jahan Zeb
Department of Computer Engineering (DCE)
College of E&ME
NUST
Assignment Operators
 Assignment expression abbreviations
– Addition assignment operator
c = c + 3; abbreviated to
c += 3;
 Statements of the form
variable = variable operator expression;
can be rewritten as
variable operator= expression;
 Other assignment operators
d -= 4 (d = d - 4)
e *= 5 (e = e * 5)
f /= 3 (f = f / 3)
g %= 9 (g = g % 9)
Increment and Decrement Operators
 Increment operator (++)
– Increment variable by one
– c++
• Same as c += 1
 Decrement operator (--) similar
– Decrement variable by one
– c--
• Same as c -= 1
Increment and Decrement Operators
 Preincrement
– Variable changed before used in expression
• Operator before variable (++c or --c)
 Postincrement
– Incremented changed after expression
• Operator after variable (c++, c--)
Increment and Decrement Operators
 If c = 5, then
– cout << ++c;
• c is changed to 6, then printed out
– cout << c++;
• Prints out 5 (cout is executed before the increment.
• c then becomes 6
Increment and Decrement Operators
 When variable not in expression
– Preincrementing and postincrementing have same effect
++c;
cout << c;
and
c++;
cout << c;
are the same
1 // Fig. 2.14: fig02_14.cpp
2 // Preincrementing and postincrementing.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 // function main begins program execution
9 int main()
10 {
11 int c; // declare variable
12
13 // demonstrate postincrement
14 c = 5; // assign 5 to c
15 cout << c << endl; // print 5
16 cout << c++ << endl; // print 5 then postincrement
17 cout << c << endl << endl; // print 6
18
19 // demonstrate preincrement
20 c = 5; // assign 5 to c
21 cout << c << endl; // print 5
22 cout << ++c << endl; // preincrement then print 6
23 cout << c << endl; // print 6
24
25 return 0; // indicate successful termination
26
27 } // end function main
5
5
6
 
5
6
6
Essentials of Counter-Controlled Repetition
 Counter-controlled repetition requires
– Name of control variable/loop counter
– Initial value of control variable
– Condition to test for final value
– Increment/decrement to modify control variable when
looping
1 // Fig. 2.16: fig02_16.cpp
2 // Counter-controlled repetition.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 // function main begins program execution
9 int main()
10 {
11 int counter = 1; // initialization
12
13 while ( counter <= 10 ) { // repetition condition
14 cout << counter << endl; // display counter
15 ++counter; // increment
16
17 } // end while
18
19 return 0; // indicate successful termination
20
21 } // end function main
1
2
3
4
5
6
7
8
9
10
for Repetition Structure
 General format when using for loops
for ( initialization; LoopContinuationTest;
increment )
statement
 Example
for( int counter = 1; counter <= 10; counter++ )
cout << counter << endl;
– Prints integers from one to ten
No
semicolon
after last
statement
1 // Fig. 2.17: fig02_17.cpp
2 // Counter-controlled repetition with the for structure.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 // function main begins program execution
9 int main()
10 {
11 // Initialization, repetition condition and incrementing
12 // are all included in the for structure header.
13
14 for ( int counter = 1; counter <= 10; counter++ )
15 cout << counter << endl;
16
17 return 0; // indicate successful termination
18
19 } // end function main
1
2
3
4
5
6
7
8
9
10
for Repetition Structure
 for loops can usually be rewritten as while loops
initialization;
while ( loopContinuationTest){
statement
increment;
}
 Initialization and increment
– For multiple variables, use comma-separated lists
for (int i = 0, j = 0; j + i <= 10; j++, i++)
cout << j + i << endl;
1 // Fig. 2.20: fig02_20.cpp
2 // Summation with for.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 // function main begins program execution
9 int main()
10 {
11 int sum = 0; // initialize sum
12
13 // sum even integers from 2 through 100
14 for ( int number = 2; number <= 100; number += 2 )
15 sum += number; // add number to sum
16
17 cout << "Sum is " << sum << endl; // output sum
18 return 0; // successful termination
19
20 } // end function main
Sum is 2550
Examples Using the for Structure
 Program to calculate compound interest
 A person invests Rs1000.00 in a savings account yielding 5 percent
interest. Assuming that all interest is left on deposit in the account,
calculate and print the amount of money in the account at the end of
each year for 10 years. Use the following formula for determining
these amounts:
a = p(1+r)
 p is the original amount invested (i.e., the principal),
r is the annual interest rate,
n is the number of years and
a is the amount on deposit at the end of the nth year
n
1 // Fig. 2.21: fig02_21.cpp
2 // Calculating compound interest.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7 using std::ios;
8
9
10 #include <iomanip>
11
12 using std::setw;
13 using std::setprecision;
14
15 #include <cmath> // enables program to use function pow
16
17 // function main begins program execution
18 int main()
19 {
20 double amount; // amount on deposit
21 double principal = 1000.0; // starting principal
22 double rate = .05; // interest rate
23
<cmath> header needed for
the pow function (program
will not compile without it).
24 // output table column heads
25 cout << "Year" << setw( 21 ) << "Amount on deposit" << endl;
26
27 // set floating-point number format
28 cout << setprecision( 2 );
29
30 // calculate amount on deposit for each of ten years
31 for ( int year = 1; year <= 10; year++ ) {
32
33 // calculate new amount for specified year
34 amount = principal * pow( 1.0 + rate, year );
35
36 // output one table row
37 cout << setw( 4 ) << year
38 << setw( 21 ) << amount << endl;
39
40 } // end for
41
42 return 0; // indicate successful termination
43
44 } // end function main
pow(x,y) = x raised to the
yth power.
Sets the field width to 21
characters, right-justified.
Year Amount on deposit
1 1050.00
2 1102.50
3 1157.63
4 1215.51
5 1276.28
6 1340.10
7 1407.10
8 1477.46
9 1551.33
10 1628.89
Numbers are right-justified
due to setw statements (at
positions 4 and 21).
do/while Repetition Structure
 Similar to while structure
– Makes loop continuation test at end, not beginning
– Loop body executes at least once
 Format
do {
statement
} while ( condition );
true
false
action(s)
condition
1 // Fig. 2.24: fig02_24.cpp
2 // Using the do/while repetition structure.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 // function main begins program execution
9 int main()
10 {
11 int counter = 1; // initialize counter
12
13 do {
14 cout << counter << " "; // display counter
15 } while ( ++counter <= 10 ); // end do/while
16
17 cout << endl;
18
19 return 0; // indicate successful termination
20
21 } // end function main
1 2 3 4 5 6 7 8 9 10
Logical Operators
 Used as conditions in loops, if statements
 && (logical AND)
– true if both conditions are true
if ( student == 1 && marks >= 70 )
++goodstudents;
 || (logical OR)
– true if either of condition is true
if ( semesterAverage >= 90 || finalExam >= 90 )
cout << "Student grade is A" << endl;
Logical Operators
 ! (logical NOT, logical negation)
– Returns true when its condition is false, & vice versa
if ( !( grade == sentinelValue ) )
cout << "The next grade is " << grade << endl;
Alternative:
if ( grade != sentinelValue )
cout << "The next grade is " << grade << endl;
Confusing Equality (==) and Assignment (=)
Operators
 Common error
– Does not typically cause syntax errors
 Aspects of problem
– Expressions that have a value can be used for decision
• Zero = false, nonzero = true
– Assignment statements produce a value (the value to be
assigned)
Confusing Equality (==) and Assignment (=)
Operators
 Example
if ( payCode == 4 )
cout << "You get a bonus!" << endl;
– If paycode is 4, bonus given
 If == was replaced with =
if ( payCode = 4 )
cout << "You get a bonus!" << endl;
– Paycode set to 4 (no matter what it was before)
– Statement is true (since 4 is non-zero)
– Bonus given in every case

More Related Content

What's hot

C++ Programming - 3rd Study
C++ Programming - 3rd StudyC++ Programming - 3rd Study
C++ Programming - 3rd Study
Chris Ohk
 
Mcrl2 by kashif.namal@gmail.com, adnanskyousafzai@gmail.com
Mcrl2 by kashif.namal@gmail.com, adnanskyousafzai@gmail.comMcrl2 by kashif.namal@gmail.com, adnanskyousafzai@gmail.com
Mcrl2 by kashif.namal@gmail.com, adnanskyousafzai@gmail.com
kashif kashif
 
C operators
C operators C operators
C operators
AbiramiT9
 
Matlab code of chapter 4
Matlab code of chapter 4Matlab code of chapter 4
Matlab code of chapter 4Mohamed El Kiki
 
C++
C++C++
C++20 features
C++20 features C++20 features
C++20 features
LogeekNightUkraine
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
AllNewTeach
 
C++ project
C++ projectC++ project
C++ project
Sonu S S
 
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
AKSHAY SACHAN
 
Simple c program
Simple c programSimple c program
Simple c programRavi Singh
 
openconfigd
openconfigdopenconfigd
openconfigd
Masakazu Asama
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)
Syed Umair
 
Removal Of Recursion
Removal Of RecursionRemoval Of Recursion
Removal Of Recursion
Richa Sharma
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
mohamed sikander
 
Manipulators in c++
Manipulators in c++Manipulators in c++
Manipulators in c++
Ashok Raj
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04hassaanciit
 
C lab programs
C lab programsC lab programs
C lab programs
Dr. Prashant Vats
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
Bharat Kalia
 

What's hot (20)

C++ Programming - 3rd Study
C++ Programming - 3rd StudyC++ Programming - 3rd Study
C++ Programming - 3rd Study
 
Mcrl2 by kashif.namal@gmail.com, adnanskyousafzai@gmail.com
Mcrl2 by kashif.namal@gmail.com, adnanskyousafzai@gmail.comMcrl2 by kashif.namal@gmail.com, adnanskyousafzai@gmail.com
Mcrl2 by kashif.namal@gmail.com, adnanskyousafzai@gmail.com
 
C operators
C operators C operators
C operators
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Matlab code of chapter 4
Matlab code of chapter 4Matlab code of chapter 4
Matlab code of chapter 4
 
C++
C++C++
C++
 
C++20 features
C++20 features C++20 features
C++20 features
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
 
C++ project
C++ projectC++ project
C++ project
 
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
 
Simple c program
Simple c programSimple c program
Simple c program
 
openconfigd
openconfigdopenconfigd
openconfigd
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)
 
Removal Of Recursion
Removal Of RecursionRemoval Of Recursion
Removal Of Recursion
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Manipulators in c++
Manipulators in c++Manipulators in c++
Manipulators in c++
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
 
C lab programs
C lab programsC lab programs
C lab programs
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 

Viewers also liked

Excel Based IP Functions
Excel Based IP FunctionsExcel Based IP Functions
Excel Based IP Functions
Rajiv Bhardwaj
 
Java Concurrency Idioms
Java Concurrency IdiomsJava Concurrency Idioms
Java Concurrency Idioms
Alex Miller
 
Operators in c language
Operators in c languageOperators in c language
Operators in c languageAmit Singh
 
Slides chapter 16
Slides chapter 16Slides chapter 16
Slides chapter 16
Priyanka Shetty
 
Elm intro
Elm introElm intro
Disruptor tools in action
Disruptor   tools in actionDisruptor   tools in action
Disruptor tools in actionMichael Barker
 
Trial set
Trial setTrial set
5 retro pay_methods_i___ii_part_5
5 retro pay_methods_i___ii_part_55 retro pay_methods_i___ii_part_5
5 retro pay_methods_i___ii_part_5
Oracle HRMS Functional Consultant
 
Hr development, methods and desig
Hr development, methods and desigHr development, methods and desig
Hr development, methods and desig
Prakash Dhakal
 
The 8051 assembly language
The 8051 assembly languageThe 8051 assembly language
The 8051 assembly languagehemant meena
 
Polymer & the web components revolution 6:25:14
Polymer & the web components revolution 6:25:14Polymer & the web components revolution 6:25:14
Polymer & the web components revolution 6:25:14
mattsmcnulty
 
Conflict Resolution In Kai
Conflict Resolution In KaiConflict Resolution In Kai
Conflict Resolution In Kai
Shunichi Shinohara
 
Downtown & Infill Tax Increment Districts: Strategies for Success
Downtown & Infill Tax Increment Districts: Strategies for SuccessDowntown & Infill Tax Increment Districts: Strategies for Success
Downtown & Infill Tax Increment Districts: Strategies for Success
Vierbicher
 
Appraisal and Performance Management in Schools - A practical approach
Appraisal and Performance Management in Schools - A practical approachAppraisal and Performance Management in Schools - A practical approach
Appraisal and Performance Management in Schools - A practical approach
Mark S. Steed
 
The Economics of Green Building
The Economics of Green BuildingThe Economics of Green Building
The Economics of Green Buildingnilskok
 
The Etsy Shard Architecture: Starts With S and Ends With Hard
The Etsy Shard Architecture: Starts With S and Ends With HardThe Etsy Shard Architecture: Starts With S and Ends With Hard
The Etsy Shard Architecture: Starts With S and Ends With Hard
jgoulah
 
mySQL - INSERT INTO
mySQL - INSERT INTOmySQL - INSERT INTO
mySQL - INSERT INTOlehrerfreund
 
Increment letter format
Increment letter formatIncrement letter format
Increment letter format
Deepti Joshi
 

Viewers also liked (20)

Excel Based IP Functions
Excel Based IP FunctionsExcel Based IP Functions
Excel Based IP Functions
 
Java Concurrency Idioms
Java Concurrency IdiomsJava Concurrency Idioms
Java Concurrency Idioms
 
Operators in c language
Operators in c languageOperators in c language
Operators in c language
 
Slides chapter 16
Slides chapter 16Slides chapter 16
Slides chapter 16
 
Elm intro
Elm introElm intro
Elm intro
 
Disruptor tools in action
Disruptor   tools in actionDisruptor   tools in action
Disruptor tools in action
 
Trial set
Trial setTrial set
Trial set
 
Control statements
Control statementsControl statements
Control statements
 
5 retro pay_methods_i___ii_part_5
5 retro pay_methods_i___ii_part_55 retro pay_methods_i___ii_part_5
5 retro pay_methods_i___ii_part_5
 
Hr development, methods and desig
Hr development, methods and desigHr development, methods and desig
Hr development, methods and desig
 
The 8051 assembly language
The 8051 assembly languageThe 8051 assembly language
The 8051 assembly language
 
Polymer & the web components revolution 6:25:14
Polymer & the web components revolution 6:25:14Polymer & the web components revolution 6:25:14
Polymer & the web components revolution 6:25:14
 
Conflict Resolution In Kai
Conflict Resolution In KaiConflict Resolution In Kai
Conflict Resolution In Kai
 
Agile Development
Agile DevelopmentAgile Development
Agile Development
 
Downtown & Infill Tax Increment Districts: Strategies for Success
Downtown & Infill Tax Increment Districts: Strategies for SuccessDowntown & Infill Tax Increment Districts: Strategies for Success
Downtown & Infill Tax Increment Districts: Strategies for Success
 
Appraisal and Performance Management in Schools - A practical approach
Appraisal and Performance Management in Schools - A practical approachAppraisal and Performance Management in Schools - A practical approach
Appraisal and Performance Management in Schools - A practical approach
 
The Economics of Green Building
The Economics of Green BuildingThe Economics of Green Building
The Economics of Green Building
 
The Etsy Shard Architecture: Starts With S and Ends With Hard
The Etsy Shard Architecture: Starts With S and Ends With HardThe Etsy Shard Architecture: Starts With S and Ends With Hard
The Etsy Shard Architecture: Starts With S and Ends With Hard
 
mySQL - INSERT INTO
mySQL - INSERT INTOmySQL - INSERT INTO
mySQL - INSERT INTO
 
Increment letter format
Increment letter formatIncrement letter format
Increment letter format
 

Similar to Lecture#5 Operators in C++

Chapter 3 Control structures.ppt
Chapter 3 Control structures.pptChapter 3 Control structures.ppt
Chapter 3 Control structures.ppt
RahulBorate10
 
Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.ppt
sanjay
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx
AqeelAbbas94
 
Control Structures: Part 2
Control Structures: Part 2Control Structures: Part 2
Control Structures: Part 2
Andy Juan Sarango Veliz
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05
HUST
 
03b loops
03b   loops03b   loops
03b loops
Manzoor ALam
 
BLM101_2.pptx
BLM101_2.pptxBLM101_2.pptx
BLM101_2.pptx
ssuser4fbfbf
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
NUST Stuff
 
Contoh program c++ kalkulator
Contoh program c++ kalkulatorContoh program c++ kalkulator
Contoh program c++ kalkulator
JsHomeIndustry
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
yash production
 
Few Operator used in c++
Few Operator used in c++Few Operator used in c++
Few Operator used in c++
sunny khan
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
Pro.docx
Pro.docxPro.docx
Lecture 3
Lecture 3Lecture 3
Lecture 3
Mohammed Saleh
 
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
andreaplotner1
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2sajidpk92
 
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
katherncarlyle
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
msharshitha03s
 

Similar to Lecture#5 Operators in C++ (20)

Chapter 3 Control structures.ppt
Chapter 3 Control structures.pptChapter 3 Control structures.ppt
Chapter 3 Control structures.ppt
 
Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.ppt
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx
 
Control Structures: Part 2
Control Structures: Part 2Control Structures: Part 2
Control Structures: Part 2
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05
 
03b loops
03b   loops03b   loops
03b loops
 
BLM101_2.pptx
BLM101_2.pptxBLM101_2.pptx
BLM101_2.pptx
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Contoh program c++ kalkulator
Contoh program c++ kalkulatorContoh program c++ kalkulator
Contoh program c++ kalkulator
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Few Operator used in c++
Few Operator used in c++Few Operator used in c++
Few Operator used in c++
 
Lecture04
Lecture04Lecture04
Lecture04
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
Pro.docx
Pro.docxPro.docx
Pro.docx
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2
 
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 

More from NUST Stuff

Me211 1
Me211 1Me211 1
Me211 1
NUST Stuff
 
Lab LCA 1 7
Lab LCA 1 7Lab LCA 1 7
Lab LCA 1 7
NUST Stuff
 
Me211 4
Me211 4Me211 4
Me211 4
NUST Stuff
 
Me211 3
Me211 3Me211 3
Me211 3
NUST Stuff
 
Drag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluidDrag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluid
NUST Stuff
 
Nums entry test 2017 paper
Nums entry test 2017 paperNums entry test 2017 paper
Nums entry test 2017 paper
NUST Stuff
 
Nums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bdsNums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bds
NUST Stuff
 
MCAT Full length paper 8-student_copy_
MCAT Full length paper  8-student_copy_MCAT Full length paper  8-student_copy_
MCAT Full length paper 8-student_copy_
NUST Stuff
 
MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_
NUST Stuff
 
MCAT Full length paper 6-student_copy_
MCAT Full length paper  6-student_copy_MCAT Full length paper  6-student_copy_
MCAT Full length paper 6-student_copy_
NUST Stuff
 
MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_
NUST Stuff
 
Mcat (original paper 2014)
Mcat (original paper 2014)Mcat (original paper 2014)
Mcat (original paper 2014)
NUST Stuff
 
MCAT Full length paper 4-student_copy
MCAT Full length paper  4-student_copyMCAT Full length paper  4-student_copy
MCAT Full length paper 4-student_copy
NUST Stuff
 
mcat (original paper 2013)
mcat (original paper 2013)mcat (original paper 2013)
mcat (original paper 2013)
NUST Stuff
 
mcat (original paper 2012)
mcat (original paper 2012)mcat (original paper 2012)
mcat (original paper 2012)
NUST Stuff
 
MCAT Full length paper 3 final
MCAT Full length paper 3 finalMCAT Full length paper 3 final
MCAT Full length paper 3 final
NUST Stuff
 
MCAT (original paper 2011)
MCAT (original paper 2011)MCAT (original paper 2011)
MCAT (original paper 2011)
NUST Stuff
 
mcat (original paper 2010)
 mcat (original paper 2010) mcat (original paper 2010)
mcat (original paper 2010)
NUST Stuff
 
MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)
NUST Stuff
 

More from NUST Stuff (20)

Me211 1
Me211 1Me211 1
Me211 1
 
Lab LCA 1 7
Lab LCA 1 7Lab LCA 1 7
Lab LCA 1 7
 
Me211 2
Me211 2Me211 2
Me211 2
 
Me211 4
Me211 4Me211 4
Me211 4
 
Me211 3
Me211 3Me211 3
Me211 3
 
Drag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluidDrag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluid
 
Nums entry test 2017 paper
Nums entry test 2017 paperNums entry test 2017 paper
Nums entry test 2017 paper
 
Nums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bdsNums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bds
 
MCAT Full length paper 8-student_copy_
MCAT Full length paper  8-student_copy_MCAT Full length paper  8-student_copy_
MCAT Full length paper 8-student_copy_
 
MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_
 
MCAT Full length paper 6-student_copy_
MCAT Full length paper  6-student_copy_MCAT Full length paper  6-student_copy_
MCAT Full length paper 6-student_copy_
 
MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_
 
Mcat (original paper 2014)
Mcat (original paper 2014)Mcat (original paper 2014)
Mcat (original paper 2014)
 
MCAT Full length paper 4-student_copy
MCAT Full length paper  4-student_copyMCAT Full length paper  4-student_copy
MCAT Full length paper 4-student_copy
 
mcat (original paper 2013)
mcat (original paper 2013)mcat (original paper 2013)
mcat (original paper 2013)
 
mcat (original paper 2012)
mcat (original paper 2012)mcat (original paper 2012)
mcat (original paper 2012)
 
MCAT Full length paper 3 final
MCAT Full length paper 3 finalMCAT Full length paper 3 final
MCAT Full length paper 3 final
 
MCAT (original paper 2011)
MCAT (original paper 2011)MCAT (original paper 2011)
MCAT (original paper 2011)
 
mcat (original paper 2010)
 mcat (original paper 2010) mcat (original paper 2010)
mcat (original paper 2010)
 
MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)
 

Recently uploaded

CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 

Recently uploaded (20)

CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 

Lecture#5 Operators in C++

  • 1. Computer System & Programming Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST
  • 2. Assignment Operators  Assignment expression abbreviations – Addition assignment operator c = c + 3; abbreviated to c += 3;  Statements of the form variable = variable operator expression; can be rewritten as variable operator= expression;  Other assignment operators d -= 4 (d = d - 4) e *= 5 (e = e * 5) f /= 3 (f = f / 3) g %= 9 (g = g % 9)
  • 3. Increment and Decrement Operators  Increment operator (++) – Increment variable by one – c++ • Same as c += 1  Decrement operator (--) similar – Decrement variable by one – c-- • Same as c -= 1
  • 4. Increment and Decrement Operators  Preincrement – Variable changed before used in expression • Operator before variable (++c or --c)  Postincrement – Incremented changed after expression • Operator after variable (c++, c--)
  • 5. Increment and Decrement Operators  If c = 5, then – cout << ++c; • c is changed to 6, then printed out – cout << c++; • Prints out 5 (cout is executed before the increment. • c then becomes 6
  • 6. Increment and Decrement Operators  When variable not in expression – Preincrementing and postincrementing have same effect ++c; cout << c; and c++; cout << c; are the same
  • 7. 1 // Fig. 2.14: fig02_14.cpp 2 // Preincrementing and postincrementing. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 int c; // declare variable 12 13 // demonstrate postincrement 14 c = 5; // assign 5 to c 15 cout << c << endl; // print 5 16 cout << c++ << endl; // print 5 then postincrement 17 cout << c << endl << endl; // print 6 18 19 // demonstrate preincrement 20 c = 5; // assign 5 to c 21 cout << c << endl; // print 5 22 cout << ++c << endl; // preincrement then print 6 23 cout << c << endl; // print 6
  • 8. 24 25 return 0; // indicate successful termination 26 27 } // end function main 5 5 6   5 6 6
  • 9. Essentials of Counter-Controlled Repetition  Counter-controlled repetition requires – Name of control variable/loop counter – Initial value of control variable – Condition to test for final value – Increment/decrement to modify control variable when looping
  • 10. 1 // Fig. 2.16: fig02_16.cpp 2 // Counter-controlled repetition. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 int counter = 1; // initialization 12 13 while ( counter <= 10 ) { // repetition condition 14 cout << counter << endl; // display counter 15 ++counter; // increment 16 17 } // end while 18 19 return 0; // indicate successful termination 20 21 } // end function main
  • 12. for Repetition Structure  General format when using for loops for ( initialization; LoopContinuationTest; increment ) statement  Example for( int counter = 1; counter <= 10; counter++ ) cout << counter << endl; – Prints integers from one to ten No semicolon after last statement
  • 13. 1 // Fig. 2.17: fig02_17.cpp 2 // Counter-controlled repetition with the for structure. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 // Initialization, repetition condition and incrementing 12 // are all included in the for structure header. 13 14 for ( int counter = 1; counter <= 10; counter++ ) 15 cout << counter << endl; 16 17 return 0; // indicate successful termination 18 19 } // end function main
  • 15. for Repetition Structure  for loops can usually be rewritten as while loops initialization; while ( loopContinuationTest){ statement increment; }  Initialization and increment – For multiple variables, use comma-separated lists for (int i = 0, j = 0; j + i <= 10; j++, i++) cout << j + i << endl;
  • 16. 1 // Fig. 2.20: fig02_20.cpp 2 // Summation with for. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 int sum = 0; // initialize sum 12 13 // sum even integers from 2 through 100 14 for ( int number = 2; number <= 100; number += 2 ) 15 sum += number; // add number to sum 16 17 cout << "Sum is " << sum << endl; // output sum 18 return 0; // successful termination 19 20 } // end function main Sum is 2550
  • 17. Examples Using the for Structure  Program to calculate compound interest  A person invests Rs1000.00 in a savings account yielding 5 percent interest. Assuming that all interest is left on deposit in the account, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula for determining these amounts: a = p(1+r)  p is the original amount invested (i.e., the principal), r is the annual interest rate, n is the number of years and a is the amount on deposit at the end of the nth year n
  • 18. 1 // Fig. 2.21: fig02_21.cpp 2 // Calculating compound interest. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 using std::ios; 8 9 10 #include <iomanip> 11 12 using std::setw; 13 using std::setprecision; 14 15 #include <cmath> // enables program to use function pow 16 17 // function main begins program execution 18 int main() 19 { 20 double amount; // amount on deposit 21 double principal = 1000.0; // starting principal 22 double rate = .05; // interest rate 23 <cmath> header needed for the pow function (program will not compile without it).
  • 19. 24 // output table column heads 25 cout << "Year" << setw( 21 ) << "Amount on deposit" << endl; 26 27 // set floating-point number format 28 cout << setprecision( 2 ); 29 30 // calculate amount on deposit for each of ten years 31 for ( int year = 1; year <= 10; year++ ) { 32 33 // calculate new amount for specified year 34 amount = principal * pow( 1.0 + rate, year ); 35 36 // output one table row 37 cout << setw( 4 ) << year 38 << setw( 21 ) << amount << endl; 39 40 } // end for 41 42 return 0; // indicate successful termination 43 44 } // end function main pow(x,y) = x raised to the yth power. Sets the field width to 21 characters, right-justified.
  • 20. Year Amount on deposit 1 1050.00 2 1102.50 3 1157.63 4 1215.51 5 1276.28 6 1340.10 7 1407.10 8 1477.46 9 1551.33 10 1628.89 Numbers are right-justified due to setw statements (at positions 4 and 21).
  • 21. do/while Repetition Structure  Similar to while structure – Makes loop continuation test at end, not beginning – Loop body executes at least once  Format do { statement } while ( condition ); true false action(s) condition
  • 22. 1 // Fig. 2.24: fig02_24.cpp 2 // Using the do/while repetition structure. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 int counter = 1; // initialize counter 12 13 do { 14 cout << counter << " "; // display counter 15 } while ( ++counter <= 10 ); // end do/while 16 17 cout << endl; 18 19 return 0; // indicate successful termination 20 21 } // end function main 1 2 3 4 5 6 7 8 9 10
  • 23. Logical Operators  Used as conditions in loops, if statements  && (logical AND) – true if both conditions are true if ( student == 1 && marks >= 70 ) ++goodstudents;  || (logical OR) – true if either of condition is true if ( semesterAverage >= 90 || finalExam >= 90 ) cout << "Student grade is A" << endl;
  • 24. Logical Operators  ! (logical NOT, logical negation) – Returns true when its condition is false, & vice versa if ( !( grade == sentinelValue ) ) cout << "The next grade is " << grade << endl; Alternative: if ( grade != sentinelValue ) cout << "The next grade is " << grade << endl;
  • 25. Confusing Equality (==) and Assignment (=) Operators  Common error – Does not typically cause syntax errors  Aspects of problem – Expressions that have a value can be used for decision • Zero = false, nonzero = true – Assignment statements produce a value (the value to be assigned)
  • 26. Confusing Equality (==) and Assignment (=) Operators  Example if ( payCode == 4 ) cout << "You get a bonus!" << endl; – If paycode is 4, bonus given  If == was replaced with = if ( payCode = 4 ) cout << "You get a bonus!" << endl; – Paycode set to 4 (no matter what it was before) – Statement is true (since 4 is non-zero) – Bonus given in every case