C Control Structures: Selection

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

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.

False (B)

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?

<p>0</p> Signup and view all the answers

The '&&' operator in C represents the logical ______ operation.

<p>AND</p> Signup and view all the answers

Which of these C code snippets correctly checks if both x and y are greater than z?

<p>(x &gt; z) &amp;&amp; (y &gt; z) (C)</p> Signup and view all the answers

In C, you can assign an integer variable a non-zero value for 'true' and zero for 'false'.

<p>True (A)</p> Signup and view all the answers

What happens if you nest an if statement inside another if statement's else clause?

<p>The nested <code>if</code> is executed only if the first <code>if</code>'s condition is false. (D)</p> Signup and view all the answers

In a nested if structure, how many of the statements will be executed?

<p>1</p> Signup and view all the answers

The break statement is optional in a switch statement.

<p>True (A)</p> Signup and view all the answers

In C, what data types can the expression in a switch statement be?

<p>int or char (B)</p> Signup and view all the answers

The default case in a switch statement is ______.

<p>optional</p> Signup and view all the answers

Match the following C operators with their meanings:

<p>&lt; = Less than</p> <blockquote> <p>= = Greater than or equal to == = Equal to != = Not equal to</p> </blockquote> Signup and view all the answers

What happens if you forget the closing brace of a switch statement's body in C?

<p>It's a syntax error, and the compiler will not compile the code. (A)</p> Signup and view all the answers

A switch statement can generally compare doubles.

<p>False (B)</p> Signup and view all the answers

Advantages of if:

<p>More general than a switch (B)</p> Signup and view all the answers

Which of the following is a common error in if statements in C?

<p>Using a semicolon after the condition (B)</p> Signup and view all the answers

What is the term for the control expression to be evaluated to select a given case?

<p>controlling expression</p> Signup and view all the answers

In the code if (x = 10), what will happen?

<p>The condition will always be true (A)</p> Signup and view all the answers

The = symbol checks for equality

<p>False (B)</p> Signup and view all the answers

Which of the following is a syntax error?

<p>if crsr or frgt == 'C'; (A)</p> Signup and view all the answers

An else is parsed with the closest ______ if statement.

<p>unmatched</p> Signup and view all the answers

Double values are permitted types in switch statements.

<p>False (B)</p> Signup and view all the answers

What happens if the break statement is ommitted?

<p>The execution falls through</p> Signup and view all the answers

Which case is executed when no other case is equal to the controlling expression and there is default?

<p>the default case (C)</p> Signup and view all the answers

The test if (0 <= x <= 4) is always true!

<p>True (A)</p> Signup and view all the answers

How to properly check a range:

<p>if (0 &lt;= x &amp;&amp; x &lt;= 4) (A)</p> Signup and view all the answers

Advantages of switch:

<p>A switch is more readable (B)</p> Signup and view all the answers

It is recommended to have a default case for a switch statement

<p>True (A)</p> Signup and view all the answers

Match the statement with where it belongs

<p>In switch statements, make sure the controlling expression and case labels are of the same permitted type. = Common Errors The statements following a case label may be one or more C statements, so you do not need to make multiple statements into a single compound statement using braces. = Remember Each else with the closest unmatched if = An else is parsed You cannot use a string such as 'Cruiser' or 'Frigate' as a case label = Remember</p> Signup and view all the answers

What is the result of if the break keyword has been removed form an expression with 3 cases?

<p>fall through</p> Signup and view all the answers

The break keyword is:

<p>Optional (C)</p> Signup and view all the answers

There is no limit on the amount of cases in a switch statement

<p>True (A)</p> Signup and view all the answers

The condition for a while loop must be surrounded by which punctuation?

<p>( ) (D)</p> Signup and view all the answers

A body also uses ______ statement.

<p>compound</p> Signup and view all the answers

How to solve 'Write if or if-else for each: If age is bigger than 60, give a discount of 10% on a purchase'?

<p>if (age &gt; 60) discount = 0.1 * price (B)</p> Signup and view all the answers

In C, you can assign a string to the case label.

<p>False (B)</p> Signup and view all the answers

Match the following

<p>if (number &gt; 0) printf (Positive ) = Checks for positive numbers else if (number &lt; 0) printf (Negative ) = Checks for negative numbers else printf (Zero ) = Checks for zero</p> Signup and view all the answers

For double and float data types, does C allow for them to be used as possible cases?

<p>no</p> Signup and view all the answers

What is the best construct for when student score is an integer?

<p>switch (C)</p> Signup and view all the answers

Flashcards

Control Structures

Control the flow of execution in a program or function.

Sequential Flow

Statements are executed in the order they are written.

Selection Control Structure

Chooses between multiple statements to execute based on a condition.

Repetition

Executes a block of code multiple times.

Signup and view all the flashcards

Compound Statement (Code Block)

A group of statements bracketed by { and } to specify sequential flow.

Signup and view all the flashcards

Selection Control Structure

A control structure that chooses among alternative program statements based on a condition.

Signup and view all the flashcards

Condition

An expression that is either false (0) or true (usually 1).

Signup and view all the flashcards

< Operator

Less than operator.

Signup and view all the flashcards

Operator

Greater than operator.

Signup and view all the flashcards

<= Operator

Less than or equal to operator.

Signup and view all the flashcards

= Operator

Greater than or equal to operator.

Signup and view all the flashcards

== Operator

Equal to operator.

Signup and view all the flashcards

!= Operator

Not equal to operator.

Signup and view all the flashcards

! Operator

Logical NOT operator.

Signup and view all the flashcards

&& Operator

Logical AND operator.

Signup and view all the flashcards

|| Operator

Logical OR operator.

Signup and view all the flashcards

"if" Statement

A selection statement that executes a block of code if a condition is true.

Signup and view all the flashcards

Logical Assignment

Assigning a non-zero value for true or zero for false to an int variable.

Signup and view all the flashcards

"if-else" Statement

A selection statement that executes one block of code if a condition is true and another block if it is false.

Signup and view all the flashcards

Nested if Statements

One 'if' statement inside another.

Signup and view all the flashcards

Multi-way Branching

Directs to one set of statements among many options

Signup and view all the flashcards

Switch Statement

A selection statement that allows you to choose one of several alternatives based on the value of a single variable.

Signup and view all the flashcards

Case Label

The value used to select one of the alternatives a switch statement

Signup and view all the flashcards

Break

To exit a switch statement.

Signup and view all the flashcards

Default Case

The statements executed if no case matches the controlling expression.

Signup and view all the flashcards

Advantages of switch:

It is more readable

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, and switch.
  • Repetition statements consist of for, while, and do...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 when void main() or exit(1) when int 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 or char.
  • 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 variable class 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 the switch statement.
  • If no case is equal to the value of class, then the default 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 write if (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.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser