Syntax Basics for Java 3

Source: Internet
Author: User
Tags case statement

Statement

1. Compound statements

When using compound statements, be aware that a compound statement creates a scope for a local variable that is part of the program where a variable is created and can be used. If the variable is used outside the scope of a variable, an error occurs.

Example: Create a compound class in your project and define a compound statement block in the main method that contains another compound statement block.

public class compound{

public static void Main (string[] args) {

{

int y=40;

System.out.println ("Value of output y:" +y);

int z=245;

Boolean B;

{

b=y>z;

SYSTEM.OUT.PRINTLN ("Y>z established:" +b);

}

}

String word = "Hello java";

System.out.println ("Output string:" +word);

}

}

2. Conditional statements

(1) If condition statement

An IF condition statement is an important programming statement that tells the program to execute a program in the case of a condition, and in another case executes another statement.

With the IF condition statement, you can choose whether to execute the statement immediately following the condition. The keyword if is followed by a Boolean expression that is the condition, and if the expression returns true, the statement that follows is executed, and if false, the statement after the IF condition is not executed. The IF condition statement can be divided into simple if statements, if......else statements, and If......else if multi-branch statements.

① Simple IF condition statement

Boolean expression: The necessary argument that indicates that the result it returns is a Boolean value. It can be a simple Boolean variable or constant, or an expression that uses a relationship or Boolean operator.

Statement sequence: Optional parameter. Can be one or more statements that execute when the value of an expression is true. If there is only one statement in the statement sequence, you can omit the curly braces in the conditional statement.

②if......else statements

The If......else statement is the most common form of a conditional statement, and it is processed in a selective way for a condition. It usually shows that "if a certain condition is met, some sort of treatment is done, otherwise the other treatment is done." ”

The value of the expression within the if () after () must be of type Boolean. If the value of the expression is true. Executes the compound statement immediately following the IF statement; if the value of the expression is false, the compound statement after the else is executed

As with simple if conditional statements, if there is only one statement in the statement sequence of the If......else statement (excluding comments), you can omit the curly braces outside the statement sequence. Sometimes there are no statements in the curly braces that follow the else or if for programming purposes.

③if......else if multi-branch statement

The If......else if multi-branch statement is used to handle a variety of situations for an event. It usually manifests as "if a certain condition is met, some sort of treatment is done, otherwise, if another is satisfied, another process is performed".

(2) Switch multi-branch statement

The value of an expression in a switch statement must be an integer or a character type, and a constant value of 1~ constant value n must also be an integer or character type. The switch statement evaluates the expression first, and if the value of the expression is the same as the value of the variable following a case, several statements of the case statement thickness are executed until a break statement is encountered. At this point, if there is no break statement in the case statement, several statements in the following cases will continue until the break statement is encountered. If no value of a constant is the same as the value of the expression, the statement following the default is executed. The default statement is optional. If it does not exist, and the value of the expression in the switch statement is not the same as the constant value of any case, switch does not do any processing.

Note: The constant value of case must be different for the same switch statement

3. Looping statements

While Loop statement Do......while Loop statement for Loop statement

Console input and output

Syntax Basics for Java 3

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.