Java If Else statement Getting Started Guide (recommended) _java

Source: Internet
Author: User

Conditional statement, which is a kind of statement that chooses to execute according to the condition in the program, the difficulty lies in how to abstract the condition accurately in the actual use of such statement. For example, to implement the program login function, if the user name and password is correct, then enter the system, otherwise pop-up "password error" such as the Prompt box.

This section is an introduction to conditional statements, with emphasis on syntax explanations and basic usage, and more detailed use of the following comprehensive Example section.

In the Java language, conditional statements have two main types of syntax: If statements and switch statements.

1, if statement

If keyword Chinese meaning is if, its meticulous grammatical induction total altogether three kinds: if statement, If-else statement and If-else If-else statement, below introduce separately.

1.1 If statement

The syntax format of the class statement is: if (conditional expression)

Functional code Syntax Description: If is the keyword in the statement, followed by a pair of parentheses, the parentheses can not be omitted at any time, the inside of the parentheses is a specific condition, the syntax requires that the expression result is a Boolean type. Following the function of the code, that is, when the condition is set up to execute the code, in the program writing, generally for intuitive expression including relations, functional code generally need to indent.

Special attention needs to be paid to:

1, the function code here can only be one line, about the function code of the multiline structure, the follow-up will explain.

2, if (conditional expression) Subsequent general does not write the semicolon if statement of the execution process is: if the conditional expression is set up, then execute the functional code, if the conditional expression is not established, then do not perform the following functional code.

Sample code:

int a = 10;

if (a >= 0)

System.out.println ("A is a positive number");

if (a% 2 = 0)

System.out.println ("A is even");

In the example code, the first condition is to determine whether the value of variable A is greater than or equal to zero, and if the condition is set to execute the output, the second condition is to determine whether the variable a is an even number, and if so, output.

Note the following code execution process:

int m = 20;

if (M > 20)

m + + 20;

System.out.println (m);

According to the previous syntax format, only m+=20; this line of code belongs to the functional code, and subsequent output statements and the preceding conditions form the sequential structure, So the output of the program after execution is 20. If the statement to be executed has multiple sentences when the condition is set up, the statement block can be used to describe the syntax format as follows:

if (conditional expression) {

Functional code block;

}

Using this syntax format, a block of code is used instead of the preceding functional code, which allows you to write arbitrary multiple lines of code within a block of code, and makes the logic of the entire program clearer, so it is recommended in actual code writing.

1.2 If-else statement

The If-else statement implements a closed condition that is more common in the program. The Else keyword has the function of "otherwise", that is, the condition is not tenable.

The syntax format for the IF-ELSE statement is as follows:

if (conditional expression)

function code 1;

Else

function code 2;

Syntax Description: The previous section and the if statement, the else part of the code behind the function, in accordance with the syntax format, the functional code can only have one sentence.

Order of execution: If the condition is true, execute the function code 1 in the IF statement, or perform the function code 2 in else. The sample code is:

int n = 12;

if (n% 2!) = 0)

System.out.println ("n is odd");

else System.out.println ("N is not odd");

Because the value of the n%2 is 0 and the condition is not valid, the code for the Else statement is executed, and the program output "N is not an odd number."

In the actual use, in order to structure clearly, as well as in the functional code part of the writing of multiple lines of code, the functional Code section of the general use of code blocks, then the syntax format is:

if (conditional expression) {

Functional code block

}else{

Functional code block

}

When there are more than if in the program, the Else statement and the most recent if match. Sample code:

if (condition 1)

function code 1;

if (condition 2)

function code 2;

else function code 3;

The Else statement here matches the IF statement corresponding to condition 2, and the preceding condition 1 is a separate statement. In the actual code, you can use curly braces to make the entire structure of the program clearer.

For If-else statements, because the conditions of the IF and else are mutually exclusive, only the functional code in one statement is executed in practice.

In actual development, some companies, when writing conditions, require that you must write else, even if they do not write code in the Else statement, so that the condition is closed. This is not a grammatical necessity.

1.3 If-else

If-else statement in reality, sometimes the condition is not one, but a group of related conditions, such as the conversion of Arabic numerals to Chinese capital, according to the score conversion to the corresponding level, are multiple conditions of the structure, in order to avoid writing multiple if statements of the structure, provides a class of specialized multiple-branch statements, This is the If-else if-else statement.

The syntax format for the If-else if-else statement is:

if (condition 1)

function code 1;

else if (condition 2)

function code 2;

else if (condition 3)

function code 3;
......

else function code;

Syntax Description:

1. Else if it is else and if two keywords, space is used to interval between.

2, the condition 1 to the condition n are all Boolean types

3, else if statement can have arbitrary multiple sentences

4, the last else statement is optional

5, if the functional code part is not a statement block, that is, without curly braces, you can only write a sentence.

Execution process: When condition 1 is established, execute function code 1; When the condition 1 is not tenable and the condition 2 is established, the function code 2 is executed, if the condition 1, Condition 2 is not tenable, and the condition 3 is set up, then the function code 3 is executed, and so on, if all the conditions are not valid, then the function code of the Else statement is executed The flowchart for its execution process is shown above.

The following is an implementation based on the value of the month, output the number of days included in the month, February output 28, regardless of the leap year sample code:

int month = 3;

  int days = 0; Number of dates

  if (month = 1) {days

  =;

  } else if (month = 2) {days

  = n

  } else if (month = 3) {days

  = =]

  else if (month = 4) {Days

  = 30;
   } else if (month = = 5) {Days

  = +

  } else if (month = 6) {Days

  = =/

  else if (month = = 7) {

  Day s = to;

  else if (month = 8) {Days

  = +

  } else if (month = 9) {days =

  } else if (month = ten days

  = ;

  } else if (month = =) {Days

  =

  } else if (month = =)

  System.out.println (days);

Let's take a look at a sample code that has the function of converting hundred results to A, B, C, D, and E, and the code is as follows:

int score =;

  if (score >=) {

  System.out.println (' A ');

  } else if (score >=) {

  System.out.println (' B ');

  } else if (score >=) {

  System.out.println (' C ');

  } else if (score >=) {

  System.out.println (' D ');

  } else{

   System.out.println (' E ');

  }

It is clear from this code that each else if statement is written in a sequential order and must be written in a logical order when it is actually written, otherwise a logical error will occur.

If-else If-else statement is a multi-branch conditional statement provided in the Java language, but it is troublesome to write when judging some problems, so another statement--switch sentence is provided in the syntax to better realize the discriminant of multiple-branch statements.

The above Java if Else statement Getting Started Guide (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.