Conditional and Loop Statement Flashcards

(13 cards)

1
Q

What is the purpose of conditional statements in programming?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do logical operators work with conditional statements?

A

Logical operators (&& for AND, || for OR) combine multiple conditions. These operators exhibit “short-circuiting” behavior, where the second operand is evaluated only if necessary.

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

What is the syntax of an if statement?

A

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.

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

What is the difference between if and else statements?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does a switch statement work?

A

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.”

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

What is the difference between traditional and switch expression in Java?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does the ternary operator work?

A

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;

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

What is the use of a loop in programming?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does a for loop work in Java?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does a while loop work?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does a do-while loop differ from a while loop?

A

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.

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

What is the purpose of the break statement in loops?

A

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.

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

What is the purpose of the continue statement in loops?

A

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.

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