(Memo) Java for statement

Iterative processing with for statement

The processes called loop processing and iterative processing are summarized. The following three are prepared in Java.

--for statement --while statement --do statement (do while)

Descriptive formula

-** for ( Initial value setting </ font>; Continuation condition </ font>; Value manipulation </ font> font>) { Processing </ font>} **

-** while ( condition </ font>) { processing </ font>} **

-** do { Processing </ font>} while ( Condition </ font>); **

Let's write the code

A program that inputs numbers and displays only the number *

 
import java.util.Scanner;
 
class PutKome {
 
  public static void main(String[] args) {
    Scanner stdIn = new Scanner(System.in);
 
    System.out.print("How many rice do you want to display:");
    int n = stdIn.nextInt();
 
    for (int i = 0; i < n; i++) {
      System.out.print('*');
    }
    System.out.println();
  }
}

【result】

What pieces*Do you want to display: 5
*****

Commentary

  1. Receive the input value of n
  2. [Initial value] Substitute 0 for i
  3. [Continuing condition] i is less than n?
  4. Yes: Output *, [Operation] i + 1… Repeat until No / No: Line feed

1566876503855.jpg

Recommended Posts