Sunday, April 27, 2025

Java Tutorial - Switch Statement

 A switch statement is a control flow statement in programming that allows a program to execute different actions based on the value of a variable or an expression.

public class Main {

	public static void main(String[] args) {
		int choice = 4;
		
		switch (choice) {
		case 1:
			System.out.println("Audio Settings");
			break;
		case 2:
			System.out.println("Video Settings");
			break;
		case 3:
			System.out.println("Controls");
			break;
		default:
			System.out.println("Game Help");				
		}							
	}	
}

The code defines a Main class with a main method that contains a switch statement. The variable choice is initialized to 4. The switch statement checks the value of choice against each of the case statements. In this example, there are three case statements, one for choice equal to 1, one for choice equal to 2, and one for choice equal to 3.

If choice matches one of the case statements, the corresponding message is printed to the console. If choice does not match any of the case statements, the default case is executed and the "Game Help" message is printed to the console.

Note that each case statement ends with a break statement, which is necessary to prevent the execution of subsequent case statements.

The default option in a switch statement is useful because it provides a fallback option when none of the cases match the input value. If there is no default option, the switch statement will simply do nothing if the input value does not match any of the cases.

Having a default option can provide a useful message to the user or perform some default action that needs to be taken when the input value does not match any of the cases. This can help prevent errors and provide a better user experience.

Switch with User Input

This code is a simple Java program that takes an input from the user and prints a message based on the choice:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner someObj = new Scanner(System.in);
		
		System.out.println("--------------------------");
		System.out.println("Menu: ");
		
		int choice = someObj.nextInt();
		System.out.println("Choice: " + choice);		
		
		switch (choice) {
		case 1:
			System.out.println("Audio Settings");
			break;
		case 2:
			System.out.println("Video Settings");
			break;
		case 3:
			System.out.println("Controls");
			break;
		default:
			System.out.println("Game Help");		
		
		}		
	}	
}

Explanations: 

int choice = someObj.nextInt();
System.out.println("Choice: " + choice);

These two lines read an integer input from the user using the Scanner object and store it in a variable named choice. It then prints the value of the choice to the console. 

switch (choice) {
case 1:
    System.out.println("Audio Settings");
    break;
case 2:
    System.out.println("Video Settings");
    break;
case 3:
    System.out.println("Controls");
    break;
default:
    System.out.println("Game Help");        
}

This is a switch statement that takes the value of the choice and executes the corresponding case.

If the choice is 1, it prints "Audio Settings". If the choice is 2, it prints "Video Settings".

If the choice is 3, it prints "Controls". If the choice is not 1, 2, or 3, it prints "Game Help".

Our code prompts the user to enter a number from a menu, reads the input, and prints a corresponding message based on the input using a switch statement.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...