C# While Loop ATM Withdrawal Simulation
Table of Contents
- C# While Loop ATM Withdrawal Simulation
- Introduction
- Learning Objectives (OBE-Based)
- By the end of this lesson, students will be able to:
- 1. Understand
- 2. Learn
- 3. Practice
- 4. Apply
- C# While Loop ATM Withdrawal Simulation – Source Code
- How the Program Works
- Example Output
- Lesson Summary
- Multiple Choice Questions (Bloom’s Taxonomy)
- Exercises and Practice Activities
- Exercises
- Assessment Activity
- Lab Exam Task
Introduction
Repetition control structures allow programs to execute a block of code multiple times based on a condition. One of the most commonly used loops in C# is the while loop. A while loop is considered a pre-test loop because it evaluates the condition before executing the code inside it. If the condition is true, the loop runs. If the condition is false, the loop stops.
In this lesson, we will create a C# While Loop ATM Withdrawal Simulation. The program will repeatedly allow a user to withdraw money as long as there is enough balance in the account. If the withdrawal amount exceeds the available balance, the transaction will be cancelled. This real-world example demonstrates how repetition and decision-making structures work together in banking systems and financial applications.
By the end of this lesson, students will understand how loops control program flow and how they are used in practical systems.
Learning Objectives (OBE-Based)
This lesson follows Outcome-Based Education (OBE) principles. The objectives focus on measurable competencies under four categories: Understand, Learn, Practice, and Apply.
By the end of this lesson, students will be able to:
1. Understand
- Define what a while loop is in C#.
- Explain how a pre-test loop works.
- Identify real-world scenarios where repetition is required.
- Describe how loop conditions control execution.
2. Learn
- Write the correct syntax of a while loop.
- Declare and initialize loop control variables.
- Apply relational operators inside loop conditions.
- Combine conditional statements with loops.
3. Practice
- Modify withdrawal limits.
- Test different account balances.
- Add transaction limits.
- Debug logical errors inside loops.
4. Apply
- Create a working ATM withdrawal simulation.
- Implement transaction validation logic.
- Prevent invalid withdrawals.
- Design simple financial systems using repetition structures.
C# While Loop ATM Withdrawal Simulation – Source Code
You’re correct. For proper C# structure—especially when teaching beginners—it is best practice to include a namespace. Below is the corrected, WordPress-ready version of the source code with a proper namespace included.
You can directly copy and paste this into WordPress (Code block).
using System;
namespace ATMWithdrawalSimulation
{
class Program
{
static void Main(string[] args)
{
double balance = 5000; // Initial account balance
double withdrawal;
Console.WriteLine("Welcome to the ATM Machine!");
Console.WriteLine("Your current balance is: " + balance);
while (balance > 0)
{
Console.Write("Enter amount to withdraw: ");
withdrawal = Convert.ToDouble(Console.ReadLine());
if (withdrawal <= balance && withdrawal > 0)
{
balance -= withdrawal;
Console.WriteLine("Withdrawal successful!");
Console.WriteLine("Remaining balance: " + balance);
}
else
{
Console.WriteLine("Invalid or insufficient balance. Transaction cancelled.");
break;
}
if (balance == 0)
{
Console.WriteLine("Your account balance is now zero.");
break;
}
}
Console.WriteLine("Thank you for using the ATM.");
}
}
}
How the Program Works
- The program starts with an initial balance of 5000.
- The while loop runs as long as the balance is greater than zero.
- The user enters the withdrawal amount.
- If the withdrawal amount is valid and does not exceed the balance, it deducts the amount.
- If the withdrawal exceeds the balance, the loop stops.
- The program ends when the balance reaches zero or when an invalid transaction occurs.
This demonstrates:
- Loop condition control
- Variable updating inside loops
- Combining relational and logical operators
- Using break to exit a loop
Example Output
Welcome to the ATM Machine!
Your current balance is: 5000
Enter amount to withdraw: 1000
Withdrawal successful!
Remaining balance: 4000
Enter amount to withdraw: 3000
Withdrawal successful!
Remaining balance: 1000
Enter amount to withdraw: 1500
Invalid or insufficient balance. Transaction cancelled.
Thank you for using the ATM.
Lesson Summary
In this lesson, students explored how the C# while loop works as a pre-test repetition structure. Through the ATM Withdrawal Simulation, learners applied conditional statements, relational operators, and logical operators to control program execution. The activity demonstrated how repetition structures are used in real-world financial systems. By combining loops and decision-making logic, students gained practical skills in designing systems that continue running until specific conditions change.
Multiple Choice Questions (Bloom’s Taxonomy)
- What type of loop checks its condition before executing its block?
A. for loop
B. do-while loop
C. while loop
D. foreach loop - Why does the loop stop when balance becomes zero?
A. The program crashes
B. The condition balance > 0 becomes false
C. There is a syntax error
D. It becomes an infinite loop - If balance is 2000 and the user withdraws 2500, what happens?
A. Withdrawal succeeds
B. Balance becomes negative
C. Transaction is cancelled
D. Loop repeats forever - Which operator ensures the withdrawal is both valid and within balance?
A. ||
B. =
C. &&
D. != - Which scenario best demonstrates a while loop?
A. Printing numbers from 1 to 5
B. Processing withdrawals until funds are insufficient
C. Declaring a variable
D. Displaying one message
Exercises and Practice Activities
To strengthen understanding of the C# While Loop ATM Withdrawal Simulation, students must modify and extend the program. Practice activities help improve logical thinking and debugging skills.
Exercises
- Add a deposit option.
- Limit withdrawal attempts to 3 tries.
- Display total withdrawn amount.
- Prevent negative withdrawal inputs.
- Add a transaction counter.
Assessment Activity
Create a menu-driven ATM system with the following options:
- Withdraw
- Deposit
- Check Balance
- Exit
Use a while loop to keep the program running until the user selects Exit.
Lab Exam Task
Develop an enhanced ATM simulation that:
- Starts with a default balance.
- Allows withdrawals and deposits.
- Tracks total number of transactions.
- Prevents invalid inputs.
- Ends only when the user selects Exit.
Evaluation Criteria:
- Correct loop implementation (30%)
- Proper logical conditions (25%)
- Input validation (20%)
- Code clarity (15%)
- Output accuracy (10%)
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.