Podcast
Questions and Answers
Which control structure executes a block of code multiple times?
Which control structure executes a block of code multiple times?
- Repetition (correct)
- Sequential flow
- Selection control structure
- Compound statement
A compound statement is used to specify repetition flow in C.
A compound statement is used to specify repetition flow in C.
False (B)
Which of the following is NOT a selection statement in C?
Which of the following is NOT a selection statement in C?
- if...else
- switch
- for (correct)
- if
What is the numerical representation of 'false' in C conditions?
What is the numerical representation of 'false' in C conditions?
The '&&' operator in C represents the logical ______ operation.
The '&&' operator in C represents the logical ______ operation.
Which of these C code snippets correctly checks if both x
and y
are greater than z
?
Which of these C code snippets correctly checks if both x
and y
are greater than z
?
In C, you can assign an integer variable a non-zero value for 'true' and zero for 'false'.
In C, you can assign an integer variable a non-zero value for 'true' and zero for 'false'.
What happens if you nest an if
statement inside another if
statement's else
clause?
What happens if you nest an if
statement inside another if
statement's else
clause?
In a nested if
structure, how many of the statements will be executed?
In a nested if
structure, how many of the statements will be executed?
The break
statement is optional in a switch
statement.
The break
statement is optional in a switch
statement.
In C, what data types can the expression in a switch
statement be?
In C, what data types can the expression in a switch
statement be?
The default
case in a switch
statement is ______.
The default
case in a switch
statement is ______.
Match the following C operators with their meanings:
Match the following C operators with their meanings:
What happens if you forget the closing brace of a switch
statement's body in C?
What happens if you forget the closing brace of a switch
statement's body in C?
A switch statement can generally compare doubles.
A switch statement can generally compare doubles.
Advantages of if:
Advantages of if:
Which of the following is a common error in if
statements in C?
Which of the following is a common error in if
statements in C?
What is the term for the control expression to be evaluated to select a given case?
What is the term for the control expression to be evaluated to select a given case?
In the code if (x = 10)
, what will happen?
In the code if (x = 10)
, what will happen?
The =
symbol checks for equality
The =
symbol checks for equality
Which of the following is a syntax error?
Which of the following is a syntax error?
An else
is parsed with the closest ______ if
statement.
An else
is parsed with the closest ______ if
statement.
Double values are permitted types in switch statements.
Double values are permitted types in switch statements.
What happens if the break statement is ommitted?
What happens if the break statement is ommitted?
Which case is executed when no other case is equal to the controlling expression and there is default?
Which case is executed when no other case is equal to the controlling expression and there is default?
The test if (0 <= x <= 4)
is always true!
The test if (0 <= x <= 4)
is always true!
How to properly check a range:
How to properly check a range:
Advantages of switch:
Advantages of switch:
It is recommended to have a default case for a switch statement
It is recommended to have a default case for a switch statement
Match the statement with where it belongs
Match the statement with where it belongs
What is the result of if the break
keyword has been removed form an expression with 3 cases?
What is the result of if the break
keyword has been removed form an expression with 3 cases?
The break
keyword is:
The break
keyword is:
There is no limit on the amount of cases in a switch statement
There is no limit on the amount of cases in a switch statement
The condition for a while loop must be surrounded by which punctuation?
The condition for a while loop must be surrounded by which punctuation?
A body also uses ______ statement.
A body also uses ______ statement.
How to solve 'Write if or if-else for each: If age is bigger than 60, give a discount of 10% on a purchase'?
How to solve 'Write if or if-else for each: If age is bigger than 60, give a discount of 10% on a purchase'?
In C, you can assign a string to the case label.
In C, you can assign a string to the case label.
Match the following
Match the following
For double and float data types, does C allow for them to be used as possible cases?
For double and float data types, does C allow for them to be used as possible cases?
What is the best construct for when student score is an integer?
What is the best construct for when student score is an integer?
Flashcards
Control Structures
Control Structures
Control the flow of execution in a program or function.
Sequential Flow
Sequential Flow
Statements are executed in the order they are written.
Selection Control Structure
Selection Control Structure
Chooses between multiple statements to execute based on a condition.
Repetition
Repetition
Signup and view all the flashcards
Compound Statement (Code Block)
Compound Statement (Code Block)
Signup and view all the flashcards
Selection Control Structure
Selection Control Structure
Signup and view all the flashcards
Condition
Condition
Signup and view all the flashcards
< Operator
< Operator
Signup and view all the flashcards
Operator
Operator
Signup and view all the flashcards
<= Operator
<= Operator
Signup and view all the flashcards
= Operator
= Operator
Signup and view all the flashcards
== Operator
== Operator
Signup and view all the flashcards
!= Operator
!= Operator
Signup and view all the flashcards
! Operator
! Operator
Signup and view all the flashcards
&& Operator
&& Operator
Signup and view all the flashcards
|| Operator
|| Operator
Signup and view all the flashcards
"if" Statement
"if" Statement
Signup and view all the flashcards
Logical Assignment
Logical Assignment
Signup and view all the flashcards
"if-else" Statement
"if-else" Statement
Signup and view all the flashcards
Nested if Statements
Nested if Statements
Signup and view all the flashcards
Multi-way Branching
Multi-way Branching
Signup and view all the flashcards
Switch Statement
Switch Statement
Signup and view all the flashcards
Case Label
Case Label
Signup and view all the flashcards
Break
Break
Signup and view all the flashcards
Default Case
Default Case
Signup and view all the flashcards
Advantages of switch:
Advantages of switch:
Signup and view all the flashcards
Study Notes
Control Structures
- Control structures manage the flow of execution in a program.
- The three basic control structures are sequential flow, selection, and repetition.
- Sequential flow is a group of statements bracketed that execute in order.
- Selection control structures choose statements to execute based on a condition.
- Repetition structures execute a code block multiple times.
Sequential Flow
- A compound statement/code block is a group of statements bracketed and specifies sequential flow.
- The
main
function is surrounded by brackets, and its statements are executed sequentially. - Function bodies use compound statements.
C Control Structures
- Selection statements in C consist of
if
,if...else
, andswitch
. - Repetition statements consist of
for
,while
, anddo...while
.
Selection Control Structure
- This lecture describes C control structure for selection.
- The selection control structure chooses alternative program statements based on a condition.
- A condition is an expression that is either false (0) or true (1).
Conditions
- Most conditions are performed using relational and logical operators.
<
means less than.>
means greater than.<=
means less than or equal to.>=
means greater than or equal to.==
means equal.!=
means not equal.!
means not.&&
means AND.||
means OR.
if
Statement
- The
if
statement determines whether to execute a statement or block. - The syntax is
if (Expression) Statement
. - Statement can be a single statement, a null statement, or a block.
Terminating a Program
return
- used whenvoid main()
orexit(1)
whenint main()
exit
function requires#include <stdlib.h>
.
Writing English Conditions in C
- Ensure the C condition is logically equivalent to the English statement.
- “x and y are greater than z” is written as
(x > z) && (y > z)
if-else
Statement
- The
if-else
statement chooses whether to execute statement A or statement B. - Statement A or B can be a single statement or a block.
- The syntax is
if (Expression) StatementA else StatementB
. - StatementA and StatementB can each be a single statement, a null statement, or a block.
- else braces can be omitted
Example Program: Circle Area and Circumference
- For a non-negative radius input by a user, calculate the circle area and circumference.
- Area = πr².
- Circumference = 2πr.
- For negative radiuses, display an "ERROR" message.
Nested if
Statements
- Nested if statements are when one if statement is inside another, to code decisions with multiple alternatives.
if (Expression1) Statement1 else if (Expression2) Statement2... else if (ExpressionN) StatementN else Statement N+1
- Exactly 1 of these statements will be executed.
Multi-Way Branching Code Example
- The sample code checks a student's credits earned and outputs the year they are in.
- Fourth year:
creditsEarned >= 90
- Third year:
creditsEarned >= 60
- Second year:
creditsEarned >= 30
- First year: all other
creditsEarned
values
Common if
Statements Errors
- No parentheses around the condition such as
if crsr_or_frgt == 'C'
. - A semicolon after the condition, such as
if (crsr_or_frgt == 'C');
.
Switch
Statement
- The switch statement is used instead of nested-if statements, to select several alternatives.
- It is based on the value of a single variable or simple expression known as the controlling expression.
- The value of this expression is commonly of type
int
orchar
. - It has limited usage and cannot be used when, for example, checking if x and y are greater than z
- Also cannot be used to know student score given his grade
- The syntax is:
switch (controlling expression) {
case value1: statements 1;
break;
case valueN: statements n;
break;
default: statements;
}
Switch
Statement Explanation
- The
switch
statement compares the variableclass
to each of the cases in a top-down approach. - It executes each line of code following the matching case, until it finds a
break
statement or the end of theswitch
statement. - If no case is equal to the value of
class
, then thedefault
case is executed. - The
default
case is optional.
Nested-if
Compared to Switch
- Advantages of
if
:- It has more general usage than a switch.
- It can be use with a range of values
- It can compare doubles
- Advantages of
switch
:- It is more readable.
- Use the switch whenever there are ten or fewer case labels.
Common Programming Errors
- Avoid
if (0 <= x <= 4)
and instead write(0 <= x && x <= 4)
- Avoid
if (x = 10)
and writeif (x = 10)
the=
symbol assigns x the value of 10, so is always true, you must use the==
More Common Errors
- Ensure you parenthesize the condition
- Ensure add
{
and}
if they are needed - C matches each else with the closest unmatched if
- In switch statements, ensure the controlling expression and case labels are of the same permitted type
- Remember to include the default case for switch statements
- Ensure add
{
and}
for the switch statement. - Always add
break
statements
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.