Java based "If" conditional branch statements and switch switch statements

Source: Internet
Author: User

This article is still written for beginners, and the following is for all the branch structures and switch branches of if, and hopefully it will help beginners.

An If statement includes a single condition, a single branch, a single condition, a double branch, and a multiple-condition multiple branch.

1. Single condition, single branch: parentheses () The value inside the bracket () must be a Boolean, followed by the execution of the statement in the following curly braces.

public static void Main (String args[]) {
		int a = ten;
		int b =;
		int t;
		if (a < b) {
			t = A;
			A = b;
			b = t;
			System.out.println ("a=" + A + ", b=" + b);

		}
}
To determine that the value in the parentheses is true, and then follow the logic in the following curly braces, a first assigns a value to the T,t=10,b assignment to the a,a=20,t assignment to b,b=10, so that A and B completes the data exchange.

Results:


2. Single condition two branches: first judge the value inside the parentheses, the value is true immediately followed the execution of the statement, otherwise execute the statement inside else.

public class Lianxi01 {public

	static void Main (String args[]) {

		Boolean b = false;
		if (b) {//true
			System.out.println ("The Spanish shark you sent to me");
		} else {

			System.out.println ("The Spanish Whale you gave me");
		}

		if (!b) {//false
			System.out.println ("The Spanish shark you sent to me");
		} else {

			System.out.println ("Sister of Hong Kong Island, You sent me to the Spanish Whale ");}}


This is the two If-else statement, in this example the parentheses first judge the b=true, the output else inside the statement, the second If-else statement, judgment. B=true was established, followed by the execution of the curly brace statement, and the results of the operation were as follows:


3. Multi-Conditional multiple branches: First calculate the value of the first parenthesis, the result is true, the output of the following compound statement and then exit the branch statement, otherwise. Continue to judge the value of the next parenthesis, and if executed to the end, there is no value in the parentheses that conforms to true, and the statement that is followed by the output else.

public class Lianxi01 {public

	static void Main (String args[]) {

		Scanner input = new Scanner (system.in);//Keyboard input 
  system.out.println ("Please enter:");
		int a = Input.nextint ()//Enter Value
		if (a > && a <) {
			System.out.println ("Sister of Hong Kong, you gave me the Spanish Shark");
		} else if (a > && a <) {
			System.out.println ("Sister of the island, you sent me the Spanish Whale");
		} else if (a <) {
			Sys Tem.out.println ("Sister of Hong Kong Island, you gave me the Chinese sturgeon of Spain");
		} else {
			System.out.println ("input Error");}}

Output results:


Switch statement: Single conditional multiple branch (break out of the current switch statement). The switch immediately follows the parentheses inside can be short, int,char,byte type of value, But it cannot be a long value. The switch statement evaluates the value of the parentheses first, and if the value of the parentheses is equal to the constant after the case, execute the statement in the case until it encounters a break to exit the switch structure. It is also possible that all the case does not conform to the value computed in the parentheses, and then executes the statement in default.

Instance:

public class Lianxi01 {public

	static void Main (String args[]) {
		Scanner input = new Scanner (system.in);
		SYSTEM.OUT.PRINTLN ("Input 12345 First:");
		int number = Input.nextint ();
		Switch (number) {case
		1:
			System.out.println ("I am contestant 1th");
		Case 2:
			System.out.println ("I am a contestant number 2nd");
		Case 3:
			System.out.println ("I am a contestant number 3rd");
		Case 4:
			System.out.println ("I am a contestant number 4th");
			break;
		Case 5:
			System.out.println ("I am a contestant number 5th");
			break;
		Default:
			System.out.println (Invalid input);}}

Run Result:


The differences between the IF and switch branch statements:

Different

The most important feature of an IF branch statement is that you can judge a range, or you can judge an expression,

1.switch Branch Statement It's not clear how to judge a range, but for

The exact value is much better than the IF statement.

A 2.if statement chooses to perform a branch operation based on a condition, and the switch statement chooses to perform a branch (case) operation based on a condition or to perform multiple branch operations (multiple case).












Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.