Conditional and Loop Statement Flashcards
(13 cards)
What is the purpose of conditional statements in programming?
Conditional statements allow a program to make decisions based on specific conditions. They execute code blocks depending on whether a condition is true or false.
How do logical operators work with conditional statements?
Logical operators (&& for AND, || for OR) combine multiple conditions. These operators exhibit “short-circuiting” behavior, where the second operand is evaluated only if necessary.
What is the syntax of an if statement?
if (condition) {
// code to execute if condition is true
}
If the condition is true, the code block inside the if statement is executed. If false, the block is skipped.
What is the difference between if
and else
statements?
The if statement executes code if the condition is true. The else statement specifies a block of code to be executed if the if condition is false.
How does a switch statement work?
A switch statement evaluates an expression against multiple cases. It executes the block of code associated with the matching case and ends with a break
statement to prevent “fall-through.”
What is the difference between traditional and switch expression in Java?
Traditional switch
requires break
statements to avoid fall-through, whereas switch
expressions (with ->) map cases to values and return results without needing break
statements.
How does the ternary operator work?
Answer: The ternary operator is a shortcut for an if-then-else statement. It evaluates a condition and returns a value based on whether the condition is true or false:condition ? value_if_true : value_if_false;
What is the use of a loop in programming?
A loop repeatedly executes a block of code as long as a specified condition is true. It allows for repetitive tasks without manually writing the code multiple times.
How does a for loop work in Java?
A for
loop iterates a fixed number of times, typically with a counter variable that is incremented in each iteration. It is useful when the number of iterations is known in advance:for (int i = 0; i < n; i++) {
// code to execute
}
How does a while loop work?
A while
loop repeatedly executes a block of code as long as the given condition is true. The condition is checked before each iteration, and if false, the loop will not execute.
How does a do-while loop differ from a while loop?
A do-while
loop guarantees at least one execution of the loop body because the condition is checked after the first iteration. In contrast, a while
loop checks the condition before executing the code block.
What is the purpose of the break statement in loops?
The break
statement exits the loop prematurely when a certain condition is met. It is often used in search operations to stop once the desired value is found.
What is the purpose of the continue statement in loops?
The continue
statement skips the current iteration of the loop and proceeds with the next iteration. It is useful for filtering out certain values or conditions.