KwickAcademy Java · 8 min · free
Menu-driven Programs with switch and System.exit
Build a menu-driven Java program: print the choices, read one with a Scanner, run it with switch, repeat with do-while, and stop with System.exit(0). A menu is a numbered list of choices, and only the task the user picks runs.
Follows the syllabus of: ICSE Class 9 Computer Applications, Programming Java
On screen in this lesson
What is a menu driven program?
| Shows a numbered list of choices, called a menu |
| The user types the number of one choice |
| The program runs only the task for that choice |
| Many tasks live inside one program |
Everyday menus
| Place | Choice | Task |
|---|---|---|
| ATM | 1 | Withdraw cash |
| ATM | 2 | Check balance |
| Recharge app | 3 | Data pack |
| Canteen kiosk | 4 | Order samosa |
Parts of a menu program
| Print the menu with System.out.println |
| Read the choice with a Scanner object |
| Use switch to run the matching task |
| Use default for a wrong choice |
Rules for switch
| Works with int, char and String values |
| Case labels must be constants, no repeats |
| break stops fall through |
| default is optional; it runs if nothing matches |
Which loop for a menu?
| Loop | Menu shown | Use |
|---|---|---|
| do while | at least once | best choice |
| while (true) | forever | needs exit |
| for | fixed times | not suitable |
The number inside exit()
| exit(0): normal end, everything went well |
| Non-zero, like exit(1): ended because of a problem |
| Nothing after exit runs, not even the next line |
| break leaves only the switch; exit ends the program |
Quick answers
What happens if you forget break in a menu switch?
Java falls through and runs the next case too.
What is the difference between break and System.exit(0)?
break leaves only the switch or loop. System.exit(0) ends the whole program.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
What is a menu in a program?43 sec
Do ten menu choices need ten if statements?44 sec
Which loop suits a menu best?42 sec
Why did break not end my program?42 secThe full lesson, in text
Hello students, welcome to Kwickprep. At an ATM, the screen shows a list of choices, and you press one number. How does a program offer choices like that? Today we will build a menu driven program with switch, repeat the menu with a loop, and stop the program with System dot exit.
Let us start with the meaning. A menu is a numbered list of choices printed on the screen. The user types the number of the choice they want. The program then runs only the task linked to that number. So one program can hold many tasks, like area, perimeter and exit, and the user picks one. That is a menu driven program.
You already use menus every day. At an ATM, pressing one lets you withdraw cash. Pressing two shows your balance. In a recharge app, choice three may open data packs. At a canteen kiosk, choice four may order a samosa. In each case, one number decides which task runs.
Every menu driven program has four parts. First, print the menu lines with System dot out dot println. Second, read the user's choice with a Scanner object, which reads what is typed on the keyboard. Third, use a switch statement to run the task for that choice. Fourth, add a default case to handle a wrong number.
Here is the first step. Three print lines show the menu: one for area, two for perimeter, three for exit. Then we create a Scanner called sc that reads from System dot in, the keyboard. The method nextInt reads a whole number, and we store it in ch, short for choice. Remember to write import java dot util dot Scanner at the top of the program.
Now the switch statement. Switch compares the value of ch with each case label. If ch is one, it finds the area of a square, side times side. If ch is two, it finds the perimeter, four times side. The word break ends the switch, so the next case does not run. Case three will end the program, which we see soon. Default runs when no case matches, so it prints Wrong.
Pause and predict. The choice ch is one, and there is no break. What prints? Both A and B print. Without break, Java falls through into the next case and runs it too. This is called fall through. In a menu, always put break at the end of each case.
Remember these rules for switch. The value can be an int, a char or a String, but not a double or a boolean. Each case label must be a fixed constant, and two cases cannot share the same value. Break stops the fall through into the next case. Default is optional, and it runs when no case matches.
A real menu should not end after one task. The user should be able to pick again. So we show the menu and read the choice. Then we ask, is ch equal to three? If yes, the program ends with System dot exit. If no, it runs the task for that choice. Then the flow goes back and shows the menu again. The program stops only through the exit choice.
The best loop for a menu is do while. A do while loop runs its body first, and checks the condition at the end. So the menu is always shown at least once. After each task, the condition asks, is ch not equal to three? Exclamation equals means not equal to. While the answer is true, the menu comes back. When the user types three, the loop ends and Thank you prints.
Which loop should you pick? A do while shows the menu at least once and stops on the exit choice, so it suits menus best. A while true loop runs forever, so it needs System dot exit or break to stop. A for loop runs a fixed number of times, but we do not know how many choices the user will make.
Now, System dot exit. It stops the whole program at once, even from inside a switch or a loop. Here, Bye prints first. Then System dot exit of zero ends the program. So the last line, Never, is never printed. Only Bye appears in the output.
The number inside the brackets is called the status code. Zero means a normal end, where everything went well. A non zero number, like one, means the program ended because of a problem. Once exit runs, no line after it runs. Do not confuse it with break. Break only leaves the switch or loop, but System dot exit ends the entire program.
Here is the whole idea together, kept short. Assume the variable side is five, and sc is our Scanner. Inside do while, we print the menu and read ch. If the user types one, it prints side times side, which is twenty five. A perimeter case would sit here in the same way. If the user types three, System dot exit ends the program. Because the condition is while true, only the exit choice can stop this menu.
Let us trace a sample run of the full program, with side five. The user types one, and the screen shows area twenty five, then the menu again. Next the user types two, and it shows perimeter twenty. Then the user types seven by mistake, so default prints Wrong choice. Finally the user types three, and the program ends. Board exams often ask you to write exactly this kind of program.
Let us revise what we learned today. A menu driven program shows numbered choices and runs only the task picked. Switch runs the case that matches the choice. Break stops fall through, and default handles a wrong number. A do while loop repeats the menu until the user picks exit. And System dot exit of zero ends the whole program normally. Try writing a menu for a calculator on your own.
Courses that teach this
| Course | Unit |
|---|---|
| ICSE Class 9 Computer Applications | Conditional Constructs in Java |
| Programming All levels Java | Control Flow and Iteration |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

