Select structure (1)

1.if selection structure

if selection structure is based on conditional judgment before processing. 
    Grammar: basic if selection structure 
              if (condition) {
                      // code block 
              } 

        eg: if (score> 90) {                        // judgment is greater than 90 points 
                      System.out.println ( "The teacher said: Yes, reward an MP4!" ); 
              }

2. Logical operators    *

Operator expression description 
  && Condition 1 && Condition 2 Only two conditions are true at the same time, the result is true
  || Condition 1 || Condition 2 As long as one of the two conditions is true, the result is true
  !! When the condition is true, When the result is false and the condition is false, the result is true. 

Operator priority order:! > Arithmetic operator> Comparison operator> &&> || 
  eg: if ((score1> 98 && score2> 80) || (score1 == 100 && score2> 70 )) { 
         System.out.println ( "The teacher said: good , Reward an MP4! " ); 
     }

3.if-else selection structure

    Question: If Zhang Hao ’s Java test score is greater than 98 points, the teacher rewards him with an MP4, otherwise the teacher fines him for encoding
       * Use two basic if selection structures 
      eg: if (score> 98 ) { 
               System.out.println ( " The teacher said: Yes, reward an MP4! " ); 
          } 
          If (score <= 98 ) { 
               System.out.println ( " Teacher said: punish for coding! " ); 
          }
        * Use if-else to choose the structure to achieve 
      Syntax: if (Condition) { 
                      // Code block 1   
           } else { 
                     // Code block 2 
           } 
       eg: if (score> 98 ) {
               System.out.println ( "Teacher said: Yes, reward an MP4!" ); 
          } Else { 
               System.out.println ( "Teacher said: Penalty for coding!" ); 
        } ! Tips:
 

  / * Method of generating random numbers (0 ~ 9) 
    int random = (int) (Math.random () * 10);   * /

4. Multiple if selection structure ***

Problem: final examination scores for the participants evaluation 
              score > = 80 : Good 
              results > = 60 : Average 
              score <60    : difference 
      Syntax: IF (score> = 80 ) { 
                    // code block. 1   
            } the else  IF (score> = 60 ) { 
                    // Code block 2 
            } else { 
                   // Code block 3 
               } 
        eg: int score = 70;     // Exam score 
              if (score> = 80 ) {    
                     System.out.println ( "Good"); 
              } else  if (score> = 60 ) {     
                     System.out.println ( "Medium" ); 
              } else {     
                     System.out.println ( "Poor" ); 
              }

 / * The order of each condition is related to its output, not arbitrary change*/

5. Nested if selection structure *****

     Question: The school holds a sports meeting. Students who run within 100 seconds of the 100-meter race are eligible to enter the finals. They will enter the men's and women's groups according to their gender. 
     Analysis: Determine whether they can enter the final 
. , Or enter the women's group 
     Grammar: if (condition 1) {
               if (condition 2) {
                    // code block 1 
              } else {
                   // code block 2 
                  } 
          } else {
                // code block 3 
          } 
    
     eg: if (score <= 10 ) {
             if (gender.equals ("男" )) { 
                 System.out.println ( "Enter the men's finals!" ); 
            } else if (gender.equals ("Female" )) { 
                 System.out.println ( "Enter the women's finals!" ); 
            } 
        } else { 
             System.out.println ( "Eliminate!" ); 
        }       

6.if structure writing specification:

     In order to make the if structure more clear and avoid execution errors, the code blocks contained in each if or else should be enclosed in braces. The 
     inner if structure must have a certain indentation 
     relative to the outer if structure . If and else should be left aligned

 

Guess you like

Origin www.cnblogs.com/yun---meng/p/12682435.html
Recommended