Analyzing conditions java

Conditional

java language C language and the like, including three basic control flow structures:

1. The program order execution

2. Analyzing the structure of the program

3. The program loop structure

If we understand the keywords previously learned vocabulary into the java language, then structured programming language is java-called "grammar" of this language to communicate with a computer is very simple.

We mentioned in the previous section, java is a semicolon (;) as the end of a statement, with no line breaks relations, must be behind any one expression with a semicolon (;), it's only the end of normal, otherwise it will report a syntax error, such as:

int a=100;
System.out.println(a);

Above all into a single statement, and put into a plurality of statements of the form "{...} ... Code" This code block become the statement block is to the left brace "{" begins to right brace end "}" code block we become statements about braces must appear in pairs, blocks can be nested with each other. Blocks can be used as a whole, similar to the plurality of blocks of statements combined into a block. We may optionally be used include several statements some statements in the code block, sometimes referred to as blocks. However, in some cases, the braces is essential, such as when a defined class, or define a common method, it is necessary to back braces, wrap section block. Such as the following:

public  class Abc { // this is a must 
   public  static  void main (String [] args) { // This is necessary 
      a float A = 346.756565f ;
       // this can remove the braces 
      {
          int B = ( int ) + A 10 ; // convert a integer 
         System.out.println (B); 
      } 
   } 
}

And the following conditional loop we mentioned judgment syntax is to block heavy use.

Decision logic is that we live in the most common logic, computers to perform with the human mind is very similar: if [conditions are met] on how to do it, otherwise do that. So to speak, the computer is the best at judging true / false of.

A, if-else statements

The if statement is the most common judgment statement, by the judgment of the condition (conditional) feel to the program. The basic format is as follows:

if (conditional expression)

Statement 1

else

Statement 2

Before the implementation of the judgment statement, the statement is the first implementation of the conditional expressions, conditional expressions return the result must be Boolean (boolean), return to the expression according to the conditions, if it is true, then the implementation of the contents of the statement of 1 If it is a false statement 2 that follows else. As in the following example:

int   I =. 1 ;
 IF (I> 10 ) 
  System.out.println (I + "greater than 10" );
 the else 
  System.out.println (I + "is not greater than 10");

Define the integer i 1 and assignment, and then executed if the conditional formula which i is greater than 10, the result returns false, so the latter is executed else statement.

In the above format, else optional part, so the easiest following conditional formula:

if (conditional expression)

Statement 1

As in the following example:

int  i=1;
if(i>10)
   System.out.println(i+"大于10");

Since the conditional is false, so the program does not output anything. It is also because else is optional part, a problem of understanding will appear in the nest when sometimes we are faced with the following example is not clear when it will feel the

int i = 1;
if (i > 10)
   if (i < 5)
       System.out.println(i + "小于5");
   else
       System.out.println(i + "不大于10");

else if it is a correspondence that? if java is a recently paired. When programmers write code, properly indented code can also improve the readability of the code, of course, we have a better solution.

else if the back and may be followed by statement, a block statement is of course also possible to follow the following format:

if (conditional expression) {

Statement block 1

}else{

Statement block 2

}

Suggest that you write if statement, even if only a statement later, you can also use braces wrap to form a block of statements, this can increase the readability of the program, as in the example above, can be modified to become like this:

IF (I> 10 ) {
     IF (I <. 5 ) { 
        System.out.println (I + "less than. 5" ); 
    } the else { 
        System.out.println (I + "is not greater than 10" ); 
   } 
}

Two, else-if statements

Sometimes more than two conditional, it may be necessary to use else-if statements, and its syntax is as follows:

if (conditional expression)

Statement 1

else if (conditional expression)

Statement 2

else if (conditional expression)

Statement 3

else if (conditional expression)

Statement 4

...

else

Statement

Such a statement will be frequently used in our future programming, the judgment process is to determine the conditional expression from top to bottom, if the first returns false judgment will be the second, and so on, but if one returns true, it will execute the statements that follow, and then the entire else-if statement will exit back if there is else-if statements are not to judge in the execution. We often will also be added at the end face an else statement, of course, this is optional, if such an effect is above all if judgments are false, then it will perform behind the else statement. Like the above if-else, like the back also can be followed by a block of statements, in order to enhance the readability of the program, we also often use the back block. Format is as follows:

if (condition 1) {

Logic executed when condition 1 == true

} Else if (condition 2) {

Logic executed when condition 2 == true

} Else if (condition n) {

Logic executed when the condition n == true

}else{

Not satisfy the above conditions and default execution logic

}

Now we realize with a few, such as we have to determine users younger than 16 years old are not allowed visit the game site, then we can use the following code:

int uage = 17 ;
 IF (uage <18 ) { 
   System.out.println ( "Sorry, please pay attention to school!" ); 
} the else { 
   System.out.println ( "Welcome!" ); 
}
int Result = 85; // score   
IF (Result> 90 ) { 
  System.out.println ( "Excellent" ); 
} the else  IF (Result> 80 ) { 
  System.out.println ( "good" ); 
} the else  IF ( Result> 60 ) { 
  System.out.println ( "qualified" ); 
} the else { 
  System.out.println ( "fail" ); 
}

Analyzing can also be nested if used, i.e. where the block of statements may also include a determination expressions if, as in the following example. Scanner wherein obtaining user input object, consider the following example:

Import java.util.Scanner;
  public  class Tt of the {
    public  static  void main (String [] args) {
       int NUM0; // first number 
      int num1; // second number 
      int type; // Calculation type 
 
      System.out .print ( "*" Please enter NUM0: "*" ); 
      Scanner the SCR = new new Scanner (System.in); 
      NUM0 = scr.nextInt (); // program will be here waiting for input from the user 
 
      System.out.print ( "*" Please enter num1: "*" ); 
      the SCR = new new  Scanner (System.in);
      num1 =scr.nextInt (); 
of System.out.print ( "*" Please enter a calculation type (0 plus; Save represents 1; represented by 2; 3, in addition to): "*" ); 
      SCR = new new Scanner (the System.in ); 
      type = scr.nextInt (); 
 
IF (type == 0 ) { 
      System.out.println (NUM0 + "*" + "*" + num1 + "*" = "*" + (+ NUM0 num1)) ; 
   } the else  IF (type ==. 1 ) { 
      System.out.println (NUM0 + "*" - "*" num1 + + "*" = "*" + (NUM0 - num1)); 
   } the else  IF (type = 2 = ) { 
      the System.out.println(num0 + "*"*"*" + num1 + "*" ="*" + (num0 * num1)); 
   } the else  if (type ==. 3 ) {
          // division, using nested if statements divisor can not be determined 0 
       IF (num1 == 0 ) { 
          System.out.println ( "*" divisor is not 0 "*" ); 
      } the else { 
          System.out.println (NUM0 + "*" / "*" num1 + "*" = "*" + (NUM0 / num1)); 
      } 
 
   } the else {
    // invalid input 
     System.out.println ( "*" is entered incorrectly your type can only be calculated [0,1,2,3] "*! " ); 
   } 
   } 
}

In this procedure using the user's input Scnner get the program to run scr.nextInt () time will stop, waiting for user input, the user input will continue to run down by a carriage return program, in the program we determining a division of the divisor is nested determination can not be zero.

Three, switch selection statement

Analyzing switch statement is another statement written, when such a sentence is selected values ​​to test for equality clause case, in fact, its functionality and determines if statement, just a different way of writing, can be between no if statement Introduction interoperability, grammar above. The specific syntax is as follows:

switch (variable is determined)

{

case Condition 1:

Execution condition logic 1

break;

case Condition 2:

Execution condition logic 1

break;

case n:

Logic execution condition n

break;

default:

Not satisfy the above conditions and default execution logic

}

Just behind the switch is judged variables, when conditions are equal behind the case, then the back of the case statement will be executed, the final surface is the default option, may need to decide whether to add based on your business logic function is similar to the else statement It is that executes the statement that follows the default case when all of the above conditions are not met.

It is noteworthy that, in JDK 7 previously participated variables determine the type can only be int, byte, char, short data types, but after JDK 7, the variable type is judged to be enhanced support for strings of String judgment. If you're still using JDK 6 should pay special attention to this point up.

Generally, switch case with a successful match, the order of execution will continue after all the code, it is generally judged to be added later successfully break statement out determination block. For details on the break keyword we will explain in a later chapter.

Look at this example: known variables int month = 1, using a switch statement to determine if it is equal to 1 month output "Jan", equal to 2 outputs "February", and so on. Codes are as follows:

int month The =. 4 ; 
 Switch (month The) {
       Case . 1 : 
         System.out.println ( "January" );
          BREAK ;
       Case 2 : 
         System.out.println ( "February" );
          BREAK ;
       Case . 3 : 
         the System.out .println ( "March" );
          BREAK ;
       Case 4 : 
         System.out.println ( "April" );
          BREAK ;
            // ... in the middle of 5 to 12 users to add their own. 
      default :
         System.out.println ( "month The only 1 to 12" ); 
}

 

Guess you like

Origin www.cnblogs.com/weibanggang/p/11184550.html