Day16 (enhanced for loop, break, continue, goto keywords, tags)

Enhanced for loop

  • Key use in the array
  • Java 5 introduced an enhanced for loop mainly used for arrays or collections
  • Java enhanced for loop syntax format:
for(声明语句:表达式){
    
    //代码}
  • Declaration statement: declare a new local variable, the type of the variable must match the type of the array element. Its scope is limited to the loop statement block, and its value is equal to the value of the array element at this time.
  • Expression: Expression is the name of the array to be accessed, or a method that returns an array.
public class A0119 {
    
    
    public static void main(String[] args) {
    
    
        int[]num={
    
    3,6,9,12,15};//定义了一个数组
        System.out.println(num);
        System.out.println(num[0]);
        System.out.println(num[1]);
        System.out.println("===============");
        for (int a = 0; a < 5; a++) {
    
    
        System.out.print(num[a]+"\t");
        }
        System.out.println();
        //遍历数组的元素
        for (int b :num){
    
    
            System.out.print(b+"\t");
 run:
 [I@1b6d3586
3
6
===============
3	6	9	12	15	
3	6	9	12	15	

break continue

  • In the subject part of any loop statement, break can be used to control the flow of the loop. Break is used to forcibly exit the loop without executing the remaining statements in the loop. (The break statement is also used in the switch statement)
  • The continue statement is used in the loop statement weight to terminate a loop process, that is, skip the statement that has not been executed in the loop body, and then determine whether to execute the loop next time.

Example:

        int a = 0;
        while (a < 16) {
    
    
            a++;
            if (a % 3 == 0) {
    
    
                break;}
            System.out.print(a+"\t");
        }
        System.out.println("\n===========");
        int b = 0;
        while (b < 16)
        {
    
    b++;
            if (b % 3 == 0) {
    
    
                continue;} //continue之后的while语句是被跳过的
            System.out.print(b+"\t");}
run:
1	2	
===========
1	2	4	5	7	8	10	11	13	14	16	

goto keyword

  • The goto keyword appeared in programming languages ​​very early. Although goto is still a reserved word in Java, it has not been formally used in the language; Java does not have goto. However, in the two keywords of break and continue, we can still see some shadows of goto-the labeled break and continue.
  • "Label" refers to an identifier followed by a colon, for example: label:
  • For Java, the only place where tags are used is before the loop statement. The only reason to set the label before the loop is: we want to nest another loop in it, because the break and continue keywords usually only interrupt the current loop, but if used with the label, they will break to the place where the label exists.

Output 1-100 prime numbers, change one line for every 5 output:

    public static void main(String[] args) {
    
    
        //输出1-100的质数 每5个输出一行
        int c=0;
       comehere:for (int a = 3; a <= 100; a++)//comehere: 这个是标签符(字符串+:)
       {
    
    for(int b=2;b<a/2;b++)
       {
    
    if (a%b==0)continue comehere;}//continue 跳到标签处
       c=c+1;
           System.out.print(a+"\t" + "");
           if (c%5==0){
    
    
               System.out.println();
run:
3	4	5	7	11	
13	17	19	23	29	
31	37	41	43	47	
53	59	61	67	71	
73	79	83	89	97	

Guess you like

Origin blog.csdn.net/SuperrWatermelon/article/details/112798310