1024programmer Java Comprehensively master the use of loop control statements and conditional judgment statements in Java

Comprehensively master the use of loop control statements and conditional judgment statements in Java

Loop control
There may be a situation when we need to execute a block of code several times, usually called a loop.
Java has a very flexible three-loop mechanism. You can use one of the following three loops:

  • while loop
  • do…while loop
  • for loop

As of Java5, enhanced for loops have been introduced. This is mainly used for arrays.

while loop
A
while loop is a control structure that repeats a task a specific number of times.

Grammar

The syntax of while loop is:

 while(Boolean_expression)
 {
   //Statements
 }
 

When executed, if the result of the Boolean expression is true, the actions in the loop will be executed. As long as the expression evaluates to true, execution continues.

Here, the key point of the while loop is that the loop may not run forever. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example

 public class Test {

   public static void main(String args[]) {
    int x = 10;

    while( x <20 ) {
      System.out.print("value of x : " + x );
      x++;
      System.out.print("\n");
    }
   }
 }

 

This will produce the following results:

 value of x : 10
 value of x : 11
 value of x : 12
 value of x : 13
 value of x : 14
 value of x: 15
 value of x : 16
 value of x : 17
 value of x : 18
 value of x : 19
 

do…while loop
The do … while loop is similar to the while loop, except that a do … while loop is guaranteed to execute at least once.

Grammar

The syntax of do…while loop is:

 do
 {
   //Statements
 } while (Boolean_expression);
 

Note that the Boolean expression appears at the end of the loop, so the statements in the loop are executed once before the Boolean test.

If the Boolean expression is true, control flow jumps back, and the statements in the loop are executed again. This process is repeated until the Boolean expression is false.

Example

 public class Test {

   public static void main(String args[]){
    int x = 10;

    do{
      System.out.print("value of x : " + x );
      x++;
      System.out.print("\n");
    }while( x <20 );
   }
 }

 

This will produce the following results:

 value of x : 10
 value of x : 11
 value of x : 12
 value of x : 13
 value of x : 14
 value of x: 15
 value of x : 16
 value of x : 17
 value of x : 18
 value of x : 19
 

for loop
A
for loop is a loop control structure that allows you to efficiently write a loop that needs to be executed a specific number of times.

For loops are beneficial when you know how many times a task will be repeated.

Grammar

The syntax of for loop is:

 for(initialization; Boolean_expression; update)
 {
   //Statements
 }
 

The following is the control flow of a for loop:

The initialization step is performed first, and only once. This step declares and initializes any loop control variables. There is no need to put a declaration here, just a semicolon.

Next, the Boolean expression is evaluated. If true, the loop body is executed. If it is false, the loop body is not executed, and the flow control jumps to the next statement after the for loop.

Afterwards, when the loop body is executed in the for loop, the control flow jumps back to the update statement. This statement allows any loop control variable to be updated. This statement can be left blank as long as a semicolon appears after the Boolean expression.

Boolean expressions are now evaluated again. If it is true, the loop executes and the process repeats (loop body, then update step, then boolean expression). After that, the Boolean expression is false and the loop terminates.
Example

 public class Test {

   public static void main(String args[]) {

    for(int x = 10; x <20; x = x+1) {
      System.out.print("value of x : " + x );
      System.out.print("\n");
    }
   }
 }

 

This will produce the following results:

 value of x : 10
 value of x : 11
 value of x : 12
 value of x : 13
 value of x : 14
 value of x: 15
 value of x : 16
 value of x : 17
 value of x : 18
 value of x : 19
 

New features of for loop in Java
As of Java5, the enhanced for loop was introduced. This is mainly used for arrays.

Grammar

The syntax of the enhanced for loop is:

 for(declaration : expression)
 {
   //Statements
 }
 

Declaration: Newly declare a block variable that is compatible with the element in the array you are accessing. The variable is available within the for block and its value will be the same as the current array element.

Expression: This calculation result needs to be looped through the array. The expression can be an array variable or a method call that returns an array.
Example

 public class Test {

   public static void main(String args[]){
    int [] numberem.out.print("This is else statement");
    }
   }
 }

 

This will produce the following results:

 Value of X is 30
 

Nested if…else statements
It is always legal to nest if-else statements, which means you can use an if or else if statement within another if or else if statement.

Grammar

The syntax of nested if…else is as follows:

 if(Boolean_expression 1){
   //Executes when the Boolean expression 1 is true
   if(Boolean_expression 2){
    //Executes when the Boolean expression 2 is true
   }
 }
 

Because we have nested if statements, we can nest else if…else in a similar way.

Example

 public class Test {

   public static void main(String args[]){
    int x = 30;
    int y = 10;

    if(x==30){
      if(y==10){
        System.out.print("X = 30 and Y = 10");
      }
     }
   }
 }

 

This will produce the following results:

 X = 30 and Y = 10
 

switch statement
The
switch statement allows a variable to be tested for equality against a series of values. Each value is called a case, and the enabled variable is checked for each case.

Grammar

The syntax of the enhanced for loop is:

 switch(expression){
   case value:
     //Statements
     break; //optional
   case value:
     //Statements
     break; //optional
   //You can have any number of case statements.
   default : //Optional
     //Statements
 }
 

The following rules apply to switch statements:

  • The variable used in the switch statement can only be a byte, short, int or char.
  • There can be any number of case statements in a switch statement. Each case is followed by the value to be compared and a colon.
  • For case the value must be of the same data type as the switch variable, which must be a constant or literal.
  • When the activated variable is equal to the case, the statement after the case will be executed until break.
  • When a break statement is reached, the switch terminates and control flow jumps to the next line following the switch statement.
  • Not every case needs to contain a break. If break does not occur, the control flow will flow through the subsequent cases until break.
  • A switch statement can have an optional default case, which must appear at the end of the switch. When no case is true while performing a task, the default case can be used. Break is not required in the default case.

Example

 public class Test {

   public static void main(String args[]){
    //char grade = args[0].charAt(0);
    char grade = 'C';

    switch(grade)
    {
      case 'A' :
       System.out.println("Excellent!");
       break;
      case 'B' :
      case 'C' :
       System.out.println("Well done");
       break;
      case 'D' :
       System.out.println("You passed");
      case 'F' :
       System.out.println("Better try again");
       break;
      default :
       System.out.println("Invalid grade");
    }
    System.out.println("Your grade is " + grade);
   }
 }

 

Compile and run the program above using various command line arguments. This will produce the following results:

 $javaTest
 Well done
 Your grade is a C

 

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/703493

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索