2 Conditional statements, loops and flow control Flashcards
Are braces required around the block after an if statement?
No but only the first line will be executed if there are no braces.
//executes both lines if(hourOfDay < 11) { System.out.println("Good Morning"); morningGreetingCount++; }
//only executes first line
if(hourOfDay < 11)
System.out.println(“Good Morning”);
morningGreetingCount++;
What is the ternary operator?
The conditional operator, ? :, otherwise known as the ternary operator, is the only operator
that takes three operands and is of the form:
booleanExpression ? expression1 : expression2
How is the ternary expression written?
booleanExpression ? expression1 : expression2
int y = 10; final int x; if(y > 5) { x = 2 * y; } else { x = 3 * y; } Compare the previous code snippet with the following equivalent ternary operator code snippet: int y = 10; int x = (y > 5) ? (2 * y) : (3 * y);
Are parenthesis required on the expressions in the ternary operator statement?
no
What is a switch statement?
A switch statement, as shown in Figure 2.4, is a complex decision-making structure
in which a single value is evaluated and fl ow is redirected to the fi rst matching branch,
known as a case statement. If no such case statement is found that matches the value, an
optional default statement will be called. If no such default option is available, the entire
switch statement will be skipped.
Which data types are allowed in switch statements?
■ int and Integer ■ byte and Byte ■ short and Short ■ char and Character ■ int and Integer ■ String ■ enum values
Which data types are NOT supported by switch statements?
boolean long, float, double and
their associated wrapper classes,
Explain compile time constant values
The values in each case statement must be compile-time constant values of the same data
type as the switch value. This means you can use only literals, enum constants, or final constant variables of the same data type. By final constant, we mean that the variable must be marked with the final modifier and initialized with a literal value in the same
expression in which it is declared.
public static void main(String args[]){
final int a=1; int b=2; int c=3; switch(c){ case a: System.out.println("A"); // Ok, its final case b: System.out.println("B"); //will not compile; case 3: System.out.println("C"); // ok, literal }
Write the logical truth tables for &, |, and ^
Here are some tips to help remember this table:
■ AND is only true if both operands are true.
■ Inclusive OR is only false if both operands are false.
■ Exclusive OR is only true if the operands are different.
In case statements, when does default get executed?
- If there is no other match found.
- If the statement above gets executed and there is no break statement it will flow down into the default.
What are the rules for the order of case statements?
- case statements can be in any order
- default statement can be in any order
What happens if a break statement is not added to a case code block?
if you leave out the break statement,
flow will continue to the next proceeding case or default block automatically.
When is the default block executed in a switch statement?
Even though the default block was before the case block, only the case block was executed.
If you recall the defi nition of the default block, it is only branched to if there is no
matching case value for the switch statement, regardless of its position within the switch
statement
What is the syntax for the for each loop?
for(datatype instance : collection) { //body }
What is a for each statement?
Starting with Java 5.0, Java developers have had a new type of enhanced for loop at their
disposal, one specifically designed for iterating over arrays and Collection objects. This
enhanced for loop, which for clarity we’ll refer to as a for-each loop