Control Structures: Branching

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

What is the primary purpose of control structures in programming?

  • To control the execution flow of a program. (correct)
  • To create HTML elements.
  • To define variable names.
  • To comment the code.

What does the if statement do?

  • It defines a function.
  • It evaluates a condition and executes code if the condition is true. (correct)
  • It executes code repeatedly.
  • It always executes the code block.

What does the else statement do when used with an if statement?

  • It always skips the `if` block.
  • It executes if the `if` condition is true.
  • It defines a new variable.
  • It executes if the `if` condition is false. (correct)

What is the purpose of a switch statement?

<p>To perform different actions based on different conditions. (C)</p>
Signup and view all the answers

What is the purpose of the break keyword in a switch statement?

<p>To exit from the <code>switch</code> block. (C)</p>
Signup and view all the answers

What is a loop?

<p>A structure that executes code repeatedly. (C)</p>
Signup and view all the answers

Which of the following is a component of a for loop?

<p>An initializer. (C)</p>
Signup and view all the answers

What is a 'pre-test' loop?

<p>A loop that evaluates the condition before executing statements. (C)</p>
Signup and view all the answers

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

<p>The <code>do...while</code> loop evaluates the condition after its statements have been executed. (C)</p>
Signup and view all the answers

What is the purpose of the continue keyword?

<p>To skip a single iteration of a loop. (D)</p>
Signup and view all the answers

Control structures determine the execution flow of a program.

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

An if statement executes code regardless of the initial condition.

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

The else block is executed when the condition in the if statement is true.

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

A switch statement can only compare against integer values.

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

In a switch statement, the default block is executed when no case matches the expression.

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

The break keyword in a switch statement is optional.

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

A for loop executes a block of code a fixed number of times.

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

A while loop continues executing as long as the condition is false.

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

A do...while loop always executes the code block at least once.

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

The break statement skips the rest of the current iteration and continues with the next iteration of the loop.

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

In JavaScript, what happens if you omit the break statement at the end of a case block within a switch statement?

<p>The next <code>case</code> block will be executed, regardless of whether its condition matches. (C)</p>
Signup and view all the answers

Which type of loop is guaranteed to execute its code block at least once, regardless of whether the condition is initially true or false?

<p><code>do...while</code> loop (C)</p>
Signup and view all the answers

Given a for loop with the structure for (initializer; condition; modifier), which part is responsible for updating the loop counter variable after each iteration?

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

What is the primary difference between a while loop and a for loop in JavaScript?

<p>A <code>for</code> loop typically includes initialization, condition, and modification expressions, while a <code>while</code> loop only has a condition. (D)</p>
Signup and view all the answers

In a for loop, what happens if the 'condition' part is always true?

<p>The loop will execute indefinitely, creating an infinite loop. (C)</p>
Signup and view all the answers

What is the purpose of the default keyword in a switch statement?

<p>It specifies the code to be executed if none of the <code>case</code> expressions match the <code>switch</code> expression. (B)</p>
Signup and view all the answers

Consider the following code snippet: if (x > 5) { y = 10; } else if (x > 2) { y = 5; } else { y = 2; } If x is equal to 3, what value will y have after this code is executed?

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

When is the condition in a while loop evaluated?

<p>Before each execution of the loop's code block. (B)</p>
Signup and view all the answers

What is the effect of the continue statement within a loop?

<p>It skips the rest of the current iteration and proceeds to the next iteration. (A)</p>
Signup and view all the answers

Which of the following is the correct syntax for a switch statement in Javascript?

<p><code>switch (expression) { case value1: code; case value2: code; default: code; }</code> (C)</p>
Signup and view all the answers

Control structures are not essential for decision-making in programming.

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

The if statement will execute code regardless of whether the condition is true or false.

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

Extending an if statement with an else block provides an alternative execution path when the condition is false.

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

In a switch statement, if no break keyword is used, execution will stop at the end of each case even if the evaluation does not match the case.

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

The getDay() method returns the weekday as a string (e.g., "Monday", "Tuesday").

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

A for loop must contain an initializer, a condition, and a terminator.

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

While loops and for loops evaluate the test condition after the statements are executed.

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

The do while loop always executes the code block at least once.

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

The break keyword can only be used inside a switch statement.

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

The continue keyword will terminate the entire loop when a specified condition is met.

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

Flashcards

Control Structures

Determine the order in which program statements are executed, allowing for conditional execution and looping.

"If" Statement

Evaluates a condition; executes code if the condition is true.

"Else" Statement

Adds an alternative execution path when the initial 'if' condition is false.

Switch Statement

A statement to perform different actions based on different conditions.

Signup and view all the flashcards

Loop

A structure that repeatedly executes a block of code as long as a condition is true.

Signup and view all the flashcards

"Do While" Loop

A loop that first executes the code block and then checks the condition.

Signup and view all the flashcards

"Break" Keyword

Immediately exits a loop, regardless of the loop condition.

Signup and view all the flashcards

"Continue" Keyword

Skips the current iteration of a loop and proceeds to the next iteration.

Signup and view all the flashcards

Initializer

A statement that sets the initial value of a loop counter variable.

Signup and view all the flashcards

Condition

An expression that is evaluated before each iteration in the loop.

Signup and view all the flashcards

Else If Statement

Adds multiple conditional tests to an 'if' statement by testing conditions at the start of each 'else' block.

Signup and view all the flashcards

Switch 'Break' Keyword

A keyword that breaks out of the current case in a switch statement.

Signup and view all the flashcards

While Loop

Evaluates a condition before executing statements; the loop continues as long as the condition is true.

Signup and view all the flashcards

Loop Modifier

A value in the test condition is updated to ensure that the condition will eventually evaluate to false.

Signup and view all the flashcards

Iteration

Each pass through a loop, involving checking the condition and executing statements.

Signup and view all the flashcards

Loop's Termination Condition

The condition, when unmet, causes a loop to stop executing.

Signup and view all the flashcards

Pre-Test loops

A loop that evaluates the condition before executing statements.

Signup and view all the flashcards

Switch 'Default' Statement

The keyword in a switch statement that executes when no case matches.

Signup and view all the flashcards

Branch If

Evaluates a condition, and if true, executes the associated code block. If false, the code is skipped.

Signup and view all the flashcards

For loop

Is the most commonly used loop structure in JavaScript, containing an initializer, condition, and modifier.

Signup and view all the flashcards

Study Notes

  • Control structures manage the execution flow of a program by allowing conditional execution and looping
  • Control structures are essential for decision-making, repetitive execution, efficient coding, and logic building

Branch If

  • The if statement evaluates a condition
  • If the condition is true, the associated code block is executed
  • Syntax: if (condition) { // code block }
  • Example:
    if (hour < 18) {
      greeting = "Good day";
    }
    

Branch Alternatives with Else

  • Extending the if statement with else adds an alternative execution path when the condition is false
  • Syntax: if (condition) { // code for true } else { // code for false }
  • Example:
    if (hour < 18) {
      greeting = "Good day";
    } else {
      greeting = "Good evening";
    }
    

Multiple Branches

  • Multiple conditional if tests at the start of each else statement block can provide multiple branches
  • Syntax: if (condition) { // code for true } else if (condition) { // code for false }
  • Example:
    if (hour < 18) {
      greeting = "Good day";
    } else if (hour < 24) {
      greeting = "Good evening";
    }
    

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

If-Else Statements in C++
8 questions

If-Else Statements in C++

StatelyPoltergeist avatar
StatelyPoltergeist
Branching - Week 4, Lecture 1
21 questions

Branching - Week 4, Lecture 1

AffectionateCoconutTree1215 avatar
AffectionateCoconutTree1215
CS4051 Lecture 3: Branching and Iteration
29 questions
Use Quizgecko on...
Browser
Browser