Decision Making Flashcards

(11 cards)

1
Q

What is the basic syntax of an if statement?

A

if (condition) {
// code to be executed if condition is true
}

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

What is the basic syntax of an if-else statement?

A

if (condition) {
// code if condition is true
} else {
// code if condition is false
}

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

What is the basic syntax of a switch statement, including case and break?

A

switch (expression) {
case value1:
// code block 1
break;
case value2:
// code block 2
break;
default:
// default code block (optional)
}

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

What is the basic syntax of a while loop?

A

while (condition) {
// code to be executed repeatedly
}

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

What is the basic syntax of a do-while loop?

A

do {
// code to be executed at least once
} while (condition);

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

What is the basic syntax of a for loop, specifying its three parts?

A

for (initialization; condition; increment/decrement) {
// code to be executed repeatedly
}

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

How is the break statement used within a loop?

A

break; is used to immediately terminate the innermost loop (or switch statement) and transfer control to the statement immediately following it.

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

How is the continue statement used within a loop?

A

continue; is used to skip the rest of the current iteration of the loop and proceed to the next iteration.

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

What is the purpose of if-else and switch statements?

A

if-else: Executes one block of code if a condition is true, and another if it’s false.

switch: Allows a variable to be tested for equality against a list of constant values (cases).

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

What are the limitations of the switch statement?

A

Cannot evaluate float or double expressions, cannot compare two variables or use relational operators, each case label must be a constant expression.

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

Explain the difference between while and do-while loops.

A

while: Condition is checked before each iteration. The loop may not execute at all if the condition is initially false.

do-while: Executes the loop body at least once, then the condition is checked. The loop continues as long as the condition is true.

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