Small white Java Learning path "Select structure (i)"

Source: Internet
Author: User
Tags logical operators

If selection structure:

The If selection structure is a grammatical structure that can be processed after the condition is judged.

1.if syntax for selection structure:

 public  class   demo{ public  static   void Main (String[]args) { //  statement 1;             //  statement 2;  if   (condition) { //  Statement 3;    " //  statement 4;  }}  /pre>
Case: Bonus MP4 If Java exam scores are greater than 98 points Public classDemo02 { Public Static voidMain (string[] args) {Scanner input=NewScanner (System.inch); System. out. println ("Please enter Zhang Hao's java exam results:"); intScore=Input.nextint (); //if the score is greater than 98 points, reward a MP4                        if(score>98) {System. out. println ("Reward a MP4"); } System. out. println ("Test it! ");

2.

Logical operators:

&&: and
A && b:a and b two expressions are simultaneous (true) the entire expression (a && B) is true
|| : OR
A | | B:a and b two expressions which have a whole expression at the time of Formation (JA | | b) is true
!: Non-
!a: The result of an expression takes the opposite value

3.if-else:

Grammar:

 Public class demo{    publicstaticvoid  Main (String[]args) {         if (condition) {                // code block 1            }else{            //  code block   2                           }}}


code block one is executed when the condition is true and does not walk code block 2
Code block Two is executed when the condition is false and does not walk code block 1

case: If Zhang Hao Java exam results more than 98 points, the teacher will reward him a MP4, otherwise the teacher will punish him to code Public classDemo04 { Public Static voidMain (string[] args) {//Create a scannerScanner input=NewScanner (System.inch); System. out. println ("Please enter your Java exam results:"); intJava=Input.nextint (); //code Block 1 is executed if the test result is greater than 98 conditions true//code block 2 is executed when the condition is false                        if(java>98){                            //code block 1System. out. println ("Reward a MP4"); }Else{                            //code block 2System. out. println ("Transcription Code"); }                                            }                }

4. Multiple If selection structure:
Grammar:

if (Score >=) {                 //         }Elseif (score >= ) {                 //        }else  {                 /  /  } score >=80 for Condition 1 score >=60 for condition 2                          

If the result of condition 1 is true, code block 1 will be executed, code block 1 will not judge other conditions after execution, directly execute the multiple if selection structure below the code
If the condition 1 result is false will again judge the condition 2, if the condition 2 is true will execute code block 2, code block 2 executes after completion will not judge other conditions, directly execute the multiple if selection structure below the code
If the condition 2 result is false when the code block 3 is executed, code block 3 executes, directly executes the multiple if selection structure below

Case Study: Evaluation results of students ' final exam results>= the: Good, score >= -: Medium, score < -: Poor Public classDemo05 { Public Static voidMain (string[] args) {Scanner input=NewScanner (System.inch); System. out. println ("Please enter the result of the completion test:"); intScore=Input.nextint (); /** Test results of Students ' completion >=80: good results >=6 0: Medium Grade <60: Poor * **/                        if(score>= the) {System. out. println ("Good"); }Else if(score>= -) {System. out. println ("Medium"); }Else if(score< -) {System. out. println ("Poor"); }                    }                }

5. Nested IF selection structure:
Grammar:

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


If condition 1 is true, go to judge condition 2, if condition 2 is true, code block 1 will be executed, if condition 2 is false, code block 2 will be executed.
If condition 1 is false, go back to execute code block 3

Case: The school holds the sports meeting, the hundred meters race runs into 10 seconds the student to be eligible to enter the final, enters the male group and the female group according to the gender respectively Public classDemo07 { Public Static voidMain (string[] args) {Scanner input=NewScanner (System.inch); System. out. println ("Please enter your score:"); intScore=Input.nextint (); if(score<=Ten) {System. out. println ("go to the finals! "); System. out. println ("Please enter your gender:"); String Sex=Input.next (); if(Sex.equals ("male") ) {System. out. println ("Enter the men's team finals"); }Else{System. out. println ("Enter the women's team finals"); }                                                    }Else{System. out. println ("you are too slow! , did not enter the final"); }                    }                                    }

Small white Java Learning path "Select structure (i)"

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.