Do While Loop in C#: Password Retry System
Table of Contents
- Do While Loop in C#: Password Retry System
- Introduction
- Lesson Objectives
- 1. Understand
- 2. Learn
- 3. Practice
- 4. Apply
- Beginner-Friendly Source Code
- Code Explanation
- Example Output
- Summary
- Multiple Choice Questions (Bloom’s Taxonomy)
- 1. Remembering
- 2. Understanding
- 3. Applying
- 4. Analyzing
- 5. Evaluating
- Exercises, Assessment, and Lab Exam
- Introductory Paragraph
- Exercises
- Assessment Activity
- Lab Exam
Introduction
In many computer systems, user authentication is one of the most important security features. Before accessing an application, users are typically required to enter a password. If the password is incorrect, the system usually allows the user to try again for a limited number of attempts. This behavior can be implemented effectively using looping structures in programming.
In this lesson, we will create a Password Retry System using a do while loop in C#. The program will prompt the user to enter a password and allow multiple attempts until the correct password is entered or the maximum number of attempts has been reached.
The do while loop is particularly useful in this situation because the program must execute the password prompt at least once before checking whether the condition is satisfied. Unlike the while loop, which checks the condition first, the do while loop executes the block of code before evaluating the condition. This makes it ideal for input validation and retry mechanisms such as login systems.
Through this activity, students will learn how to control repeated input attempts, manage conditional logic inside loops, and implement basic authentication behavior in a console application.
By completing this lesson, students will gain practical experience in applying looping structures to simulate simple security systems commonly found in real-world applications.
Lesson Objectives
Outcome-Based Education (OBE) focuses on what learners should be able to demonstrate after completing a lesson. The objectives in this section guide students from understanding the concept of loops to applying them in real-world programming scenarios.
This lesson is designed to help students progressively build their knowledge by first understanding the concept of the do while loop, learning how it works in authentication scenarios, practicing implementation through coding exercises, and applying the concept to improved versions of the program.
1. Understand
- Explain the structure and syntax of the
do whileloop in C#. - Describe how loops control repeated program execution.
- Identify situations where a
do whileloop is more appropriate than other loop types.
2. Learn
- Learn how to store and compare user input with a predefined password.
- Understand how to use a counter variable to track login attempts.
- Recognize how conditional statements work within loops.
3. Practice
- Write a console program that repeatedly asks the user to enter a password.
- Implement logic that limits the number of attempts.
- Test the program with correct and incorrect passwords.
4. Apply
- Apply the password retry logic to a login module in a larger program.
- Extend the system to include username verification.
- Improve the system by adding security measures such as account lockout.
Beginner-Friendly Source Code
Below is a simple C# console program that demonstrates a Password Retry System using a do while loop.
using System;
namespace PasswordRetrySystem
{
class Program
{
static void Main(string[] args)
{
string correctPassword = "admin123";
string userPassword;
int attempts = 0;
int maxAttempts = 3;
Console.WriteLine("=== Password Retry System ===");
do
{
Console.Write("Enter password: ");
userPassword = Console.ReadLine();
attempts++;
if (userPassword == correctPassword)
{
Console.WriteLine("Access Granted.");
break;
}
else
{
Console.WriteLine("Incorrect password.");
Console.WriteLine("Attempts remaining: " + (maxAttempts - attempts));
}
} while (attempts < maxAttempts);
if (userPassword != correctPassword)
{
Console.WriteLine("Maximum attempts reached. Access denied.");
}
}
}
}
Code Explanation
Namespace
PasswordRetrySystem groups the program logically within a project.
Variables
correctPasswordstores the predefined password.userPasswordstores the user input.attemptstracks the number of login attempts.maxAttemptsdefines the maximum number of allowed tries.
Do While Loop
do
{
// ask for password
}
while (attempts < maxAttempts);
The loop ensures the password prompt is executed at least once and continues until the attempt limit is reached.
Break Statement
If the user enters the correct password, the break statement immediately exits the loop.
Example Output
=== Password Retry System ===
Enter password: 123
Incorrect password.
Attempts remaining: 2
Enter password: admin
Incorrect password.
Attempts remaining: 1
Enter password: admin123
Access Granted.
Summary
In this lesson, we developed a simple Password Retry System using a do while loop in C#. The program demonstrates how loops can be used to control repeated user input while limiting the number of attempts. The do while loop ensures that the user is prompted at least once, making it suitable for login systems and input validation tasks. Understanding this concept provides a strong foundation for building authentication systems and other real-world applications that require controlled repetition and conditional logic.
Multiple Choice Questions (Bloom’s Taxonomy)
1. Remembering
Which loop executes its code block at least once?
A. for loop
B. while loop
C. do while loop
D. foreach loop
2. Understanding
Why is the do while loop suitable for a password retry system?
A. Because it runs indefinitely
B. Because it checks condition before execution
C. Because it guarantees the password prompt appears at least once
D. Because it cannot use conditions
3. Applying
Which variable keeps track of the number of attempts?
A. userPassword
B. correctPassword
C. attempts
D. maxAttempts
4. Analyzing
What will happen if the break statement is removed?
A. The loop may continue even if the password is correct
B. The program will not compile
C. The loop will execute once
D. The password will automatically match
5. Evaluating
Which improvement would make the program more secure?
A. Remove attempt limit
B. Hide password input characters
C. Remove password comparison
D. Use only one attempt
Exercises, Assessment, and Lab Exam
Introductory Paragraph
To strengthen programming skills, students should extend the Password Retry System by modifying and enhancing its functionality. These exercises encourage learners to experiment with loop logic, conditional structures, and user interaction while improving program security and usability.
Exercises
- Modify the program to allow 5 password attempts instead of 3.
- Display a message when the user successfully logs in after multiple attempts.
- Add a username validation before the password check.
- Implement a delay after each failed attempt.
- Hide password characters using secure input techniques.
Assessment Activity
Create a Login System Program that includes:
- Username input
- Password retry mechanism
- Maximum of 3 attempts
- Welcome message upon successful login
Lab Exam
Task: Develop a Secure Login Simulation
Requirements:
- Validate username and password
- Allow 3 login attempts
- Display remaining attempts
- Lock the account after exceeding attempts
- Use proper loop structure and readable code
Evaluation Criteria:
- Loop implementation (30%)
- Program logic accuracy (30%)
- Code readability and structure (20%)
- Program functionality (20%)
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.