Java selection control syntax: if, switch

Java uses if and switch syntax for select control. The basic use of if-else and if-else-if syntax is shown below.

if-else

if(condition) {
	sentence 1;
} else {
	sentence 2;
}

if-else-if

if(condition) {
	sentence 1;
} else if(condition) {
	sentence 2;
} else if(condition) {
	sentence 3;
} else {
	sentence n;
}

if Basic example

In addition to Java, most programming languages support if, if-else, and switch syntax. The following example is a program that accepts data from a user, converts the data to an integer, compares it with data through an if condition, and outputs the message.

class IfElse {
	public static void main(String args[]) {
		int month = Integer.parseInt(args[0]);
		//In the execution command, data is input from the user, converted to an integer, and stored in month
		String MtoS;

		if(month == 12 || month == 1 || month == 2) {
			MtoS = "winter";
		} else if(month == 3 || month == 4 || month == 5) {
			MtoS = "spring";
		} else if(month == 6 || month == 7 || month == 8) {
			MtoS = "summer";
		} else if(month == 9 || month == 10 || month == 11) {
			MtoS = "autumn";
		} else {
			MtoS = "There is no corresponding season.";
		}

		System.out.println( month + "month is " + MtoS);

	}
}

Basic usage and examples of switch

switch(Equation) {
	case value1:
		sentence;
		break;
	case value2:
		sentence;
		break;
	case value3:
		sentence;
		break;
	default:
		Sentence to be treated implicitly;
}

Below is a simple example of outputting rock-paper-scissors using a switch.

class Switch {
	public static void main(String args[]) {
		for(int i=1; i<5; i++) {
			switch(i) {
				case 1:
					System.out.println( i + " : Rock" );
					break;
				case 2:
					System.out.println( i + " : Paper" );
					break;
				case 3:
					System.out.println( i + " : Scissors" );
					break;
				default:
					System.out.println( i + "is not a value between 1 and 3" );
			}
		}
	}
}

Share This Post

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Post

  • 8 things you need to think about for work-life balance
    View on Youtube Image: juliezhuo.com who works as a design VP for Facebook, presented 8 ways for work and life balance in her article “The
  • 10 special abilities of Smartphone
    View on Youtube No matter where you go or what you do, your smartphone is always with us. We use the internet with a smartphone,
  • Java: abstract class, abstract method
    An abstract class in Java is a class that declares an abstract function to be implemented in a subclass as an abstract method. An abstract
  • Java: inheritance and constructors
    When an object is created from an inherited class, the constructor of the superclass is performed before the constructor of the subclass. That is, when
  • Java reserved words: super, final
    super is a Java reserved word that is used to access member variables or methods of a superclass that is obscured by a subclass. Access
  • Java: finalize method and garbage collection
    The Java Virtual Machine(JVM) automatically performs garbage collection for objects that are no longer needed for efficient system operation. At this point, the JVM calls
  • Java: Method overloading
    Method overloading is the same concept as constructor overloading. That is, you can use methods of the same name in the same class, but methods
  • Java final variable
    You can specify an end variable using the reserved word final. The final variable represents a constant value that can not be changed. Typically, final
  • Java class variables
    Class variables are declared with static and have the concept of global variables. Class variables are used for the following purposes. Object references and object
  • Member variables in Java
    Java’s member variables are all declared outside the methods in the class and represent the properties that the object can have. Member variables consist of