您的位置:首页 > 编程语言 > Java开发

Java Note - Controlling Execution

2015-06-07 17:24 423 查看
Most procedural programming languages have some kind of control statement, and there is often overlap among languages.

True or False

All conditional statements use the truth or falsehood of a conditional expression to determine the execution path.

Java does not allow you to use a number as a boolean.

Selection

The basic way to control program flow is selection

// select an execute path according to boolean expression
if (boolean-exp)
statement
else
statement

// select from among pieces of code based on the value of an expression
switch (exp)
case (val_1) :
statement_1; break;
case (val_2) :
statement_2; break;
default
statment_default; break;


Iteration

Looping is controlled by while, do-while and for, which are sometimes classified as iteration statements.

A statement repeats until the controlling boolean-expression evaluates to false.

// the most common loop syntax
while (boolean-exp)
statement

// at least execute once
do
statement
while (boolean-exp)

// be used for counting
for (initialization; boolean-exp; step)
statement

// Foreach syntax
for (type t : iterable-object)
statement


Several keywords represent unconditional branching, which simply means that the branch happens without any test.

return - cause the current method to exit and specify what value the method return
break - quit the loop without executing the rest of the statements
continue - stop the execution of the current iteration and begin the next iteration
(label:) - the only place a lable is useful in java is right before an iteration statement


The comma operator

The comma operator has only one use in Java: in the control expression of a for loop

Using the comma operator, you can define multiple variables within a loop statement, but they must be of the same type

for (int i=0, j=0; i < 10; i++, j=i*2)
statement
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: