Exit The Program
Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 85 of 93.
Challenge
MediumIn the previous lesson, you created the menu display for the expense tracker. Now, add a loop that continuously shows the menu and reads the user's choice until they select option 5 to exit.
Wrap your menu in a loop that:
- Displays the menu
- Reads the user's choice as input
- If the choice is
"5", printGoodbye!and exit the loop - Otherwise, print
Invalid optionand show the menu again
Input format:
Each line contains a menu choice (a string). The inputs will continue until "5" is entered.
Output format:
For each iteration, print the menu, then either Invalid option (for choices other than "5") or Goodbye! (when "5" is entered).
Example:
If the inputs are 7, 3, and 5, the output should be:
--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
Invalid option
--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
Invalid option
--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
Goodbye!Note: For now, only option 5 has actual functionality. All other options (including valid ones like 1-4) should print Invalid option. You'll implement the other options in upcoming lessons.
Try it yourself
fun main() {
// TODO: Write your code below to print the expense tracker menu
println("--- Expense Tracker ---")
println("1. Add Expense")
println("2. View All Expenses")
println("3. Total and Average")
println("4. Clear All")
println("5. Exit")
println("Choose an option:")
}All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorAugmented AssignmentComparison OperatorsRecap - Simple Math7Basic IO
Println FunctionString TemplatesReadLine InputType ConversionRecap - Years Until RetirementRecap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesNamed ArgumentsDefault ValuesSingle Expression FunctionsRecap - Sigma FunctionRecap - Validation Function2Variables
Val vs VarType InferenceNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Logical Operators Part 3Ternary With If ExpressionRecap - Simple Logic8Bill Split Calculator
Welcome MessageGetting Input3Nullability
What Is Null SafetyNullable TypesSafe Call OperatorElvis OperatorFunction Challenge BasicsNot Null AssertionRecap - Safe Access6Decision Making
If StatementIf - ElseIf As An ExpressionWhen ExpressionWhen With RangesRecap - Simple Calculator9Loops
For LoopWhile LoopDo-While LoopBreakContinueRanges In LoopsNested LoopRecap - FactorialRecap - Dynamic Input12Lists Basics
List vs MutableListAccessing ElementsModifying ListsList MethodsPair And TripleRecap - Product ListRecap - Reversed List15Daily Expense Tracker
Project OverviewExit The ProgramPractice on your own: Kotlin playground