Java from entry to the master Chapter 4 Process Control

table of Contents

Conditional statements

loop statement

Loop control

Exercise


Conditional statements

  • if
  • if else
  • if else if
    • Value of the expression must be a boolean type
    • If there are no braces else with the nearest if match
    • A single sequence of statements can omit the braces, but it is best not to omit, to increase readability
    • If there is no statement can be used
    • else if else and can not be used alone, must be used in combination if
  • switch case:
    • Expression value must be an integer, character or string (after JDK1.7), enum
    • Value must not be the same between the case
    • Each situation encountered end break, no break continues until it encounters a break down
    • Behind the implementation is not in line with the situation of default, default optional
package Number;

public class ex4_5_Getifelse {
    public static void main(String[] args) {
        int math = 95;
        int english = 56;
        if (math > 60) {
            System.out.println("math is passed");
        } else {
            System.out.println("math is not passed");
        }
        if (english > 60) {
            System.out.println("english is passed");
        } else {
            System.out.println("english is not passed");
        }

        int x = 20;
        if (x > 30) {
            System.out.println("x>30");
        } else if (x > 10) {
            System.out.println("x>10");
        } else if (x > 0) {
            System.out.println("x>0");
        }
    }
}

 

package Number;

public class GetSwitch {
    public static void main(String[] args) {
        int week = 2;
        switch (week) {
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            case 3:
                System.out.println("3");
                break;
            default:
                System.out.println("sorry");
        }
    }
}

loop statement

  • while() {}
    • Expression NOT NULL
    • Expressions are not permitted to false
    • Conditions have changed in the loop body statement
  • do {} while ();
    • At the end of more than a semicolon
    • first determine and then performed while, do while performing the first determination and then
  • for(;;)
    • Expression initialization expression 1, expression 2 is the value of boolean expressions, Expression 3 after trimming the variable cycle operation expression, to change the loop condition
    • Sometimes used for (;;) to achieve an infinite loop, using a break out of the loop
  • for (type x: traverse the object obj) {statement cited java of x}
package Number;

public class ex4_10_GetSum {
    public static void main(String[] args) {
        int x = 1;
        int sum = 0;
        while (x <= 10) {
            sum = sum + x;
            x++;
        }
        System.out.println("sum=" + sum);

        int a = 100;  // while -- do...while
        while (a == 60) {
            System.out.println("ok1");
            a--;
        }
        int b = 100;
        do {
            System.out.println("ok2");
            b--;
        } while (b == 60);
    }
}
package Number;

public class ex_4_12_Circulate {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 2; i <= 100; i += 2) {
            sum = sum + i;
        }
        System.out.println("sum:" + sum);

        // foreach
        int arr[] = {7, 10, 1};
        for (int x : arr) {
            System.out.println(x);
        }
    }
}

Loop control

  • break the cycle ends
    • break tag; end of the specified loop. For example, the outer loop ends.
  • continue this cycle ends
    • continue label; designated end of this loop. Such as the end of this time the outer loop, the outer loop continues to the next.
package Number;

public class ex4_14_BreakTest {
    public static void main(String[] args) {

        // for
        for (int i = 0; i <= 100; i++) {
            System.out.println(i);
            if (i == 6) {
                break;
            }
        }
        System.out.println("----end----");

        //使用标签功能,结束外层循环
        Loop:
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 6; j++) {
                if (j == 4) {
                    break Loop;
                }
                System.out.println("i=" + i + " j=" + j);

            }
        }

        //continue
        for (int i = 1; i < 20; i++) {
            if (i % 2 == 0) {
                continue;
            }
            System.out.println(i);
        }
    }
}

Exercise

  • Print diamond
  • Use while computing 1 + 1/2! + 1/3! + ... + 1/20!
package Number;

public class ex4_practice_2 {
    public static void main(String[] args) {
        //应用for循环打印菱形
        int lineCount = 7;  //菱形的总行数
        int maxLineNum = lineCount / 2 + 1;  //菱形的上半部分行数
        for (int i = 1; i <= maxLineNum; i++) {
            for (int space = maxLineNum - i; space >= 1; space--) {  //空格数与行数相反
                System.out.print(" ");
            }
            for (int star = 1; star <= i * 2 - 1; star++) {
                System.out.print("*");
            }
            System.out.println();
        }

        //菱形下半部分
        for (int i = maxLineNum - 1; i >= 1; i--) {
            for (int space = 1; space <= maxLineNum - i; space++) {
                System.out.print(" ");
            }
            for (int star = 1; star <= i * 2 - 1; star++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
package Number;

public class ex4_practice_3 {
    public static void main(String[] args) {
        int t = 20;
        double sum = 0;
        for (int i = 1; i <= t; i++) {
            int multi = 1;
            
            for (int j = 1; j <= i; j++) {
                multi = multi * j;
            }
            sum = sum + 1.0 / multi;
        }
        System.out.println("for--sum: " + sum);

        int i = 1;
        int m = 1;
        double sum2 = 0;
        while (i <= t) {
            m *= i;
            sum2 += 1.0/m;
            i++;
        }
        System.out.println("while--sum: " + sum2);

    }
}

 

 

 

 

 

 

 

 

 

 

 

Published 46 original articles · won praise 0 · Views 1035

Guess you like

Origin blog.csdn.net/weixin_37680513/article/details/103299521