Two java Control statements

Source: Internet
Author: User
Java Control statements-general Linux technology-Linux programming and kernel information. For details, see the following. Although the control statements will be carefully discussed in chapter 5th, here we will briefly introduce two control statements so that they can be used in the example programs in Chapter 3rd and Chapter 4th, they will also help illustrate an important feature of Java: program blocks.

2.4.1 if control statement
In Java, if control statements are very similar to IF statements in other languages. In addition, it has the same syntax as the if statement in C/C ++. Its simplest form is as follows:

If (condition) statement;

Here, condition is a Boolean expression. If the condition is true, the statement is executed. If the condition is false, the statement is bypassed and not executed. The following is an example:

If (num <100) println ("num is less than 100 ");

In this example, if the value of the variable num is less than 100, the value of the conditional expression is true and the method println () is called for execution. If the value of the variable num is greater than or equal to 100, the method println () is bypassed and not executed. In Chapter 4th, you will see all the Relational operators used in the condition statements in Java. The following are part of them:

Operator meaning

<Less> greater than = equal

Note that the relational operator used to determine whether to be equal is two equal signs "= ". The following program illustrates the use of the if control statement:

/* Demonstrate the if.

Call this file "IfSample. java". */class IfSample {

Public static void main (String args []) {
Int x, y;

X = 10;

Y = 20;

If (x <y) System. out. println ("x is less than y ");

X = x * 2;
If (x = y) System. out. println ("x now equal to y ");

X = x * 2;

If (x> y) System. out. println ("x now greater than y ");

// This won't display anything

If (x = y) System. out. println ("you won't see this ");}}

The results of this program are as follows:

X is less than y
X now equal to y
X now greater than y

In this program, another thing to note is: int x, y;

The program line uses commas to separate the Variable list and defines two variables x and y.

2.4.2 for Loop
You may know from your previous programming experience that loop statements are an important part of almost all programming languages. Java is no exception. In fact, you will see in Chapter 5th that Java provides a powerful loop structure. A For Loop may be the most common. If you are familiar with C or C ++, you should be happy because Java's for loop operations are exactly the same as those in other languages. If you are not familiar with C/C ++, The for loop is also easy to use. The simplest for loop structure is as follows:

For (initialization; condition; iteration) statement;

In this most common form, the initialization part of the loop body sets the loop variable and assigns the initial value to the variable. Condition is a Boolean expression used to test the cyclic control variable. If the test result is true, the statement continues to be executed repeatedly. If the test result is false, the loop ends. The iteration expression determines how the loop control variable changes after each loop. The following short program illustrates how to use the for Loop:

/*
Demonstrate the for loop.

Call this file "ForTest. java ".

*/

Class ForTest {

Public static void main (String args []) {
Int x;

For (x = 0; x <10; x = x + 1)
System. out. println ("This is x:" + x );
}
}

The result of this program is as follows:

This is x: 0
This is x: 1
This is x: 2
This is x: 3
This is x: 4
This is x: 5
This is x: 6
This is x: 7
This is x: 8
This is x: 9

In this example, x is a loop control variable. It is initialized to zero in the initialization part of. Execute the condition test x <10 at the beginning of each repeated iteration (including the first iteration. If the test result is true, the println () Statement is executed and the iteration part of the loop body is executed. This process continues until the results of the conditional test are false.

Interestingly, the iterative part of the loop body is generally not the same as the previous program example in a program written by a Java professional programmer. That is, you will rarely see the following statement:

X = x + 1;

The reason is that Java has a special incremental operator that can perform this operation more efficiently. The incremental operator is "++" (that is, two plus signs ). The incremental operator adds 1 to the target object each time. By using the incremental operator, the preceding statement can be written as follows:

X ++;

In this way, the preceding for loop statement is generally written as follows:

For (x = 0; x <10; x ++)

You can rewrite the for loop statement of the previous program to give it a try. You will see that the running structure is the same as before. Java also provides a decimal OPERATOR: "--" (that is, two minus signs ). The subtraction operator reduces the number of objects to 1 each time.
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.