What is a conditional statement in programming?
A conditional statement directs program control flow based on the value of a condition, which is a Boolean expression (true or false).
What is the general syntax of an if-then-else statement?
The general syntax is: if condition then consequent else alternative end if. The condition evaluates to true or false; if true, the consequent runs, otherwise the alternative runs. The else part is optional.
What is the 'dangling else' problem?
It occurs when an else clause is ambiguously paired with a preceding if in nested conditionals. It is resolved by using explicit block-ending syntax (like 'end if') or block enclosures (like curly braces).
What is an else-if (elif) construct?
An else-if construct allows chaining multiple conditions. Only the statements following the first true condition are executed; all others are skipped. It is often written as 'else if' or 'elif'.
In the pseudocode example, if discount is 10%, what is printed?
If the discount is 10%, the first if statement is true, so 'you have to pay $30' is printed, and all other statements are skipped.
Why does Python use the keyword 'elif'?
Because Python uses indentation for structure, a repeated 'else if' would require increased indentation after every condition. 'elif' avoids that.
Which languages use 'elsif'?
Perl and Ruby use 'elsif' to avoid large numbers of braces.
What is a switch statement?
A switch statement supports multiway branching by comparing the value of an expression with constant values and transferring control to the code of the first match. It often has a default action if no match is found.
What is the purpose of a default case in a switch statement?
The default case is executed when no other case matches the expression's value.
What is the Guarded Command Language (GCL)?
GCL, by Edsger Dijkstra, uses guarded commands: a list of Boolean guards with corresponding statements. Exactly one statement whose guard is true is executed, but which one is arbitrary.
What is the difference between a conditional statement and a conditional expression?
A conditional statement directs control flow, while a conditional expression evaluates to a value without changing control flow.
What is structured programming?
Structured programming supports control flow via code blocks, making code easier to read and maintain, as opposed to using goto statements.
What is the 'else if' construct in Ada?
In Ada, 'elseif' is syntactic sugar for the two words 'else if'.
What is the purpose of the 'end if' in pseudocode?
'end if' marks the end of an if-then-else statement, helping to avoid the dangling else problem.
What is the general syntax of an if-then-else statement?
if condition then consequent else alternative end if. The condition is a Boolean expression; if true, consequent runs, else alternative runs. The else part is optional.
What is the 'dangling else' problem?
It occurs when an else clause is ambiguously paired with a preceding if in nested conditionals. It is resolved by using explicit block-ending syntax (like 'end if') or block enclosures (like curly braces).
What is an else-if (elif) construct?
An else-if construct allows chaining multiple conditions. Only the statements following the first true condition are executed; all others are skipped. It is often written as 'else if' or 'elif'.
In the pseudocode example, if discount is 10%, what is printed?
If the discount is 10%, the first if statement is true, so 'you have to pay $30' is printed, and all other statements are skipped.
Why does Python use the keyword 'elif'?
Because Python uses indentation for structure, a repeated 'else if' would require increased indentation after every condition. 'elif' avoids that.
Which languages use 'elsif'?
Perl and Ruby use 'elsif' to avoid large numbers of braces.
What is a switch statement?
A switch statement supports multiway branching by comparing the value of an expression with constant values and transferring control to the code of the first match. It often has a default action if no match is found.
What is the purpose of a default case in a switch statement?
The default case is executed when no other case matches the expression's value.
What is the Guarded Command Language (GCL)?
GCL, by Edsger Dijkstra, uses guarded commands: a list of Boolean guards with corresponding statements. Exactly one statement whose guard is true is executed, but which one is arbitrary.
What is the difference between a conditional statement and a conditional expression?
A conditional statement directs control flow, while a conditional expression evaluates to a value without changing control flow.
What is structured programming?
Structured programming supports control flow via code blocks, making code easier to read and maintain, as opposed to using goto statements.