Control Flow Statements Flashcards
1
Q
Type Casting
A
- when you convert a value from one primitive data type to another type
2
Q
Widening Casting
A
- converts a smaller type to a larger size type
- done automatically
3
Q
Narrowing Casting
A
- converts a larger type to a smaller size type
- it can be done manually by placing the type in parenthesis in front of the value
code example
double myDouble = 9.78d;
int myInt = (int) myDouble;
4
Q
What is the four key control flow statements in Java?
A
- switch
- for
- while
- do-while
5
Q
switch statement
A
- used to select one of many code blocks to be executed.
- syntax:
switch (expression) { case x: // code block break; case y: // code block break; default: // code block break; }
6
Q
for statement
A
syntax:
for (init; condition; increment) { // statements }
7
Q
while statement
A
syntax:
while (condition) { // statements }
8
Q
do-while statement
A
do { // statements } while (condition);
9
Q
continue
A
- continue with a loop (bypass all other code in the block/body)
10
Q
continue
A
- when it is used within a loop, the loop bypasses all other code in the block/body
11
Q
break
A
- it can be used to exits the loop
12
Q
What are the difference between while and do-while statements?
A
- while loop checks the condition at the start before executing the block
- do-while loop executes the code block at least once and then the condition is checked