ITCS 103 | Programming Fundamentals | Lab 5
Decision Making in C++
اتخاذ القرارات في لغة سي بلس بلس
if / else Logical Operators Relational Operators if-else-if ladder

Bilingual Lab Booklet | Full Complete Code (VS Code Theme)

Dr. Lubna Fayez Eliyan · College of IT

✔️ Complete Solutions with #include & main()
🎯 Objectives:
✓ Work with relational operators (==, !=, <, >, <=, >=)
✓ Use logical operators (&&, ||, !)
✓ Implement if, if-else, and if-else-if statements
🎯 أهداف التعلم:
✓ التعامل مع المعاملات العلائقية
✓ استخدام المعاملات المنطقية
✓ تنفيذ جمل if و if-else و if-else-if
⚡ if statement
if (condition) {
    // executes if true
}
🔄 if-else statement
if (condition) {
    // true block
} else {
    // false block
}
🔢 Relational Operatorsالمعاملات العلائقية
OperatorMeaning (EN)المعنى (AR)
>greater thanأكبر من
<less thanأصغر من
>=greater or equalأكبر أو يساوي
<=less or equalأصغر أو يساوي
==equal toيساوي
!=not equalلا يساوي
🧠 Logical Operatorsالمعاملات المنطقية
OperatorExampleDescription
&&(x>5) && (x<10)Both true → true
||(x<2) || (x>8)At least one true → true
!!(x==5)Negation (x not equal 5)
📊 avg.cpp – Complete Program (Average & Pass/Fail)برنامج المعدل كاملاً
#include <iostream>
#include <string>
using namespace std;

int main() {
    string full_name;
    double grade1, grade2, grade3, avg;
    cout << "Please enter your name: ";
    getline(cin, full_name);
    cout << "Enter the grade of the first course: ";
    cin >> grade1;
    cout << "Enter the grade of the second course: ";
    cin >> grade2;
    cout << "Enter the grade of the third course: ";
    cin >> grade3;
    avg = (grade1 + grade2 + grade3) / 3.0;
    cout << "Your average = " << avg << endl;
    if (avg >= 60)
        cout << full_name << " Congrats!.. You passed the semester!" << endl;
    else
        cout << full_name << " You failed the course, try harder next time!" << endl;
    return 0;
}
🧪 pH Classifier – Complete Programبرنامج تصنيف pH كاملاً
#include <iostream>
using namespace std;

int main() {
    double pH;
    cout << "Enter pH please: ";
    cin >> pH;
    cout << "The solution is : ";
    if (pH < 3)
        cout << "very acidic." << endl;
    else if (pH >= 3 && pH < 7)
        cout << "acidic." << endl;
    else
        cout << "alkaline." << endl;
    return 0;
}
Difference with separate ifs: Using separate ifs would evaluate all conditions even after a match → slower and inefficient. else-if chain stops at first true condition → more efficient.
🔢 Exercise 1: Even or Odd – Full Programتمرين 1: زوجي أو فردي – البرنامج كاملاً
#include <iostream>
using namespace std;

int main() {
    int N;
    cout << "Enter an integer: ";
    cin >> N;
    if (N % 2 == 0)
        cout << N << " is even number" << endl;
    else
        cout << N << " is odd number" << endl;
    return 0;
}
🔢 Exercise 2: Positive, Negative or Zero – Full Programتمرين 2: موجب، سالب أو صفر – البرنامج كاملاً
#include <iostream>
using namespace std;

int main() {
    int N;
    cout << "Enter an integer: ";
    cin >> N;
    if (N > 0)
        cout << N << " is positive" << endl;
    else if (N < 0)
        cout << N << " is negative" << endl;
    else
        cout << N << " is ZERO" << endl;
    return 0;
}
💰 Exercise 3: Income Tax – Full Programتمرين 3: حساب الضريبة – البرنامج كاملاً
#include <iostream>
using namespace std;

int main() {
    double income, tax = 0;
    int children;
    cout << "Enter total income: ";
    cin >> income;
    cout << "Enter number of children: ";
    cin >> children;
    if (income < 30000)
        tax = income * 0.10;
    else if (income >= 30000 && children >= 3)
        tax = income * 0.20;
    else if (income >= 30000 && children < 3)
        tax = income * 0.30;
    cout << "Tax to pay: " << tax << endl;
    return 0;
}
📊 Exercise 4: Letter Grade – Full Program with Validationتمرين 4: تحويل الدرجة إلى حرف – البرنامج كاملاً
#include <iostream>
using namespace std;

int main() {
    int score;
    cout << "Enter the score: ";
    cin >> score;
    if (score < 0 || score > 100)
        cout << "Invalid input, value must be between ZERO and 100." << endl;
    else if (score >= 90)
        cout << "Grade is A" << endl;
    else if (score >= 80)
        cout << "Grade is B" << endl;
    else if (score >= 70)
        cout << "Grade is C" << endl;
    else if (score >= 60)
        cout << "Grade is D" << endl;
    else
        cout << "Grade is F" << endl;
    return 0;
}
📐 Quadratic Equation Roots – Full Programجذور المعادلة التربيعية – برنامج كامل
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double a, b, c, disc, root1, root2;
    cout << "Enter coefficients a, b, c: ";
    cin >> a >> b >> c;
    disc = b*b - 4*a*c;
    if (disc >= 0) {
        root1 = (-b + sqrt(disc)) / (2*a);
        root2 = (-b - sqrt(disc)) / (2*a);
        cout << "Roots are: " << root1 << " and " << root2 << endl;
    } else {
        cout << "Roots are complex (discriminant negative)" << endl;
    }
    return 0;
}
🎲 Multiplication Game – Full Program (random 1-10)لعبة الضرب العشوائية – برنامج كامل
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    srand(time(0));
    int num1 = rand() % 10 + 1;
    int num2 = rand() % 10 + 1;
    int userAnswer, correct = num1 * num2;
    cout << "The generated numbers are " << num1 << " and " << num2 << endl;
    cout << num1 << " x " << num2 << " ? ";
    cin >> userAnswer;
    if (userAnswer == correct)
        cout << "Great! .. Correct answer" << endl;
    else
        cout << "Wrong answer.. The correct answer is " << correct << endl;
    return 0;
}
🏆 Largest of Three Numbers – Full Programأكبر ثلاثة أرقام – برنامج كامل
#include <iostream>
using namespace std;

int main() {
    double a, b, c, largest;
    cout << "Enter three numbers: ";
    cin >> a >> b >> c;
    if (a >= b && a >= c)
        largest = a;
    else if (b >= a && b >= c)
        largest = b;
    else
        largest = c;
    cout << "The Largest is " << largest << endl;
    return 0;
}
⚖️ BMI Calculator – Full Program with Warningحاسبة مؤشر كتلة الجسم – برنامج كامل
#include <iostream>
using namespace std;

int main() {
    double mass, height, bmi;
    cout << "Enter mass (kg): ";
    cin >> mass;
    cout << "Enter height (m): ";
    cin >> height;
    bmi = mass / (height * height);
    cout << "Your BMI = " << bmi << endl;
    if (bmi >= 30)
        cout << "You are severely overweight, start a diet now!" << endl;
    return 0;
}
📚 GPA Message System – Full Programنظام رسائل المعدل التراكمي – برنامج كامل
#include <iostream>
using namespace std;

int main() {
    double gpa;
    cout << "Enter GPA: ";
    cin >> gpa;
    if (gpa < 0 || gpa > 4.0)
        cout << "Out of range" << endl;
    else if (gpa >= 0.0 && gpa <= 0.99)
        cout << "Failed semester" << endl;
    else if (gpa >= 1.0 && gpa <= 1.99)
        cout << "On probation for next semester" << endl;
    else if (gpa >= 2.0 && gpa <= 2.99)
        cout << "(no message)" << endl;
    else if (gpa >= 3.0 && gpa <= 3.49)
        cout << "Dean's list for semester" << endl;
    else
        cout << "Highest honors for semester" << endl;
    return 0;
}
📐 Area: Rectangle or Triangle – Full Programمساحة مستطيل أو مثلث – برنامج كامل
#include <iostream>
using namespace std;

int main() {
    char shape;
    double base, height, area;
    cout << "Enter shape (R for rectangle, T for triangle): ";
    cin >> shape;
    cout << "Enter base and height: ";
    cin >> base >> height;
    if (shape == 'R' || shape == 'r')
        area = base * height;
    else if (shape == 'T' || shape == 't')
        area = 0.5 * base * height;
    else {
        cout << "Error: Invalid character! Only R or T allowed." << endl;
        return 1;
    }
    cout << "Area = " << area << endl;
    return 0;
}
📌 Key Takeaways – Don't Forget!📌 نقاط مهمة تذكرها
  • ✔ Relational operators compare values (>, <, ==, !=, >=, <=).
  • ✔ Logical operators (&&, ||, !) combine multiple conditions.
  • ✔ if-else if ladder stops evaluating after first true condition → more efficient.
  • ✔ Always validate input (e.g., GPA 0-4, score 0-100).
  • ✔ Use % modulus to check even/odd or divisibility.
  • ✔ For mutually exclusive ranges, use else-if rather than separate ifs.
  • ✔ In tax problems, combine income and children with &&.
  • ✔ Quadratic discriminant: if b²-4ac ≥ 0 → real roots, else complex.
💡 Final Advice: Always use curly braces {} for readability even with single statements. Remember that = is assignment, == is comparison.
⭐ Summary: Decision making is the backbone of logic. Use if when a condition may be false, if-else for two branches, else-if for multiple exclusive paths. Logical operators enable complex decisions.
⭐ الخلاصة: اتخاذ القرار هو أساس المنطق البرمجي. استخدم if للشروط الفردية، if-else لفرعين، وelse-if لتفرعات متعددة حصرية. المعاملات المنطقية تتيح قرارات مركبة.
📖 Common Mistakes to Avoid⚠️ أخطاء شائعة تجنبها
  • Using = instead of == inside if condition (if(x=5) → always true).
  • Forgetting to handle invalid user input (out of range).
  • Not using parentheses with logical operators leads to wrong precedence.
  • Using multiple if statements when else-if ladder is correct.
  • Missing braces for multi-statement blocks.
✅ All lab exercises fully solved with complete #include and main() functions. Ready to compile and run.