"Java Base-java control statement"

Source: Internet
Author: User

Knowledge Points: 1. Conditional control Statements 2. Loop control Statement 3. Jump statement

The film is from the "realization of the building", and there is no advertising meaning. In this narrative a mouth, afraid of causing copyright and other unnecessary disputes.

I. Conditional CONTROL STATEMENT 1.1 if statement

Grammar:

if (condition) {    code executed when condition is set}

The If...else statement executes the code block of the If section when the condition is true, or to the Else section when the condition is not true. For example, if the one-month number of days is greater than 30 days, then Otsuki, or Yue.

Grammar:

if (condition) {    code block 1}else{    code block 2}

The multiple if statement, in which condition 1 is not satisfied, will be judged by condition 2, and then the code in the Else block is eventually executed when none of the preceding conditions are true.

Grammar:

if (condition 1) {    code block 1}else if (condition 2) {    code block 2}...else {    code block n}

Note: If the IF (or else if, or else) condition is the only one that executes the statement, you can omit the curly braces! However, if there are multiple execution statements, then curly braces are indispensable.

Like what:

int days = 31;if (Days >)    System.out.println ("This month is Otsuki"); else    System.out.println ("This month is Xiao Yue");

Next we do a simple exercise: Xiao Ming exam 78 points, 60 points above, 80 points for good, 90 points above for excellent, 60 points below to re-test, write a program, output Xiao Ming's situation bar.

The reference code is as follows:

public class Scorejudge {public    static void Main (string[] args) {        int score =;        if (score >=) {            if (score >=) {                if (score >=) {                    System.out.println ("excellent score");                }                else{                    System.out.println ("good grades");                }            }            else{                System.out.println ("Pass the Grade");}        }        else{            System.out.println ("need to Retake");}}}    

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

Second, circular control statements

The loop has three, while,do{}while, for Loop

2.1while

While syntax:

while (condition) {    code block}

While the execution process is first judged, then executed.

    1. Determines whether the condition after the while is true or false

    2. When the condition is set, the code within the loop is executed and then repeated, 1. 2. until the loop condition is not set to

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

2.2 Do-while

Grammar:

do{    code block}while (condition);

The execution of the Do-while is performed first and then judged (so that the code within the loop executes at least once)

    1. Perform a loop operation first and then determine if the loop condition is true

    2. If the condition is true, continue execution 1. 2. until the loop condition is not established

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

2.3 For Loop

Grammar:

for (loop variable initialization; loop condition; cyclic variable change) {    loop action}

For a more concise and readable structure than while and do-while statements, the order in which they are executed:

    1. Executes the loop variable initialization section, setting the initial state of the loop, which executes only once in the entire loop

    2. The loop condition is judged, the loop body code is executed if the condition is true, and if False, the loop is exited directly

    3. Execute loop variable change part, change the value of the loop variable, in order to make the next condition judgment

    4. Re-execute, in turn, 2. 3. 4. until the loop is exited

For example, calculate the sum of the numbers within 100 that cannot be divisible by 3:

int sum = 0; Save cannot be divisible by 3 sum    //loop variable i initial value of 1, each execution of the variable plus 1, as long as less than or equal to 100 repeats the loop for    (int i = 1;i<=100;i++) {    //variable I and 3 in The line modulo (take remainder), if not equal to 0, it is not divisible by 3 if        (i% 3! = 0) {             sum = sum + i;//cumulative Sum        }    }    System.out.println ("1 to 100 The sum of the numbers that cannot be divisible by 3: "+ sums";

When speaking conditional control statements, to the students to explain the nested if statement, in the Loop statement, three kinds of circular statements can be nested themselves, can also be nested, the most common is the double cycle. In a double loop, the outer loop executes once, and the inner loop executes a circle.

For example, I want to print

*

**

***

****

Such a graphic

The outer loop controls the number of rows        for (int i = 1; i<=4; i++) {            //inner loop controls the value of the * number            //inner loop variable for each line and the values of the outer loop variable equal for            (int j = 1; j< =i; J + +) {                System.out.print ("*");            }            NewLine System.out.println () after each line is printed            ;        }

Three, jump statement

When we explained the switch statement above, we saw the break keyword, what's the use of it? Students, we will remove the code in the above switch exercises, break run a bit, found what problem? breakthe meaning of jumping out, often used in the condition and loop statements, to jump out of the Loop statement .

For example:

for (int i = 1; i <=; i++) {        System.out.println ("loop" +i+ "th");    if (0 = = i% 3) {break        ;    }    if (0 = = i% 5) {        System.out.println ("I'm coming in!") ");    }}

Operation Result:

In the above code, the for statement will cause the code in the loop to loop 10 times, but when it i=3 enters the first conditional statement, it encounters break , ends the loop, and never gets into the second conditional statement.

In the jump statement, there is also a similar continue break , its role is to skip the loop in the body of the remaining statements to perform the next loop.

For example, we want to print all the odd numbers within 10:

for (int i = 1; i <=; i++) {    if (0 = = i% 2)//Determine if I is even        continue;  End this cycle by continue    System.out.println (i);}

Operation Result:

3.1 Other

In a looping statement, Java SE5 introduces a new, more concise for syntax for arrays and containers, that is foreach语法 . We will meet in the following chapters.

gotoProgram control that originated in assembly language, although it is still a reserved word in Java, it is not used in the language.

"Java Base-java control statement"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.