The If-switch selection structure in Java

Source: Internet
Author: User
Tags ming

Words and essays walk, pen to walk, essays, as you pleases. Purely individual Learning Analysis summary, if the viewer also please do not Sik Sik Yuen experience.

1.IF selection Structure

What is the IF structure: the If selection structure is a grammatical structure that is processed according to the judgment result.

The syntax is:

if (judging condition) {         action content}

If also contains if-else & multiple if and nested if three kinds.

if-else type           nested if                  multiple ifif(judging condition) {          if(interpretation condition) {if                ( (interpretation condition) {Action Content if (interpretation condition) {action content} else {action content                                                }else if{
Action Content} action content
}}}else{Action content }

if () {} is used for simple judgments, and these three are used primarily for complex judgments.

Process: The program is executed from top to bottom. First determine whether the condition is established, and then execute the contents of the else{} brace downwards.

In life when we do something, but there are prerequisites, then we can use the if selection structure.

For example: Xiao Ming is over 18 years old, car will buy a ticket. It can be seen and inferred that if Xiao Ming needs to buy tickets, then Xiao Ming's age must be greater than or equal to 18.

Age >=18 to buy tickets, on the contrary, the age <18 do not buy tickets. Then I can use the if structure to judge and get the result.

int age=18; if (age>=18) {    System.out.println ("Xiao Ming's age greater than 18 need to buy tickets");} Else {    System.ou.println ("Xiao Ming's age is less than 18 do not need to buy tickets");}

Precautions:

1. If there is a statement in a pair of curly braces after the IF keyword, you can omit the curly brace, but in order to avoid having to forget the curly braces when there are multiple statements, and to keep the overall style of the program consistent, it is recommended not to omit the braces for the IF structure.

Below is an explanation of the if selection structure under complex conditions

If the structure of the if selection in complex situations is to use multiple judging conditions, and then many conditions, we have to say the usual logical operators.

Common logical operators have:&& (with, and) | | (or, OR)! Non- 1.&&

1.&& condition 1&& Condition 22 conditions are true at the same time, the result is true; even if one of the conditions is false, the result is false.

2.| | Condition 1| | Condition 22 Conditions One is true, the result is true, and two conditions are false, the result is false.

3! Condition 1! Condition 2 When the condition is true, the result is false, and if the condition is false, the result is true.

Example: Zhang San's java score >90 and Zhang San's music score >80

Or

Zhang San's java score ==100 and Zhang San's music score >70

Caveats: You can use parentheses to control when operators are more numerous and cannot determine the order in which operators are executed.

First Method: score1>90&&score2| | Score1==100&&score2>70

The second method: (Score1>90&&score2) | | (score1==100&&score2>70)

It is recommended to use the second method to clearly indicate the condition

*************************************************************************************

2IF-ELSE selection Structure

The grammatical structure of if-else is;

if (judging condition) {    execute content      }else{          execute content                       }            

Above we learned the IF () {} Base selection structure

The grammatical structure of if-else is to add a else{} (otherwise) statement on the basis of the IF structure. As the name implies, when we give a statement, and set the condition, the setting executes the contents of the curly braces in the IF () {}, and executes each step, and then comes to else, but it does not finish executing the contents of the IF () {} in curly braces after executing else. So when does it happen? is to execute the contents of the else{} brace when the judging condition in the if () parenthesis is not valid.

That is, if the condition is true, the contents of the IF () {} Curly brace are executed, otherwise the contents of the else{} brace are executed.

Example: If Zhang San exam result is more than 90 points, the teacher will reward him, otherwise punish.

 Public class test{  publicstaticvoid main (string[] args) {                                                    int score=90; if (scor>90)                                         {System.ou.println ("reward"); }Else{System.ou.println ("Penalty");  }                                             

3. Multiple from If structure

The grammatical structure of multiple if;

if (condition 1) {
Code Block 1
}else if (condition 2) {
Code Block 2
}else{
Code block 3
}

So how does the multiple if structure execute? For example, first, the program to determine the condition 1, if set up, then execute the code block 1, and then jump out of the multiple if selection structure, if not, then judge the condition 2, Condition 2 is established, then execute the code block 2, and then jump out of this multiple if selection structure, if the condition 2 is not set, then the code block 3 Then jump out of this multiple if selection structure.

Example: If the exam results >=80, >=60 is less than 60 is poor, assuming that Zhang San's score is 70, the interpretation of his grades is.

 Public classtest{ Public Static voidMain (string[] args) {intScore=70; Fi (Score>=80) {System.out.println (Excellent);}Else if(score>=60) {System.out.println (In);}Else{System.out.println (Poor); }}}

Advantage: Multiple If selection structures have a great advantage in solving the conditions that need to be judged as continuous intervals.

Note: Many more from the if selection structure the last else can be ignored in bloom.

*************************************************************************************

3. Nested IF selection structure

Nested IF selection structure syntax;

if (condition 1) {   if(condition 2) {       code block 1   }else{         code block 2   }}else  {       code block 3}

Example:

The school held a sports meeting, the Hundred meters race results in 10 seconds or less students are eligible to enter the final, according to gender divided into men and women groups.

Now let's use the nested if selection structure to code the problem.

ImportJava.util.*;  Public classtest{ Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); System.out.print ("Please enter the match score (s):"); DoubleScore=input.nextdouble (); System.out.print ("Please enter the match score (s):"); DoubleScore=input.nextdouble (); if(score<10){                     if(Gender.equals ("Male") {System.out.print ("Into the men's group"); }Else if(Gender.equals ("female") {System.out.print ("Into the women's group"); }                }Else{System.out.print ("You have not entered the final"); }  } }                                        

Precautions:

1. The condition of the inner if selection structure is judged only if the condition of the outer if selection structure is satisfied.

The 2.else is always signed with the closest missing else if pairing.

*************************************************************************************

At this point, this chapter for the selection of the structure of learning to this end, I hope you practice, early master. Thank you.

The If-switch selection structure in Java

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.