Decision Making Flashcards
(11 cards)
What is the basic syntax of an if statement?
if (condition) {
// code to be executed if condition is true
}
What is the basic syntax of an if-else statement?
if (condition) {
// code if condition is true
} else {
// code if condition is false
}
What is the basic syntax of a switch statement, including case and break?
switch (expression) {
case value1:
// code block 1
break;
case value2:
// code block 2
break;
default:
// default code block (optional)
}
What is the basic syntax of a while loop?
while (condition) {
// code to be executed repeatedly
}
What is the basic syntax of a do-while loop?
do {
// code to be executed at least once
} while (condition);
What is the basic syntax of a for loop, specifying its three parts?
for (initialization; condition; increment/decrement) {
// code to be executed repeatedly
}
How is the break statement used within a loop?
break; is used to immediately terminate the innermost loop (or switch statement) and transfer control to the statement immediately following it.
How is the continue statement used within a loop?
continue; is used to skip the rest of the current iteration of the loop and proceed to the next iteration.
What is the purpose of if-else and switch statements?
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).
What are the limitations of the switch statement?
Cannot evaluate float or double expressions, cannot compare two variables or use relational operators, each case label must be a constant expression.
Explain the difference between while and do-while loops.
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.