JavaScript Selection Statements - CP1-6
29 Questions
0 Views

JavaScript Selection Statements - CP1-6

Created by
@StreamlinedRainbow

Questions and Answers

What is the primary function of selection statements in JavaScript?

  • To loop through a series of statements.
  • To make decisions based on conditions. (correct)
  • To store variable values.
  • To define function parameters.
  • What does an if-else statement do when its condition evaluates to false?

  • It executes the if block.
  • It executes the else block. (correct)
  • It skips to the end of the code.
  • It throws an error.
  • When would you use an else if statement in JavaScript?

  • To change values in an array.
  • To check multiple conditions successively. (correct)
  • To declare a function.
  • To create an infinite loop.
  • In a switch statement, what is the purpose of the break keyword?

    <p>To prevent fall-through behavior.</p> Signup and view all the answers

    What is the output when the score is set to 50 in the provided if-else example?

    <p>You failed.</p> Signup and view all the answers

    What will the following code output if day is set to 4? switch (day) { case 1: console.log('Monday'); break; case 2: console.log('Tuesday'); break; case 3: console.log('Wednesday'); break; default: console.log('Invalid day'); }

    <p>Invalid day</p> Signup and view all the answers

    Which of the following statements correctly describes a condition in the context of an if statement?

    <p>An expression that evaluates to true or false.</p> Signup and view all the answers

    Which selection statement is most appropriate for evaluating an expression against multiple possible cases?

    <p>switch statement</p> Signup and view all the answers

    What will be the output if the role is set to 'editor'?

    <p>Access to edit content.</p> Signup and view all the answers

    Which statement accurately describes a 'for' loop in JavaScript?

    <p>It runs only when the number of iterations is predetermined.</p> Signup and view all the answers

    What is the correct syntax structure for a 'for' loop?

    <p>for (initialization; condition; increment/decrement) {}</p> Signup and view all the answers

    When would a switch statement default case execute?

    <p>When the role matches no predefined cases.</p> Signup and view all the answers

    What type of tasks are looping statements particularly beneficial for in JavaScript?

    <p>Tasks that are repetitive or require iteration.</p> Signup and view all the answers

    What happens if no conditions are met in an if...else if statement?

    <p>The else block runs, if present.</p> Signup and view all the answers

    What is the purpose of the nested if statement?

    <p>To check secondary conditions only if the primary condition is true.</p> Signup and view all the answers

    In the grading system example, which grade corresponds to marks below 60?

    <p>Grade: F</p> Signup and view all the answers

    Which part of the if statement runs when the condition evaluates to false?

    <p>Code block inside the else.</p> Signup and view all the answers

    What is indicated by the condition within an if statement?

    <p>An expression that can either be true or false.</p> Signup and view all the answers

    When is an else if block executed in an if...else if statement?

    <p>When all previous conditions are false.</p> Signup and view all the answers

    What will the following code output if number is set to 5? if (number % 2 === 0) { console.log('The number is even.'); } else { console.log('The number is odd.'); }

    <p>'The number is odd.'</p> Signup and view all the answers

    In an if...else structure, what is the code block for if responsible for?

    <p>Running code only if the condition is true.</p> Signup and view all the answers

    What is the purpose of the outer condition in a nested if statement?

    <p>To evaluate first before executing any inner conditions</p> Signup and view all the answers

    What will be displayed in the console if the age is 17 and hasMembership is true?

    <p>Access denied. You must be at least 18 years old.</p> Signup and view all the answers

    What does the 'break' statement do in a switch statement?

    <p>It ends the current case block and prevents fall-through</p> Signup and view all the answers

    What happens if no cases match in a switch statement?

    <p>The default block is executed</p> Signup and view all the answers

    Which of the following is NOT a part of the switch statement syntax?

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

    What will the console output if age is 25 and hasMembership is false?

    <p>Access granted to the general area.</p> Signup and view all the answers

    Which statement best describes nested if statements?

    <p>They allow for a hierarchical structure of conditions</p> Signup and view all the answers

    What is a consequence of omitting the 'break' statement in a switch case?

    <p>It will cause the next case to execute</p> Signup and view all the answers

    Study Notes

    JavaScript Selection Statements

    • Selection statements allow decision-making in programs by executing specific code blocks based on evaluated conditions.
    • Common selection statements include if, else if, else, and switch.

    If-Else Statement

    • Executes a block of code if a condition is true; otherwise, executes the else block.
    • Example:
      let age = 20;
      if (age >= 18) {
          console.log("You are an adult.");
      } else {
          console.log("You are a minor.");
      }
      

    Else If Statement

    • Checks multiple conditions in sequence.
    • Example:
      let score = 85;
      if (score >= 90) {
          console.log("Grade: A");
      } else if (score >= 75) {
          console.log("Grade: B");
      } else {
          console.log("Grade: C");
      }
      

    Switch Statement

    • Evaluates an expression and executes code corresponding to matching cases.
    • Example:
      let day = 2;
      switch (day) {
          case 1:
              console.log("Monday");
              break;
          case 2:
              console.log("Tuesday");
              break;
          default:
              console.log("Invalid day");
      }
      

    If Statement

    • Executes a block of code if a specified condition is true.
    • Syntax:
      if (condition) {
          // code to execute
      }
      
    • Example with else:
      let score = 50;
      if (score >= 60) {
          console.log("You passed.");
      } else {
          console.log("You failed.");
      }
      

    If...Else Statement

    • Executes one block if true, another if false.
    • Syntax:
      if (condition) {
          // true block
      } else {
          // false block
      }
      
    • Basic example:
      let number = 10;
      if (number % 2 === 0) {
          console.log("The number is even.");
      } else {
          console.log("The number is odd.");
      }
      

    If...Else If Statement

    • Evaluates multiple conditions in sequence for greater flexibility.
    • Syntax:
      if (condition1) {
          // block for condition1
      } else if (condition2) {
          // block for condition2
      }
      
    • Example of a grading system:
      let marks = 82;
      if (marks >= 90) {
          console.log("Grade: A");
      } else if (marks >= 80) {
          console.log("Grade: B");
      } else if (marks >= 70) {
          console.log("Grade: C");
      } else if (marks >= 60) {
          console.log("Grade: D");
      } else {
          console.log("Grade: F");
      }
      

    Nested If Statement

    • Allows evaluation of multiple conditions hierarchically.
    • Syntax:
      if (condition1) {
          if (condition2) {
              // code for both conditions true
          } else {
              // code for condition1 true, condition2 false
          }
      } else {
          // code for condition1 false
      }
      
    • Example:
      let age = 25;
      let hasMembership = true;
      if (age >= 18) {
          if (hasMembership) {
              console.log("Access granted to the VIP lounge.");
          } else {
              console.log("Access granted to the general area.");
          }
      } else {
          console.log("Access denied. You must be at least 18 years old.");
      }
      

    Switch Statement

    • Organizes execution based on the value of an expression.
    • Syntax:
      switch (expression) {
          case value1:
              // block for value1
              break;
          default:
              // block if no case matches
      }
      
    • Example for user role access:
      let role = "admin";
      switch (role) {
          case "admin":
              console.log("Access to all features.");
              break;
          case "editor":
              console.log("Access to edit content.");
              break;
          default:
              console.log("No access.");
      }
      

    Looping Statements

    • Enable execution of code blocks repeatedly based on conditions; main types are for, while, and do...while.

    For Loop

    • Executes a block of code a specified number of times; ideal when iterations are anticipated.
    • Syntax:
      for (initialization; condition; increment/decrement) {
          // code to execute
      }
      
    • Example of summing numbers (incomplete in original text).

    Studying That Suits You

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

    Quiz Team

    Description

    This quiz focuses on selection statements in JavaScript, including if-else and switch statements. It evaluates your understanding of how these statements make decisions within a program by executing particular code blocks based on specified conditions. Test your knowledge with various examples and scenarios.

    More Quizzes Like This

    jQuery Method and Selector Quiz
    6 questions
    Basic JavaScript Functions Quiz
    6 questions

    Basic JavaScript Functions Quiz

    SustainableAntigorite1088 avatar
    SustainableAntigorite1088
    JavaScript Class Definition
    12 questions
    Use Quizgecko on...
    Browser
    Browser