0% found this document useful (0 votes)
7 views5 pages

Lecture Loops

The document provides an overview of nested if-else statements and loops in C++, explaining their syntax, use cases, and real-life examples such as ATM withdrawal and vehicle refueling. It also includes a mini project for a banking system that allows users to deposit, withdraw, and check their balance using loops and conditions. Additionally, it lists assigned mini projects for further practice, including a student grading system and an ATM simulator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Lecture Loops

The document provides an overview of nested if-else statements and loops in C++, explaining their syntax, use cases, and real-life examples such as ATM withdrawal and vehicle refueling. It also includes a mini project for a banking system that allows users to deposit, withdraw, and check their balance using loops and conditions. Additionally, it lists assigned mini projects for further practice, including a student grading system and an ATM simulator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lecture: Nested If-Else and Loops in C++

Understanding the Topic


1. Nested If-Else

In C++, nested if-else statements refer to an if-else structure inside another if-else block. This
is used when multiple conditions need to be checked sequentially.

Why Use Nested If-Else?

• When we need to apply multiple conditions before making a decision.


• Used in scenarios such as traffic signal control, ATM withdrawal verification, or
grocery store discount calculations.

Syntax:

if (condition1) {
if (condition2) {
// Code block if both conditions are true
} else {
// Code block if condition1 is true but condition2 is false
}
} else {
// Code block if condition1 is false
}

Real-Life Example: ATM Withdrawal System

Scenario: A person tries to withdraw money from an ATM. The system checks if they have
sufficient balance and if the entered amount is within the daily withdrawal limit before
processing the transaction.

#include <iostream>
using namespace std;

int main() {
int balance = 5000;
int withdrawalAmount;
int dailyLimit = 3000;

cout << "Enter withdrawal amount: ";


cin >> withdrawalAmount;

if (withdrawalAmount <= balance) {


if (withdrawalAmount <= dailyLimit) {
cout << "Transaction successful!" << endl;
} else {
cout << "Amount exceeds daily withdrawal limit!" << endl;
}
} else {
cout << "Insufficient balance!" << endl;
}
return 0;}
Loops in C++
Loops allow executing a block of code multiple times, which is useful in automation,
calculations, and iterative processing.

Differences Between Loops

Loop Type When to Use Key Feature


For Loop When the number of iterations Executes a block of code a fixed number
is known. of times.
While Loop When the number of iterations Executes a block of code while the
is unknown. condition remains true.
Do-While When at least one iteration is Executes the block once before checking
Loop required. the condition.

For Loop

Syntax:

for (initialization; condition; update) {


// Code to execute in each iteration
}

Real-Life Example: Printing Multiplication Tables

#include <iostream>
using namespace std;

int main() {
int number;
cout << "Enter a number: ";
cin >> number;

for (int i = 1; i <= 10; i++) {


cout << number << " x " << i << " = " << number * i << endl;
}
return 0;
}

While Loop

Syntax:

while (condition) {
// Code to execute while condition is true
}
Real-Life Example: Refueling a Vehicle

#include <iostream>
using namespace std;

int main() {
int fuelLevel = 0;
int requiredFuel = 50;

while (fuelLevel < requiredFuel) {


cout << "Adding fuel... Current level: " << fuelLevel << " liters"
<< endl;
fuelLevel += 10;
}
cout << "Fuel tank is full!" << endl;
return 0;
}

Do-While Loop

Syntax:

do {
// Code to execute at least once
} while (condition);

Real-Life Example: Menu-Driven System for User Input

#include <iostream>
using namespace std;

int main() {
int choice;
do {
cout << "\nMenu:\n";
cout << "1. Start Game\n";
cout << "2. Settings\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
} while (choice < 1 || choice > 3);

cout << "You selected option " << choice << "\n";
return 0;
}
Mini Project: Banking System
Scenario:

• Allows users to deposit, withdraw, and check balance using loops and conditions.

#include <iostream>
using namespace std;

int main() {
int balance = 10000, choice, amount;
do {
cout << "\nBanking System Menu:\n";
cout << "1. Deposit\n";
cout << "2. Withdraw\n";
cout << "3. Check Balance\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter deposit amount: ";
cin >> amount;
balance += amount;
cout << "Deposit successful!\n";
break;
case 2:
cout << "Enter withdrawal amount: ";
cin >> amount;
if (amount > balance)
cout << "Insufficient balance!\n";
else {
balance -= amount;
cout << "Withdrawal successful!\n";
}
break;
case 3:
cout << "Your balance is: " << balance << "\n";
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice, try again!\n";
}
} while (choice != 4);
return 0;
}
Assigned Mini Projects
1. Student Grading System - Calculate grades based on marks.
2. Basic Calculator - Perform basic arithmetic operations using switch-case.
3. ATM Simulator - Implement a PIN system with balance check and withdrawal.
4. Number Guessing Game - Generate a random number and prompt user guesses.
5. Employee Salary Calculator - Calculate salary based on hours worked.
6. Hotel Booking System - Allow users to book rooms and check availability.

You might also like