Java Basic Syntax---process control

Source: Internet
Author: User
Tags case statement terminates

Java Basic Syntax---process control 0. Overview

Three major flow control statements: Order, selection, loop.

Select structure:

    • If structure, if-else structure;
    • multiple IF-ELSE statements;
    • nested IF-ELSE statements;
    • Switch structure;

Loop structure:

    • While loop, Do-while Loop, for loop;
    • Java Enhanced for Loop
    • Loop nesting
1. Select Structure 1.1 If structure

An If statement contains a Boolean expression and one or more execution statements;

The Boolean expression value is true to execute an if statement;

Format:

if(布尔表达式){    //布尔表达式值为true,执行语句;}
1.2 If-else structure

The Boolean expression value is true to execute an if statement;

The Boolean expression value is false and the Else statement is executed;

Format:

if(布尔表达式){    //布尔表达式值为true,执行语句;}else{    //布尔表达式值为false,执行语句;}
1.3 Multi-If-else structure

Format:

if(布尔表达式1) {    //布尔表达式1值为true,执行语句;}else if(布尔表达式2){    //布尔表达式2值为true,执行语句;}else if(布尔表达式3){    //布尔表达式值3为true,执行语句;}else{    //如果以上所有表达式的值都为false,则执行语句;}
1.4 If nesting structure

Format:

if(布尔表达式1){    //布尔表达式1值为true,执行语句    if(布尔表达式2)    {        //布尔表达式2值为true,执行语句    }}
1.5 Switch structure

Format:

switch(常量值/expression){    case value1:    //执行语句    break;  //可选        case value2:    //执行语句    break;  //可选        ......    default :    //执行语句}

The switch statement has the following rules:

    • The variable type in a switch statement can be only a byte, short, int, or char.
    • A switch statement can have more than one case statement. Each case is followed by a value and a colon to compare.
    • The data type of the value in the case statement must be the same as the data type of the variable, and can only be constants or literal constants.
    • When the value of a variable is equal to the value of the case statement, the statement after the case statement starts executing until the break statement appears and jumps out of the switch statement.
    • The switch statement terminates when a break statement is encountered. The program jumps to the statement following the switch statement execution. The case statement does not have to contain a break statement. If no break statement appears, the program continues to execute the next case statement until a break statement appears.
    • A switch statement can contain a default branch, which must be the last branch of a switch statement. Default is executed when there is no case statement with the value equal to the value of the variable. The default branch does not require a break statement.
1.6 If and switch differences

If structure

    • To judge a condition as a Boolean type (Boolean expression)
    • Judging condition is a range

Switch structure

    • Judging condition as constant value
1.7 Case of the Output 99 multiplication table
public class Multiplicationtable {public static void main (string[] args) {for (int i = 1; I <= 9;                    ++i) {for (int j = 1; J <= 9; j + +) {if (J < i) {                The output space is determined by "%d *%d =%2d" System.out.print ("");                } else {System.out.printf ("%d *%d =%2d", I, J, I*j);        }} System.out.println ();            }}}output:1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9  2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 5 * 5 = 25 5 * 6 = 30 5 *7 = 35 5 * 8 = 40 5 * 9 = 45 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9                                                                              = 54 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63                                                                                         8 * 8 = 64 8 * 9 = 72 9 * 9 = 81
1.8 Case 2--Output How many days a certain day of a year
public class Days {public static void main (string[] args) {int days = 0;        Scanner sc = new Scanner (system.in);        System.out.println ("Please enter the year to be determined:");        int year = Sc.nextint ();        System.out.println ("Please enter the month to be determined:");        int month = Sc.nextint ();            Switch (month) {case 1:case 3:case 5:case 7:case 8:                Case 10:case 12:days = 31;            Break                Case 4:case 6:case 9:case 11:days = 30;            Break Case 2:if (%400 = = 0) | |                (Year%! = 0 && Year%4 = = 0))                    {days = 29;                Break                    } else {days = 28;                Break } default:System.out.println ("The month you entered is incorrect!)                "); SyStem.exit (0);    } System.out.printf ("%4d%2d month total%2d days \ n", year,month,days); }}output: Please enter the year you want to determine: 2018 Enter the month you want to determine: 022018 years February Total 28 days Please enter the year you want to determine: 2008 Enter the month you want to determine: 022008 years, February, 29 days.
2. Loop Structure 2.1 whileCycle

As long as the Boolean expression value is true , the loop content is executed. Until the Boolean expression value is false , exit the loop;

while(布尔表达式){    //布尔表达式值为true,执行循环内容}
2.2 do ... whileCycle

As long as the Boolean expression value is true , the loop content is executed. Until the Boolean expression value is false , exit the loop, and while Similarly, the do...while statement will be executed at least once;

do{    //布尔表达式值为true,执行循环内容}while(布尔表达式)
2.3 forCycle

forThe number of cycle executions is determined before execution.

for(初始化;布尔表达式;更新){    //执行代码}

A for few notes about:

    • The initialization step is performed first. You can declare a type, but you can initialize one or more loop control variables, or you can be an empty statement.
    • Then, the value of the Boolean expression is detected. If true, the loop body is executed. If False, the loop terminates, starting execution of the statement following the loop body.
    • After the loop is executed, the loop control variable is updated.
    • Re-detects the Boolean expression. The loop executes the above procedure.
2.4 Java Enhanced for Loop

declaration statement: declares a new local variable that must have a type that matches the type of the array element. Its scope is scoped to the Loop statement block, whose value is equal to the value of the array element at this time.

expression: The expression is the name of the array to access, or is the method that returns the value to the group.

for(声明语句:表达式){    //执行代码}
    • ?
2.5 case in loop output 26 English lowercase letters requiring two lines of output
public class OutputLetters {    public static void main(String[] args) {        //循环输出26个英文小写字母,要求分两行输出        char ch = 'a';        int count = 0;//控制换行        while(ch <= 'z')        {            System.out.printf(ch + " ");            ch ++;            count ++;            if (count % 13 == 0)            {                System.out.println();            }        }        System.out.println();        ch = 'a';        count = 0;        for(count = 1, ch = 'a';ch <= 'z';ch ++,count ++)        {            System.out.printf(ch + " ");            if (count % 13 == 0)            {                System.out.println();            }        }    
2.6 break

breakStatement

    • breakStatement can end the execution of the current loop;
    • After executing the statement break , the statement in the loop body that is break behind the statement will not be executed;
    • In multi-loop nesting, the break statement is equivalent to jumping out of a layer;
2.7 continue

continueStatement:

    • continueStatements can only be used in loops;
    • continueStatement to end the execution of the current loop, but to continue the execution of the next loop;
  public class Outputlettersdemo {public static void main (string[] args) {//Loop output 26 English lowercase        Female, requires two lines of output//practice break,cotinue char ch = ' a ';            int count = 0;//controls the newline while (ch <= ' Z ') {if (ch = = ' x ') breaks;            System.out.printf (ch + "");            CH + +;            Count + +;            if (count% = = 0) {System.out.println ();        }} System.out.println ();        ch = ' a ';        Count = 0;            for (count = 1, ch = ' a '; Ch <= ' z '; ch ++,count + +) {if (ch = = ' x ') continue;            System.out.printf (ch + "");            if (count% = = 0) {System.out.println (); }}}}output:a b c d E F g h i j k l m n o p q R S t u v w a b c d e F g h i j k l m n o p q R S t u v w y z  

As can be seen from the above example, the break statement exits directly when the layer loop (to x exits directly, no longer outputs), while the continue statement simply ends the current loop and does not exit (just no output x).

Java Basic Syntax---process control

Related Article

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.