The basic syntax (lower)

This section is connected to the section, we continue to explain the basis of the remaining content of Java syntax. Operating conditions including the flow control statements, loop and related operations and jump statement input by the user, the array.

Process control is vital for any one programming language, which provides the basic means to control procedural steps for us. To be divided into common, conditional statements, looping statements, a jump statement.

Knowledge Point

  • Process Control
  • Array
  • User input operation

    determining if the statement is a statement.

    grammar:

    if(条件){
        条件成立时执行的代码
    }
    

    if statement execution

    When if ... else statement when the condition is satisfied, if part of the code blocks is performed; when the condition is not satisfied, the process proceeds to the else part. For example, if the number of days a month more than 30 days, compared Otsuki, Satsuki otherwise.

    grammar:

    if(条件){
        代码块1
    }
    else{
        代码块2
    }
    

    Execution if ... else statement

    Multiple if statements, in the case where the condition 1 is not satisfied, condition 2 will be determined, in order downward; when the current condition of the surface are not established, the final execution of the code in the else block.

    grammar:

    if(条件1){
        代码块1
    }
    else if(条件2){ 代码块2 } ... else { 代码块n } 

    Multiple if statements

    Note: If the statement is executed if (or else if, or else) is only one condition is met, can be omitted braces! However, if the implementation of a number of statements, the braces is indispensable.

    such as:

    int days = 31;
    if(days > 30)
        System.out.println("本月是大月"); else System.out.println("本月是小月"); 

    nested if statements may be made in the inner layer. Nested if statements, only when the condition is satisfied if the outer layer, the inner layer will determine if the condition.

    grammar:

    if(条件1){
        if(条件2){
            代码块1 } else{ 代码块2 } } else{ 代码块3 } 

    if nesting

    if statement Exercise: Xiao Ming exam, 78 points, 60 points or more passing, more than 80 points as well, more than 90 points for the best, 60 points or less for a re-examination, write source code ScoreJudge.java, Xiao Ming output of the situation.

    Reference code is as follows:

    public class ScoreJudge {
        public static void main(String[] args){
            int score = 78; if(score >= 60){ if(score >= 80){ if(score >= 90){ System.out.println("成绩优秀"); } else{ System.out.println("成绩良好"); } } else{ System.out.println("成绩及格"); } } else{ System.out.println("需要补考"); } } } 

    Note:所有的条件语句都是利用条件表达式的真或假来决定执行路径,Java 里不允许将一个数字作为布尔值使用,虽然这在C和C++是允许的,如果要在布尔测试里使用一个非布尔值,需要先用一个条件表达式将其转换成布尔值,其他控制语句同理。

    Compiler implementation:

    $ javac ScoreJudge.java
    $ Java ScoreJude
    成绩及格

    When you need to determine the equivalent options, use the switch statement is more concise. For example: Yaohao shake to get the first prize, second prize to shake 2, such as to shake the third prize 3, shake to no other prize.

    grammar:

    switch(表达式){
        case 值1:
            代码块1
            break; case 值2: 代码块2 break; ... default: 默认执行的代码块 } 

    The same, started when the value of expression values ​​and case statements from this position after the switch down until the switch statement break statement is encountered or the end of the block; If no matching default case statement code block is executed.

    Create a source code file Draw.java.

    public class Draw {
        public static void main(String[] args){ int num = 2; switch(num){ case 1: System.out.println("恭喜你,获得了一等奖"); break; case 2: System.out.println("恭喜你,获得了二等奖"); break; case 3: System.out.println("恭喜你,获得了三等奖"); break; default: System.out.println("很遗憾,下次再来"); } } } 

    Compile and run:

    $ javac Draw.java
    $ java Draw
    恭喜你,获得了二等奖

    whilegrammar:

    while(条件){
        代码块
    }
    

    It is performed while the first determination process, and then executed.

    1. Judge behind while the condition is satisfied (true or false)
    2. When the condition is established, the code execution cycles, and then repeatedly performed 1., 2.until the loop until the condition is not satisfied

    while flow statement

    do-whilegrammar:

    do{
        代码块
    }while(条件);
    

    Do-while execution is performed first, and then determines (in the cycle so the code will be executed at least once)

    1. To survive a cycle of operation, and then determine whether the conditions established cycle
    2. If the conditions are met, continue 1., 2.until the loop condition does not hold up

    do ... while the process

    Such as:

    int i = 0;
    while(i < 100){
        System.out.println("I love ShiYanlou!"); i++; } 
    int i = 0;
    do {
        System.out.println("I love ShiYanlou!"); i++; } while(i < 100); 

    Exercise: and respectively, while the two methods do-while, to write source code files SumOfEven.java, implemented in even numbers and 1-1000, and outputs. Both methods verify the results of your output is consistent with it?

    Reference code is as follows:

    public class SumOfEven {
        public static void main(String[] args){ int i1 = 1, i2 = 1; int sum1 = 0, sum2 = 0; while (i1 <= 1000){ //循环1000次 if(0 == i1 % 2){ //判断是否为偶数 sum1 += i1; //将偶数加入到总数里 } i1++; //i自增1 } System.out.println("用while,1到1000中,所有偶数的和为:"+sum1); do { if (0 == i2 % 2){ //在条件语句中,将数值写在前面是为了防止将==写成了= sum2 += i2; } i2++; } while(i2 <= 1000); System.out.println("用do-while,1到1000中,所有偶数的和为:"+sum2); } } 

    Compile and run:

    $ javac SumOfEven.java
    $ java SumOfEven
    用while,1到1000中,所有偶数的和为:250500
    用do-while,1到1000中,所有偶数的和为:250500

    forgrammar:

    for(循环变量初始化; 循环条件; 循环变量变化){
        循环操作
    }
    

    for comparison while and do-while sentence structure is more concise and easy to read, it's the order of execution:

    1. The initial state of the execution cycle variable initialization section, the cycle is provided, in this section is performed only once throughout the cycle
    2. Analyzing circulation condition, if the condition is true, the code within the loop body is executed; if false, the loop exits directly
    3. Execution cycle variable change section that changes the value of the loop variable for the next determination condition
    4. Followed by re-execution 2., 3., 4.until it exits the loop

    for the process

    For example, 100 can not be calculated within the number of divisible and 3:

        int sum = 0; // 保存不能被3整除的数之和
        // 循环变量 i 初始值为 1 ,每执行一次对变量加 1,只要小于等于 100 就重复执行循环
        for (int i = 1;i<=100;i++) { // 变量 i 与 3 进行求模(取余),如果不等于 0 ,则表示不能被 3 整除 if (i % 3 != 0) { sum = sum + i; // 累加求和 } } System.out.println("1到100之间不能被3整除的数之和为:" + sum); 

    Exercise: write source code files SumOfOdd.javato achieve 1-1000 and in all even, and output.

    Reference code is as follows:

    public class SumOfOdd {
        public static void main(String[] args){ int sum = 0; for(int i = 1; i <= 1000; i++){ if(0 == i % 2){ sum += i; } } System.out.println("用for,1到1000中,所有偶数和为:"+sum); } } 

    Compile and run:

    $ javac SumOfOdd.java
    $ java SumOfOdd
    用for,1到1000中,所有偶数和为:250500

    breakKeywords often used in conditional and looping statements, to jump out of a loop statement.

    continueKeywords will skip the next iteration loop statement execution remaining. Create a source code file Jump.java.

    public class Jump{
        public static void main(String[] args){ //break 练习 for(int i = 1; i <= 10; i++){ System.out.println("循环第"+i+"次"); if(0 == i % 3){ break; } if(0 == i % 5){ System.out.println("我进来了!"); } } //continue练习 打印10以内的所有奇数 for(int i = 1; i <= 10; i++){ if(0 == i % 2) //判断i是否为偶数 continue; //通过continue结束本次循环 System.out.println(i); } } } 

    Compile and run:

    $ javac Jump.java
    $ java Jump 
    循环第1次
    循环第2次
    循环第3次
    1
    3 5 7 9

Guess you like

Origin www.cnblogs.com/liuba/p/11131613.html