Lecture 9
Menu Driven Programs using switch +
while/do-while & Introduction to for-each
Loop
Duration: 1.5 – 2 Hours
Learning Objectives
After this lecture, students will be able to:
● Create menu-driven programs using switch
● Use while loop with switch
● Use do-while loop with switch
● Understand where for-each loop is used
● Know the difference between for and for-each
Part 1
Menu Driven Programs
Introduction
Suppose we create a Calculator.
1. Addition
2. Subtraction
3. Exit
After selecting one option, the program closes.
But in real-world applications like:
● ATM
● Student Management System
● Employee Management
● Food Ordering
● Banking System
the menu is displayed repeatedly until the user chooses Exit.
For this, we combine:
switch
+
while / do-while
Program 1
Student Management Menu
import [Link];
public class StudentMenu {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int choice = 0;
while(choice != 4) {
[Link]("\n===== Student Menu =====");
[Link]("1. Add Student");
[Link]("2. Update Student");
[Link]("3. Delete Student");
[Link]("4. Exit");
[Link]("Enter Choice : ");
choice = [Link]();
switch(choice) {
case 1:
[Link]("Student Added Successfully");
break;
case 2:
[Link]("Student Updated Successfully");
break;
case 3:
[Link]("Student Deleted Successfully");
break;
case 4:
[Link]("Thank You...");
break;
default:
[Link]("Invalid Choice");
}
}
[Link]();
}
}
Explanation
while(choice != 4)
means
Continue displaying the menu until the user enters 4 (Exit).
Program 2
Calculator Menu
import [Link];
public class CalculatorMenu {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int choice = 0;
while(choice != 5) {
[Link]("\n===== Calculator =====");
[Link]("1. Addition");
[Link]("2. Subtraction");
[Link]("3. Multiplication");
[Link]("4. Division");
[Link]("5. Exit");
[Link]("Enter Choice : ");
choice = [Link]();
switch(choice) {
case 1:
[Link]("Enter First Number : ");
int a = [Link]();
[Link]("Enter Second Number : ");
int b = [Link]();
[Link]("Answer = " + (a+b));
break;
case 2:
[Link]("Enter First Number : ");
a = [Link]();
[Link]("Enter Second Number : ");
b = [Link]();
[Link]("Answer = " + (a-b));
break;
case 5:
[Link]("Calculator Closed");
break;
default:
[Link]("Invalid Choice");
}
}
}
Program 3
Employee Menu using do-while
import [Link];
public class EmployeeMenu {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int choice;
do {
[Link]("\n===== Employee Menu =====");
[Link]("1. Add Employee");
[Link]("2. Search Employee");
[Link]("3. Delete Employee");
[Link]("4. Exit");
[Link]("Enter Choice : ");
choice = [Link]();
switch(choice) {
case 1:
[Link]("Employee Added");
break;
case 2:
[Link]("Employee Found");
break;
case 3:
[Link]("Employee Deleted");
break;
case 4:
[Link]("Thank You");
break;
default:
[Link]("Invalid Choice");
}
} while(choice != 4);
while vs do-while
while do-while
Condition checked first Condition checked after execution
May execute zero times Executes at least one time
Good for known conditions Best for menu-driven programs
Part 2
Introduction to for-each Loop
What is a for-each Loop?
A for-each loop is a special type of loop used to traverse all elements of an array or
collection.
It automatically visits each element one by one.
Syntax
for(dataType variable : arrayName)
{
// code
}
Example
int[] numbers = {10,20,30,40,50};
for(int num : numbers)
{
[Link](num);
}
Output
10
20
30
40
50
How does it work?
numbers
10
↓
20
↓
30
↓
40
↓
50
One value is stored in:
num
during every iteration.
Equivalent for Loop
int[] numbers = {10,20,30,40,50};
for(int i=0;i<[Link];i++)
{
[Link](numbers[i]);
}
Same Program Using for-each
for(int num : numbers)
{
[Link](num);
}
Much shorter.
Advantages
● Simple syntax
● Easy to read
● Less code
● No index management
● Best for printing arrays
Limitations
Cannot:
● Access index
● Traverse in reverse order
● Skip elements easily
● Modify array elements directly
Difference Between for and for-each
for Loop for-each Loop
Uses index No index
Can move forward/backward Forward only
Can modify elements using index Used mainly for reading
Works with all loop scenarios Works with arrays and collections
Practice Programs
Program 1
Menu Driven Student System
1 Add Student
2 Update Student
3 Delete Student
4 Exit
Program 2
ATM Menu
1 Deposit
2 Withdraw
3 Check Balance
4 Exit
Program 3
Print
int[] marks={70,80,90,60,75};
using for-each.
Program 4
Print
String[] names={"Rahul","Amit","Neha","Priya"};
using for-each.