EXPRESSIONS, STATEMENTS, BLOCKS AND CONTROL FLOW STATEMENTS Flashcards
What is an expression and give examples?
A construct made up of variables, operators, and
method invocations, constructed according to the syntax of the language, that evaluates to a single value.
• Examples of expressions:
int cadence = 0;
anArray[0] = 100;
System.out.println(“Element 1 at index 0: “ + anArray[0]);
int result = 1 + 2; if (value1 == value2)
System.out.println(“value1 == value2”);
What are statements?
Statements are roughly equivalent to sentences in
natural languages. A statement forms a complete unit
of execution.
What are expression statements?
Expressions that have been made into statements by terminating the expressions with a ; They include: • Assignment expressions • Any use of ++ or -- • Method invocations • Object creation expressions *Also for statements
What is a block?
A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.
What are the different types of control flow statements?
- decision-making statements (if-then, if-then-else, switch)
- the looping statements (for, while, do-while) and
- the branching statements (break, continue, return)
What are the two types of break statements and when are they used?
Labeled and Unlabeled
• The unlabeled form can be used in a switch statement. It can also be used to terminate a for,
while, or do-while loop.
• The labeled break has the given expression or
statement that it terminates alongside it.
What is the function of the continue statement?
The continue statement skips the current iteration of a
for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop’s body and evaluates the boolean expression that controls the loop.
What is the function and different forms of the return statement?
• The return statement exits from the current method, and control flow returns to where the method was invoked.
• The return statement has two forms: one that returns a value, and one that doesn’t.
• To return a value, simply put the value (or an expression that calculates the value) after the return keyword. E.g. return ++count;
NB:
• The data type of the returned value must match the type of the method’s declared return value.
• When a method is declared void, use the form of return that doesn’t return a value: return;