Nexus Learn

Practical 3: Operators & Expressions

Lab Practical CS-307 Programming Fundamentals 10 min read Updated 8 Nov 2026
On this page
  1. 1. Lab Objectives
  2. 2. Word Bank (Difficult Words Made Easy)
  3. 3. Background: From Calculation to Decision
  4. 4. Time Plan
  5. Part A: Predict the Output (15 min)
  6. Part B: Print a Truth Table (10 min)
  7. Part C: Bug Hunt — Five Silent Bugs (25 min)
  8. Part D: Graded Lab Tasks (60 min)
  9. Part E: Post-Lab Questions
  10. Marking Rubric (20 marks)
  11. Submission

Duration

Language

Tools

Linked Lecture

120 minutes

C++

Dev-C++ or Code::Blocks (online compiler as backup)

Lectures 3 and 4

Mapped outcomes: CLO4 (C++ syntax and semantics) and CLO1 (problem solving) at psychomotor level P3 – Guided Response · PLO2, PLO3, PLO5

1. Lab Objectives

By the end of this lab, you will be able to:

  • Predict the value of expressions that use precedence, ++/-- and casting.
  • Print results neatly with fixed, setprecision and boolalpha.
  • Use relational (== != < > <= >=) and logical (&& || !) operators to build conditions.
  • Turn real-world rules (result card, electricity bill, leap year, admission) into C++ expressions.
  • Find and fix bugs that compile but give wrong answers.

2. Word Bank (Difficult Words Made Easy)

Word

Simple meaning

Real-world example

Expression

Operands and operators that give one value.

price * qty + tax on a shop bill.

Precedence

Which operator runs first.

BODMAS in maths: multiply before add.

Cast

Changing the type of a value on purpose.

Converting rupees to dollars before adding them.

Condition

An expression whose answer is true or false.

"Is the student 18 or older?"

Relational operator

Compares two values (==, <, >= …).

Comparing two cricket scores.

Logical operator

Joins conditions with AND, OR, NOT.

"CNIC AND age ≥ 18" to get a passport.

Truth table

A table of all inputs and results.

A chart showing when a lift door opens.

boolalpha

Prints true/false instead of 1/0.

A form that says "Yes/No" instead of "1/0".

Tolerance

A very small allowed difference.

A shopkeeper's scale that allows ±1 gram.

3. Background: From Calculation to Decision

Lecture 3 taught the rules of calculation (precedence, ++, casting). Lecture 4 taught questions whose answers are true/false. In this lab you will use both together: first calculate a value (a percentage, a bill), then ask a question about it (passed? protected consumer?).

Priority

Operators

1 (highest)

( )

2

! ++ -- cast

3

* / %

4

+ -

5

< <= > >=

6

== !=

7

&&

8

||

9 (lowest)

= += -= …

4. Time Plan

Part

Activity

Time

A

Predict the output (calculation)

15 min

B

Print a truth table

10 min

C

Bug hunt: five silent bugs

25 min

D

Graded lab tasks

60 min

E

Post-lab questions and submission

10 min

Part A: Predict the Output (15 min)

Write your prediction for every line before running the program. Then run it and fill the last column.

lab3_predict.cpp

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

int a = 9, b = 4, c, d;

cout << a + b * 2 << endl; // 1

cout << (a + b) * 2 << endl; // 2

cout << a / b * b + a % b << endl; // 3

cout << static_cast<double>(a) / b << endl; // 4

cout << static_cast<double>(a / b) << endl; // 5

c = a++;

cout << a << " " << c << endl; // 6

d = --b;

cout << b << " " << d << endl; // 7

cout << fixed << setprecision(2) << 10.0 / 3 << endl; // 8

return 0;

}

#

Line

My prediction

Actual output

1

a + b * 2

2

(a + b) * 2

3

a / b * b + a % b

4

static_cast<double>(a) / b

5

static_cast<double>(a / b)

6

c = a++; a, c

7

d = --b; b, d

8

fixed, setprecision(2), 10.0 / 3

Part B: Print a Truth Table (10 min)

Type and run this program. Compare its output with the truth table from Lecture 4 (Slide L04-S11).

lab3_truth.cpp

#include <iostream>

using namespace std;

int main() {

bool A, B;

cout << boolalpha;

cout << "A\tB\tA&&B\tA||B\t!A" << endl;

A = false; B = false;

cout << A << "\t" << B << "\t" << (A && B) << "\t" << (A || B) << "\t" << !A << endl;

A = false; B = true;

cout << A << "\t" << B << "\t" << (A && B) << "\t" << (A || B) << "\t" << !A << endl;

A = true; B = false;

cout << A << "\t" << B << "\t" << (A && B) << "\t" << (A || B) << "\t" << !A << endl;

A = true; B = true;

cout << A << "\t" << B << "\t" << (A && B) << "\t" << (A || B) << "\t" << !A << endl;

return 0;

}

Output

A B A&&B A||B !A

false false false false true

false true false true true

true false false true false

true true true true false

Try it: add a column for !(A && B) and another for !A || !B. What do you notice?

My observation

Part C: Bug Hunt — Five Silent Bugs (25 min)

Every program below compiles and runs but gives a wrong answer. Run each one with the given input, write the wrong output, find the bug and fix it.

Bug 1 (bug1.cpp) — Input 75

#include <iostream>

using namespace std;

int main() {

int marks;

cout << "Enter marks: ";

cin >> marks;

bool full = (marks = 100);

cout << "Full marks? " << full << endl;

return 0;

}

Wrong output, cause and fix

Bug 2 (bug2.cpp) — Input 45

#include <iostream>

using namespace std;

int main() {

int temp;

cout << "Enter temperature: ";

cin >> temp;

bool pleasant = 20 <= temp <= 30;

cout << "Pleasant? " << pleasant << endl;

return 0;

}

Wrong output, cause and fix

Bug 3 (bug3.cpp) — No input

#include <iostream>

using namespace std;

int main() {

int obtained = 437, total = 550;

double percentage = obtained / total * 100;

cout << "Percentage: " << percentage << endl;

return 0;

}

Wrong output, cause and fix

Bug 4 (bug4.cpp) — No input

#include <iostream>

using namespace std;

int main() {

int side = 5;

int area = side ^ 2;

cout << "Area of square: " << area << endl;

return 0;

}

Wrong output, cause and fix

Bug 5 (bug5.cpp) — Input z

#include <iostream>

using namespace std;

int main() {

char ch;

cout << "Enter a small letter: ";

cin >> ch;

bool vowel = ch == 'a' || 'e' || 'i' || 'o' || 'u';

cout << "Vowel? " << vowel << endl;

return 0;

}

Wrong output, cause and fix

Part D: Graded Lab Tasks (60 min)

Rules for every task

Use const for fixed numbers (pass mark, rates).

Print money and percentages with 2 decimal places.

Print every condition result with boolalpha (true/false).

Test with the sample input and at least one input of your own.

Task 1: Semester Result Card (Easy — 3 marks)

Input the marks (out of 100) of five first-semester subjects. Print the total out of 500, the percentage (2 decimals), whether the student passed all subjects (pass mark 40 in each), and whether the student earned a distinction (80% or more AND passed all).

Sample run

Enter marks (out of 100) for PF, Calculus, English, Physics, ICT: 78 65 82 39 90

Total : 354 / 500

Percentage : 70.80%

Passed all : false

Distinction : false

Save as lab3_task1.cpp. Show the running program to your instructor.

Task 2: Electricity Bill (Medium — 4 marks)

Input the previous and current meter readings. Units = current − previous. Use these example rates as constants: Rs. 22.50 per unit, fixed charge Rs. 75, GST 18% on (energy + fixed charge). Print the units, each charge and the total bill, and whether the consumer is in the protected slab (200 units or less).

Sample run

Previous meter reading: 12450

Current meter reading : 12630

Units used : 180

Energy charge : Rs. 4050.00

Fixed charge : Rs. 75.00

GST (18%) : Rs. 742.50

Total bill : Rs. 4867.50

Protected consumer : true

Save as lab3_task2.cpp. Show the running program to your instructor.

Task 3: Leap Year Checker (Medium — 4 marks)

Input a year. Store three bool variables (divisible by 4, by 100, by 400), combine them into isLeap, and print all four. Use the ?: operator to print the number of days in February. Test with 2024, 1900, 2000 and 2026.

Sample run

Enter a year: 1900

Divisible by 4 : true

Divisible by 100 : true

Divisible by 400 : false

1900 is a leap year: false

Days in February : 28

Save as lab3_task3.cpp. Show the running program to your instructor.

Task 4: University Admission Eligibility (Medium — 4 marks)

A student is eligible if their age is between 17 and 25 (inclusive) AND either their FSc percentage is 60% or more OR they have a sports quota. Input age, FSc marks out of 1100 and sports quota (1/0). Print the FSc percentage and the results of the age check, the marks check and the final decision.

Sample run

Enter age: 19

Enter FSc marks (out of 1100): 600

Sports quota? (1/0): 1

FSc percentage : 54.55%

Age OK : true

Marks OK : false

ELIGIBLE : true

Save as lab3_task4.cpp. Show the running program to your instructor.

Task 5: Triangle Inspector (Challenge — 5 marks)

Input three sides (double). Print whether they form a valid triangle (all sides > 0 and the sum of any two sides is greater than the third), and whether it is equilateral, isosceles and right-angled. For the right angle, do not use == on doubles — compare with a tolerance: fabs(a*a + b*b - c*c) < 1e-9 (check all three arrangements).

Sample run

Enter three sides: 3 4 5

Valid triangle : true

Equilateral : false

Isosceles : false

Right-angled : true

Save as lab3_task5.cpp. Show the running program to your instructor.

Part E: Post-Lab Questions

Q1. Why does static_cast<double>(a / b) not give the decimal answer, while static_cast<double>(a) / b does?

Answer

Q2. Explain why 20 <= temp <= 30 is always true in C++.

Answer

Q3. In Task 4, what goes wrong if you write ageOk && marksOk || sportsQuota without brackets?

Answer

Q4. Why should you not compare two doubles with ==? What should you do instead?

Answer

Q5. Write the opposite (NOT) of age >= 18 && hasCnic without using ! in front of brackets.

Answer

Marking Rubric (20 marks)

Component

Marks

What earns full marks

Task 1

3

Correct total and percentage (cast); passedAll uses && for all five subjects.

Task 2

4

Correct units, charges and GST; constants used; 2 decimal places; protected-slab bool.

Task 3

4

Three named bools combined correctly; ?: for February; tested with four years.

Task 4

4

Brackets correct: ageOk && (marksOk || sportsQuota); percentage with decimals.

Task 5

5

Validity check; all three types correct; right angle uses a tolerance, not ==.

Note: Parts A–C and the post-lab questions are checked in the viva. A program that does not compile gets at most half marks for that task.

Submission

  • Show your running programs to the instructor before leaving the lab.
  • Zip lab3_task1.cpp – lab3_task5.cpp in a folder named with your roll number and submit as instructed.
  • Copy the final code and output of each task into your Lab Book (Practical 3 pages) and get it signed.
  • Copying code from a classmate gets zero marks for both students.

Next practical: From Algorithm & Flowchart to Code — sequential programs (linked to Lectures 5 and 6).

← All CS-307 material