Process Control in Java (i)

Source: Internet
Author: User

<title>Process Control of the program (i)</title> On the process Control of Java programs (i)

From a structured programming perspective, the program has three structures:

    • Sequential structure
    • Select structure
    • Loop structure
1. Sequential structure

is the program from the top to the next line of execution, the middle no judgment and jump.

2.if Conditional Statement ①
if(条件语句)
{
执行语句块
}

Where the conditional statement can be any kind of logical expression, if the conditional statement returns a result of true, executes the execution statement in the later brace pair, executes the other program code later, and the program skips the execution statement in the brace pair after the conditional statement.
Note: If there is only one statement in the curly braces, you can also omit this pair of curly braces
Such as:

int x=0
if(x==1)
System.out.println("x=1");

Ii

if(条件语句)
{
执行语句块1
}
else
{
执行语句块2
}

This format adds an ELSE clause after the IF clause, on the basis of the single if statement above, and when the conditional statement returns FALSE, the subordinate clause of the later part is executed, such as:

int x=0;
if(x==1)
System.out.println("X=1");
else
System.out.println("X!=1");

If the value of x equals 1 prints "x=1", otherwise "x!=1" will be printed.

For If......else ... Statement, there is a more concise notation:

变量=布尔表达式? 语句1:语句2;

The following code:

if(x>0)
y=x;
else
y=:-x;

Can be simply written as:

y=x>0?x:-x;

if(条件语句1)
{
执行语句块1
}
else if(条件语句2)
{
执行语句块2
}
……
else if(条件语句n)
{
执行语句块n
}
else
{
执行语句块n+1
}

If statements are nested:
When using the if nested statements, it is best to use {} to determine the level of each other's relationships.
Such as:

if(x==1)
{
if(y==1)
System.out.println("x=1,y=1");
else
System.out.println("x=1,y!=1");
}
else if(x !=1)
{
if(y==1)
System.out.println("x!=1,y=1");
else
System.out.println("x!=1,y!=1");
}

Process Control in Java (i)

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.