Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

A brief survey of control statements.

Numeric promotions | Objects and references | The class String | Useful examples | Defining attributes of objects | Defining operations on objects (methods) | Defining methods of object creation (constructors) | Static members | Packages and imports | Scope of an identifier. Local variables. Access control. |


Читайте также:
  1. A (Statistical Process Control (SPC), moderate)
  2. A Brief History of Sri Lanka.
  3. A BRIEF HISTORY OF STRING THEORY
  4. A brief history of the United States
  5. A BRIEF OUTLINE OF THE DEVELOPMENT OF THE ENGLISH LITERARY (STANDARD) LANGUAGE
  6. A good thesis sentences will control the entire argument.

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


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.


Дата добавления: 2015-11-16; просмотров: 164 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Structure of a program. Running an application.| Comparison operators and expressions

mybiblioteka.su - 2015-2024 год. (0.008 сек.)