Slide 2
Quick Recap of Lecture 2
5 minutes · check homework
Remember
Homework check: seconds → h m s
- A variable is a named box in memory
- int, double, char, bool, string
- cin >> puts data INTO a variable
- cout << shows data on screen
- int / int gives an int: 7 / 2 = 3
- % gives the remainder: 7 % 2 = 1
- int s = 3725;
- int hours = s / 3600;
- int minutes = (s % 3600) / 60;
- int seconds = s % 60;
- // 1 h 2 m 5 s
Module 0 · Recap
Programming Fundamentals · Lecture 3
Slide 3
Today's Plan
90 minutes · 6 modules
Recap & Homework Check
5 min
Expressions & Operator Precedence
20 min
Shortcuts: +=, ++ and --
20 min
Type Conversion & Casting
20 min
Formatting Output, cmath & Programs
20 min
Wrap-up & Homework
5 min
Programming Fundamentals · Lecture 3
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
SLO 3.1 → CLO4 · C3
SLO 3.2 → CLO4 · C3
Evaluate arithmetic expressions using operator precedence and associativity
Use compound assignment (+=, -= …) and increment/decrement (prefix and postfix)
SLO 3.3 → CLO4 · C2
SLO 3.4 → CLO1 · C3
Explain implicit conversion and apply explicit casting with static_cast
Format output with fixed/setprecision and use cmath functions to solve real problems
- CLO1 Identify the necessary properties of good problem-solving techniques.
- CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 3
Slide 5
Operators, Operands & Expressions
The vocabulary we need today
bill = price * qty + tax;
Module 1 · Expressions & Precedence
Programming Fundamentals · Lecture 3
Slide 6
Operator Precedence
Which operator runs first? Higher rows run first
- Like BODMAS
- Brackets first, then × ÷ %, then + −.
- Same level? Go left to right.
- New words
- Precedence = priority (who goes first)
- Associativity = direction for equal priority
Rule of thumb: when unsure, use brackets. They cost nothing and make code easy to read.
Module 1 · Expressions & Precedence
Programming Fundamentals · Lecture 3
Slide 7
Evaluating Step by Step
2 + 3 * 4 - 8 / 2 % 3 = ?
Module 1 · Expressions & Precedence
Programming Fundamentals · Lecture 3
Slide 8
Brackets Change the Answer
A real bug: the average of three marks
average.cpp
Output
- int a = 70, b = 80, c = 90;
- cout << a + b + c / 3; // ?
- cout << (a + b + c) / 3; // ?
- 180
- 80
Why 180? Only c is divided: 70 + 80 + 30 = 180.
Module 1 · Expressions & Precedence
Programming Fundamentals · Lecture 3
Slide 9
Compound Assignment Operators
Shortcuts for "change a variable using its own value"
trace.cpp
- int n = 10;
- n += 5; // 15
- n *= 2; // 30
- n -= 4; // 26
- n /= 3; // 8
- n %= 4; // 0
- Real-world example: a mobile wallet
- balance += deposit; // money comes in
- balance -= billAmount; // money goes out
Module 2 · Shortcuts
Programming Fundamentals · Lecture 3
Slide 10
Increment ++ and Decrement --
Add 1 or subtract 1 — the most common change in programming
x++ or ++x
x-- or --x
same as x = x + 1
same as x = x - 1
Real-world: A gate counter: one more person enters
Real-world: Seats left on a bus after each booking
counter.cpp
- int score = 0;
- score++; score++; score--; // score is now 1
Module 2 · Shortcuts
Programming Fundamentals · Lecture 3
Slide 11
Prefix vs Postfix
The difference only matters when the value is USED in the same statement
Postfix: y = x++;
Prefix: y = ++x;
use first, then add
add first, then use
Never write x++ + ++x — changing x twice in one statement gives an undefined (unpredictable) result.
Module 2 · Shortcuts
Programming Fundamentals · Lecture 3
Slide 12
CLASS ACTIVITY · 7 MIN · IN PAIRS
Predict the Output
Write down what each line prints. Then we run it and check. One mark for each correct line!
Code
Hint
- int p = 10;
- cout << p++ << endl; // 1
- cout << p << endl; // 2
- cout << ++p << endl; // 3
- cout << p-- << endl; // 4
- cout << 17 / 4 * 4 + 17 % 4; // 5
- Postfix prints the OLD value.
- Prefix prints the NEW value.
- In line 5, * / % go left to right before +.
Slide 13
Implicit Conversion (Type Promotion)
C++ converts automatically when types are mixed
char
→
int
→
float
→
double
smaller type ───────────── promoted (moved up) to the bigger type ───────────── no data lost
Module 3 · Type Conversion
Programming Fundamentals · Lecture 3
Slide 14
Assigning to a Smaller Type Loses Data
double → int cuts off (truncates) the decimal part
narrowing.cpp
- Two traps
- 1. double → int: decimal part is cut off (87.9 → 87, not 88).
- 2. 7 / 2 is done in int FIRST (3), then stored in d.
- int marks = 87.9;
- cout << marks; // 87
- double d = 7 / 2;
- cout << d; // 3
- New word: truncate
- To truncate = to cut off the end. C++ never rounds when converting double to int; it just drops everything after the decimal point.
- To round properly, use round(87.9) from <cmath> → 88.
Module 3 · Type Conversion
Programming Fundamentals · Lecture 3
Slide 15
Explicit Casting with static_cast
Tell C++ yourself: "treat this value as another type"
cast.cpp
Output
- int total = 245, count = 3;
- double avg1 = static_cast<double>(total) / count;
- double avg2 = static_cast<double>(total / count);
- double avg3 = (double) total / count; // C-style
- avg1 = 81.6667
- avg2 = 81
- avg3 = 81.6667
avg2 is wrong: it casts AFTER the int division.
Module 3 · Type Conversion
Programming Fundamentals · Lecture 3
Slide 16
Characters Are Numbers Inside
Every char has a code number (ASCII)
ascii.cpp
- cout << 'A' + 1; // 66
- cout << static_cast<char>(66); // B
- char next = 'A' + 1;
- cout << next; // B
- Useful trick
- Lowercase to uppercase: subtract 32.
- char up = 'g' - 32; // 'G'
Module 3 · Type Conversion
Programming Fundamentals · Lecture 3
Slide 17
Formatting Decimal Output
#include <iomanip> — input/output manipulators
format.cpp
- #include <iomanip>
- cout << fixed << setprecision(2);
- cout << 1234.5 << endl; // 1234.50
- cout << 2.0 / 3 << endl; // 0.67
- cout << setw(8) << 42; // " 42"
- Why this matters
- Without fixed, large numbers can print as 1.25e+06 (scientific notation — 1.25 × 10⁶). Customers expect Rs. 1250000.00.
- Remember Practical 2, Task 3? fixed fixes that output.
Module 4 · Formatting, cmath & Programs
Programming Fundamentals · Lecture 3
Slide 18
Ready-Made Maths: <cmath>
Library functions do common calculations for you
- Warning
- 2 ^ 3 is NOT 8 in C++. It gives 1. Use pow(2, 3).
- Real-world
- ceil: buses needed for 130 students, 50 per bus → ceil(130 / 50.0) = 3 buses.
Module 4 · Formatting, cmath & Programs
Programming Fundamentals · Lecture 3
Slide 19
Program: Shop Bill with GST
Constants + arithmetic + formatting together
gst.cpp
Output
- int main() {
- const double GST_RATE = 0.18; // 18% sales tax
- double price;
- int qty;
- cout << "Enter unit price (Rs.): ";
- cin >> price;
- cout << "Enter quantity: ";
- cin >> qty;
- double subtotal = price * qty;
- double gst = subtotal * GST_RATE;
- double total = subtotal + gst;
- cout << fixed << setprecision(2);
- cout << "Subtotal : Rs. " << subtotal << endl;
- cout << "GST 18% : Rs. " << gst << endl;
- cout << "Total : Rs. " << total << endl;
- return 0;
- }
- Enter unit price (Rs.): 249.99
- Enter quantity: 4
- Subtotal : Rs. 999.96
- GST 18% : Rs. 179.99
- Total : Rs. 1179.95
- Map to IPO
- Input: price, qty
- Process: subtotal, gst, total
- Output: 3 lines, 2 decimals
Module 4 · Formatting, cmath & Programs
Programming Fundamentals · Lecture 3
Slide 20
Program: How Long Is the Ladder?
Pythagoras theorem with sqrt and pow
ladder.cpp
Output
- #include <cmath>
- ...
- double height, distance;
- cin >> height >> distance;
- double ladder = sqrt(pow(height, 2) + pow(distance, 2));
- cout << "Ladder length = " << ladder << " m" << endl;
- cout << "Buy a ladder of " << ceil(ladder) << " m";
- 6 2.5
- Ladder length = 6.5 m
- Buy a ladder of 7 m
? m ladder
6 m (wall)
2.5 m
Formula: c = √(a² + b²) → sqrt(pow(a, 2) + pow(b, 2))
Module 4 · Formatting, cmath & Programs
Programming Fundamentals · Lecture 3
Slide 21
Common Mistakes
Spot them before the compiler — or your teacher — does
!
Missing brackets
!
^ used as power
avg = a + b / 2;
area = r ^ 2;
Write (a + b) / 2.0
Use pow(r, 2) or r * r
!
!
Cast in the wrong place
Expecting rounding
static_cast<double>(a / b)
int n = 4.9; // 4
Cast first: static_cast<double>(a) / b
Use round(4.9) → 5
Module 4 · Formatting, cmath & Programs
Programming Fundamentals · Lecture 3
Slide 22
Quick Recap
Answer aloud
Q1
What is the value of 2 + 3 * 4 - 8 / 2 % 3 ?
Q2
If x = 5, what are x and y after y = x++; ?
Q3
Why does static_cast<double>(a / b) give the wrong average?
Q4
Which header gives us setprecision? Which gives sqrt?
Programming Fundamentals · Lecture 3
Slide 23
Homework
Input the radius of a circle; print its area and circumference with 2 decimal places (use const PI and pow)
Input obtained marks and total marks (both int); print the percentage correctly using static_cast
Input a loan amount, yearly rate and years; print the amount after compound interest: A = P(1 + r)ⁿ
Dry run by hand, then check on a PC: int a = 4, b = 3; a += b++; b *= --a; — final a and b?
Students on a trip: input students and bus capacity; print how many buses are needed (use ceil)
Next lecture: Comparison & logical operators — true/false questions for our programs