Java 12.for statement

for statement

One study java for loop is commonly used statement. for statement is the most flexible and most commonly used loop structure.


for a statement before the execution, we must first determine whether the conditional expression is true loop. If the result of the conditional expression is false, then it will not execute the loop. for statement is usually used in a loop to know the number of cycles in.


As shown in the for statement syntax is as follows.

for (Conditional Expression 1; Conditional Expression 2; conditional expression 3) 
{ 
block of statements; 
}
public static void main(String args[]) {
          for(int x = 10; x < 20; x = x+1) {
              System.out.print("x的值是 : " + x );
              System.out.print("\n");
           }
       }

 

Nested loop:

public class Stars{
    public static void main(String[] args){
      final int MAX_ROWS = 10;

      for (int row = 1; row <= MAX_ROWS; row++){
         for (int star = 1; star <= row; star++)
            System.out.print("*");

         System.out.println();
      }
   }
}

 

Guess you like

Origin www.cnblogs.com/H97042/p/10960141.html