Control Flow: Sequencing and 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

In computer science, the order in which individual statements, instructions, or function calls are executed or evaluated is known as the ______.

flow of control

In structured programming, execution proceeds sequentially from one statement to the next within a code block; this type of flow control is known as ______.

sequencing

______ statements such as if, if-else, and switch, allow programs to make decisions based on certain conditions.

Selection

The primary difference between a while loop and a do-while loop is that a do-while loop will execute the code block at least ______ before checking the condition.

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

A ______ statement is an unconditional branch statement that transfers control to a labeled statement within the same function, but its use is generally discouraged due to potential readability and maintenance issues.

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

Within a switch statement, the ______ keyword is used to prevent fall-through to the next case, ensuring that only the code block associated with the matching case is executed.

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

Inside a loop, a ______ statement is used to skip the rest of the current iteration and proceed directly to the next iteration, without exiting the loop entirely.

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

The ______ statement is used to exit a function and optionally return a value to the caller; if the function's return type is void, it can be used without a value to simply exit the function.

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

In a for loop, the increment expression is executed ______ each iteration, often used to update a counter variable that controls the loop's termination.

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

Distinguish between sequencing and iteration; sequencing executes instructions ______, while iteration repeats a block of code based on a condition.

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

Flashcards

Flow of Control

The order in which individual statements, instructions, or function calls are executed in a program.

Sequencing

Executes statements in the order they appear in the source code, one after another.

Selection Statements

Allows programs to execute different code sections based on whether a condition is true or false.

if statement

Executes a block of code only if a specified condition is true.

Signup and view all the flashcards

if-else statement

Provides an alternative code block to executes if the initial condition of an if statement is false.

Signup and view all the flashcards

switch statement

Selects one code block to execute from multiple choices based on the value of a variable or expression.

Signup and view all the flashcards

Iteration Statements

Allows a block of code to be executed repeatedly as long as a specified conditon remains true.

Signup and view all the flashcards

for loop

Repeats a block of code a specific number of times, controlled by initialization, condition, and increment.

Signup and view all the flashcards

while loop

Executes a block of code as long as a condition is true, with the condition checked before each execution.

Signup and view all the flashcards

do-while loop

Similar to a while loop, but the code block is executed at least once before the condition is checked.

Signup and view all the flashcards

Study Notes

  • Flow of control, or control flow, dictates the order of execution for statements, instructions, and function calls in an imperative program.
  • Statements include expressions, which always return values.
  • An example of a simple statement: x = x + 1.
  • Control flow statements alter the order in which statements are executed.
  • Categories of control flow statements:
    • Sequencing
    • Selection
    • Iteration
    • Unconditional branch

Sequencing

  • A code block groups statements together.
  • Structured programming executes statements sequentially.
  • Sequencing represents the most basic control flow.
  • Statements are executed in the order they appear in source code.
  • After executing a statement, control flow moves to the next one.
  • Code blocks are fundamental for grouping statements in control structures.

Selection

  • Selection, or conditional, statements allow programs to make decisions, executing different code sections based on conditions.
  • Selection statements include:
    • if statements
    • if-else statements
    • switch statements

if statements

  • if statements execute a block of code only when a specified condition is true.
  • Example in C++:
if (condition) {
    // Code to execute if condition is true
}
  • The condition is a boolean expression.
  • If the condition is true, the code block within the if statement executes.
  • If the condition is false, the code block is skipped.

if-else statements

  • if-else statements provide an alternative code block to execute if the if condition is false.
  • Example in C++:
if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}
  • If the condition is true, the first block of code executes.
  • If the condition is false, the second block (inside the else statement) is executed.

switch statements

  • switch statements select among multiple code blocks based on a variable or expression's value.
  • Example in C++:
switch (variable) {
    case value1:
        // Code to execute if variable == value1
        break;
    case value2:
        // Code to execute if variable == value2
        break;
    default:
        // Code to execute if variable doesn't match any case
}
  • The switch statement evaluates the variable and compares it to case values.
  • If a match is found, the code block for that case executes.
  • The break statement exits the switch after a match.
  • If no case matches, the default case executes, if provided.

Iteration

  • Iteration statements, or loops, allow repeated code execution.
  • Loops continue as long as a specified condition remains true.
  • Iteration statements include:
    • for loops
    • while loops
    • do-while loops

for loops

  • for loops repeat a code block a specific number of times.
  • Example in C++:
for (initialization; condition; increment) {
    // Code to execute repeatedly
}
- Initialization executes before the loop starts.
- Condition is checked before each iteration; the loop continues if `true`.
- Increment executes after each iteration, often updating a counter.

while loops

  • while loops execute a code block as long as a specified condition is true.
  • Example in C++:
while (condition) {
    // Code to execute while condition is true
}
  • The condition is checked before each iteration.
  • If the condition is true, the code block executes; otherwise, the loop terminates.

do-while loops

  • do-while loops execute a code block at least once before checking the condition.
  • Example in C++:
do {
    // Code to execute at least once
} while (condition);
  • The code block executes first, and then the condition is checked.
  • If the condition is true, the loop continues; otherwise, it terminates.

Unconditional Branch

  • Unconditional branch statements transfer control to another part of the program without any condition.
  • These statements include:
    • goto statements
    • break statements
    • continue statements
    • return statements

goto statements

  • goto statements transfer control to a labeled statement within the same function.
  • Example in C++:
goto label;
// Some code
label:
// Code after the label
  • goto statements can reduce code readability and maintainability, so they are generally not recommended in structured programming.
  • The goto statement moves execution to the program point with the corresponding label.

break statements

  • break statements are used inside loops or switch statements to exit the structure early.
  • Inside a loop, break terminates the loop, transferring control to the next statement.
for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break; // Exit the loop when i equals 5
    }
    // Some code
}
  • Inside a switch statement, break prevents fall-through to the next case.
switch (variable) {
    case value1:
        // Code
        break;
    case value2:
        // Code
        break;
}

continue statements

  • continue statements, used inside loops, skip the rest of the current iteration and proceed to the next iteration.
  • Example in C++:
for (int i = 0; i < 10; i++) {
    if (i == 5) {
        continue; // Skip the rest of the iteration when i equals 5
    }
    // Some code
}
  • When continue is encountered, the remaining statements are skipped, and the loop goes to the next iteration.

return statements

  • return statements exit a function, returning a value to the caller (if the function has a return type).
  • Example in C++:
int add(int a, int b) {
    return a + b; // Return the sum of a and b
}
  • The return statement terminates the function and passes the value back to the caller.
  • For a void return type, return can be used without a value to simply exit the function.
void printMessage() {
    cout

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

Use Quizgecko on...
Browser
Browser