Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lecture 1.docx
Скачиваний:
1
Добавлен:
01.07.2025
Размер:
704 Кб
Скачать
☆

9.1. A brief survey of control statements.

The following table depicts all Java instructions:

Type of statement

Statement

Sample

The empty statement

semicolon

;

Expression statements Note: an expression statement is one of the nearby statements terminated with semicolon

assignment;

a=b;

preincrement;

++a;

postincrement;

a++;

predecrement;

--a;

postdecrement;

a--;

method invocation;

x.met(); met();

class instance creation;

new Para();

Block note: after the closing brace semicolon is not present

sequence of instructions and declarations put between braces

{ a=b; c=b; }

Labeled statements

label: arbitrary instruction

Specific syntactic forms and samples are presented below

Control statements

if

if .. else

switch

while

do ... while

break

continue

return

cf. lecture 7

The throw statement

cf. the lecture concerning exceptions

The synchronized statement

Support for concurrent programming (II semester)

No other syntactic structure is a statement in Java.

Thus

  • a declaration is not an instruction, although it is terminated with a semicolon (a declaration is not executed; however if a declaration has an initializer then its initializing statement is executed as an ordinary instruction)

  • the expression a+b terminated with a semicolon (similarly to other expression statements not included in the above list) - is not an instruction (it generates a compilation error: if a compiler prints the message "not a statement" - it means that some instruction in the program is incorrectly formulated)

Control statements are special type of instructions.

Control statements influence the sequence of execution of a program.

A short survey of control statements is depicted in the table. Detailed discussion follows.

Control statements

if ( exp ) ins

If the expression exp is true, then the instruction ins is executed.

if ( exp ) ins1 else ins2

If the expression exp is true, then the instruction ins1 is executed; in other case the instruction ins2 is executed.

switch ( exp ) { case cs1 : ins1 [ break;] case cs2 : ins2 [ break;] .... case csN : insN [ break;] default : insDef [ break;] }

The switch statement transfers control to one of several statements depending on the value of an expression.

The type of the expression exp must be char, byte, short, or int. Its value is compared with the constant expressions (literals or constants) cs1 .. csN. If one of the case constants is equal to the value of the expression, then we say that the case matches, and all statements after the matching case label in the switch block, if any, are executed in sequence. If no case matches but there is a default label, then all statements after the matching default label in the switch block, if any, are executed in sequence.

while ( exp ) ins

The instruction ins is repeatedly executed until the value of the expression exp is true.

do ins while ( exp );

First the instruction ins is executed, then the expression exp is evaluated. If it is true - the execution of the loop continues from the beginning.

for ( init ; exp ; upd ) ins

First the initialization init is executed (it may be a declaration of a variable or evaluation of a list of expressions separated with commas). Then the following are executed repeatedly:

  1. the expression exp is evaluated and

  2. if it is true, the instruction ins is executed and

  3. list of expression statements upd is evaluated

All the elements init, exp, upd are optional.

continue [ label ];

This instruction immediately ends the current iteration of the loop and begins a new one. If a label is present, the instruction attempts to transfer control to the enclosing labeled statement (with the same label).

break [ label ];

A break statement transfers control out of an enclosing statement. It is used in iteration loops and in the switch statement.

return exp ;

Completes execution of a method returning the value of the expression exp (which may be skipped if the method does not return any value).

Instructions (and declarations of variables) may be grouped in blocks by surrounding a sequence of instructions with braces: { and }. It is a so called block statement and it is treated as a single instruction in a program. For example, we can group instructions which are to be executed if some condition holds: if (a > b) { c = a + b; d = a - b; e = a*b; } Note that there is no semicolon after the closing brace.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]