Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the two control statements of java?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Java's two control statements are what, for this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem to find a simpler and easier way.

We'll briefly introduce two control statements here so that you can use them in your programs, and they will help illustrate an important feature of Java: program blocks.

1 if control statement

If control statements in Java are very similar to IF statements in other languages. And it has exactly the same syntax as the if statement in C/ C++. In its simplest form:

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. Here is an example:

if(num

< 100) println("num is less than 100"); 在这个例子中,如果变量num 的值小于100 ,那么条件表达式的值为真,方法println ( ) 将被调用执行。如果变量num 的值大于或等于100,那么方法println ( ) 被绕过而不被执行。在第4章,中你将看到Java 在条件语句中用到的所有的关系运算符,下面是其中一部分: 运算符含义 < 小于>

Greater than == equal to

Note that the relational operators that determine equality are the 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 procedure produces the following results:

x is less than y

x now equal to y

x now greater than y

Another caveat in this program is: int x, y ;

The program line uses commas to separate the variable list, defining two variables x and y.

2 for loop

As you probably know from previous programming experience, loop statements are an important part of almost all programming languages. Java is no exception. In fact, as you will see in Chapter 5, Java provides a powerful loop structure. For loops are perhaps the most versatile. If you're familiar with C or C++, you should be happy because Java's for loop is exactly the same as for loops in other languages. If you are unfamiliar 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 loop variables and assigns initial values to variables. The condition is a Boolean expression that tests the loop control variable. If the result of the test is true, the statement continues to execute repeatedly; if the result of the test 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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report