Login Simulation (Username and Password Check)
Table of Contents
- Login Simulation (Username and Password Check)
- Introduction
- Objectives
- Beginner-Friendly Source Code and Step-by-Step Instructions
- Step-by-Step Instructions
- C# Program: Login Simulation
- How to Run the Program
- Example Output
- Explanation of the Program
- Summary
- Quiz (5 Items – Bloom’s Taxonomy)
- Exercises, Assessment, and Lab Exam
- Introduction to Exercises
- Exercises
- Assessment
- Lab Exam
Introduction
In modern software development, user authentication is one of the most essential features. Whether you’re logging into your email, social media, or online banking, programs often require you to enter a username and password to verify your identity. In this lesson, you’ll learn how to simulate a simple login program in C# using if and if-else statements.
This simulation will help you understand how conditions work to check user inputs and how logical operators like == and && are used to validate multiple conditions at once. By the end of this lesson, you’ll be able to build a basic login system that accepts user credentials and verifies if they match predefined values.
Objectives
Before coding, let’s define what you will achieve in this lesson. The objectives follow the Outcome-Based Education (OBE) format, focusing on four essential learning outcomes — understand, learn, practice, and apply.
- Understand: Explain how conditional statements (if and if-else) are used to check multiple conditions in a program.
- Learn: Identify the proper syntax and logic for comparing user input with stored credentials using
==and&&. - Practice: Write and test a C# console program that performs a simple username and password check.
- Apply: Modify and enhance the login program by adding validation messages or multiple login attempts.
These objectives aim to build both your conceptual and practical understanding of decision control structures in real-world programming.
Beginner-Friendly Source Code and Step-by-Step Instructions
Step-by-Step Instructions
- Open Visual Studio Code
Make sure you have the .NET SDK installed.
To confirm, open the terminal and type:dotnet --version - Create a new project folder
Example:D:\2025-1_doe_bsis_1a - Open the folder in VS Code
Go to File → Open Folder… - Create a console project
In the VS Code terminal, type:dotnet new console -n login_simulation - Navigate into your project folder
cd login_simulation - Open Program.cs and replace the code with the sample below.
C# Program: Login Simulation
using System;
namespace LoginSimulationApp
{
class Program
{
static void Main(string[] args)
{
string correctUsername = "admin";
string correctPassword = "12345";
Console.WriteLine("=== Simple Login Simulation ===");
Console.Write("Enter username: ");
string username = Console.ReadLine();
Console.Write("Enter password: ");
string password = Console.ReadLine();
if (username == correctUsername && password == correctPassword)
{
Console.WriteLine("\nLogin successful! Welcome, " + username + "!");
}
else if (username == correctUsername && password != correctPassword)
{
Console.WriteLine("\nIncorrect password. Please try again.");
}
else if (username != correctUsername && password == correctPassword)
{
Console.WriteLine("\nIncorrect username. Please check your spelling.");
}
else
{
Console.WriteLine("\nLogin failed! Invalid username and password.");
}
Console.WriteLine("\nProgram ended.");
}
}
}
How to Run the Program
- In the terminal, type:
dotnet run
Example Output
Example 1: Successful Login
=== Simple Login Simulation ===
Enter username: admin
Enter password: 12345
Login successful! Welcome, admin!
Example 2: Incorrect Password
Enter username: admin
Enter password: 54321
Incorrect password. Please try again.
Example 3: Both Incorrect
Enter username: user
Enter password: test
Login failed! Invalid username and password.
Explanation of the Program
This program demonstrates how to use if-else conditional statements to compare input values and make logical decisions.
- The correct username and password are stored in variables
correctUsernameandcorrectPassword. - The program prompts the user to input their username and password using
Console.ReadLine(). - The if statement checks if both values match the correct credentials using the logical AND operator (
&&). - If both are correct, a success message is shown.
- If one or both values are incorrect, specific error messages are displayed depending on the situation.
- The final
elsestatement handles all other invalid cases.
This simple simulation introduces the concept of authentication logic — one of the foundations of login systems used in real applications.
Summary
In this lesson, you learned how to build a login simulation in C# using if and if-else statements. The exercise demonstrated how to compare user input with stored credentials and handle multiple conditions logically. This skill is essential for developing secure and interactive applications. Although this version is a simple simulation, it lays the groundwork for future lessons where you’ll use databases, hashing, and more secure authentication techniques.
Quiz (5 Items – Bloom’s Taxonomy)
- What C# statement is used to check multiple conditions like username and password?
A. for
B. if-else
C. switch
D. loop - Which logical operator is used to ensure both username and password are correct?
A. ||
B. !=
C. &&
D. == - What happens if the entered username is correct but the password is incorrect?
A. Login successful
B. Incorrect password
C. Invalid input
D. Program ends abruptly - What function is used to read user input in C#?
A. Console.Write()
B. Console.ReadLine()
C. Console.Read()
D. Console.Input() - What is displayed if both username and password are incorrect?
A. Login successful
B. Incorrect password
C. Invalid username and password
D. Try again
Exercises, Assessment, and Lab Exam
Introduction to Exercises
The following exercises will reinforce your understanding of conditional logic and help you apply it to real-world login scenarios. You will practice handling multiple conditions and improving user experience with better messages or validation.
Exercises
- Modify the program to allow three login attempts before locking out the user.
- Add an option to display “Forgot Password” if login fails.
- Create a version that uses case-insensitive username comparison (
ToLower()). - Store multiple usernames (e.g., “admin”, “user1”, “teacher”) and check them using
ifconditions. - Add a welcome message that varies based on the username (e.g., “Welcome, Teacher!”).
Assessment
- Quiz: Answer the 5-item multiple-choice quiz above.
- Performance Task: Create a modified login program that accepts three users, each with a unique password, and displays a custom message after login.
Lab Exam
Write a C# console program that asks for a username and password.
- If both are correct, display “Access Granted.”
- If incorrect, display “Access Denied.”
- Use if-else statements and handle incorrect credentials properly.
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.