Day Book Java Basics Learning Notes

Source: Internet
Author: User
Tags case statement

Ternary operators

1. Format: (conditional expression)? Expression 1: Expression 2;

2. Arithmetic rule: Evaluates the conditional expression first, obtains a logical value, if true, executes the expression 1, and if the value is false, the expression 2 is executed. 44

3. Typical usage: String str = (5<3)? "5 is greater than 3": "5 is greater than 3"; System.out.println (str);

Program execution classification

1. Sequential structure: The program executes from top to bottom, without any judgment jumps in the middle

2. Branching structure: According to the conditions to choose to execute a piece of code, there are if......else and switch two conditional statements

3. Circular statement: According to the condition Loop executes a piece of code, has whle,do......whle,for three kinds of condition statement

If branch statement

Three forms of the IF statement:

    1. if (true) {Execute code block}
    2. if (conditional expression) {Execute code block}else{execute code block}
    3. if (conditional expression) {Execute code block}else if{Execute code block}else{execute code block}
If {} after if or else is omitted, then the IF condition can only control the first semicolon thereafter. In the third way, the statements in the else if () {} are intended to be executed and must meet two conditions:
第一它是已经排除了上一个else if的条件,或者是if的条件;这是隐含的条件第二它必须满足本else if 的条件才能执行,这是显式条件。
If else has a basic principle, always prioritize the small range of conditions in front of the processing. How to get keyboard input to rewind a packet
import java.util.Scanner;class Test{    public static void main(String[] args){        Scanner s = new Scanner(System.in);        System.out.println("input");        int a = s.nextInt();    }}
Switch Statement Summary:
当某些case没有break语句的时候,程序从条件表达式中的值和第一个匹配到的case语句开始执行,一直执行到遇到break或者最后。case相当于一个switch的执行入口。break想当于switch的出口,如果一直没有出口的话,一直执行到switch结束
Switch (variable) {
case 值1:case 值2:case 值3:default:

}

    • 1. According to the value of the variable, select the appropriate case to determine, once the case condition is satisfied, execute the corresponding statement of case. If there is no break or has reached the end, it will continue to execute the case statement under it.

    • 2.default: is optional, and the position is flexible.

    • 3. What types of variables can be? char byte short int enumeration String (jdk1.7)

    • 4.case conditions: Where the condition can only be a value, not a range of values!

Loop structure

Loop statement function: In the case of certain conditions, repeated execution of specific code functions, the need to change the condition of the loop at some specific time to false to end the loop, otherwise it will be a dead loop

Four components of a looping statement
    • Initialization section (init_statement) to initialize some variables

    • The Loop Condition section (TEST_EXP) condition is true to perform a loop, and the condition is false to not execute the loop

    • Statement executed when the Loop body part (body_statement) condition is true

    • Iteration part (alter_statement) variable whose value needs to be changed

Circular statement classification
    • For loop

    • While loop

    • Do/while Cycle

For loop
``for(int i = 0;i < 10 ;i++){System.out.println(i);

}

    • int i = 0; for initialization part
    • I < 10; for the Cycle condition section
    • System.out.println (i); for the Loop body part
    • i++; for the iteration part
While loop
`[初始化语句]while( 布尔值测试表达式){语句或语句块;     //循环执行语句[更改语句;]         //控制while何时结束}1          初始条件while(2){  循环条件3          循环体4          迭代条件
difference between for loop and while loop

If you want to access a loop-controlled variable outside of the loop body, you need to use a while loop because the variable definition of the while loop is outside the while loop body, and the For loop is used when you do not need to access the loop control variable outside of the loop.

Do-while Loop Statements
[初始化语句]do{    语句或语句块;    [更改语句;]}while(布尔值测试表达式);1        初始化语句do{    2    循环体    3    迭代条件}while(4);   循环条件

Note To add a semicolon after the while expression

The difference between do-while and while

Do-while statements are executed at least once

Day Book Java Basics Learning Notes

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.