EXPRESSIONS, STATEMENTS, BLOCKS AND CONTROL FLOW STATEMENTS Flashcards

1
Q

What is an expression and give examples?

A

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”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are statements?

A

Statements are roughly equivalent to sentences in
natural languages. A statement forms a complete unit
of execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are expression statements?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a block?

A

A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the different types of control flow statements?

A
  • decision-making statements (if-then, if-then-else, switch)
  • the looping statements (for, while, do-while) and
  • the branching statements (break, continue, return)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the two types of break statements and when are they used?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the function of the continue statement?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the function and different forms of the return statement?

A

• 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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly