Java Note - Controlling Execution

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值