Decision Control Statements in C
7 Questions
6 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which of the following statements evaluates a condition in C?

  • do-while loop
  • for loop
  • if statement (correct)
  • while loop
  • The expression if(0) results in a TRUE condition.

    False

    What does the if-else statement do when the condition is FALSE?

    It executes the block of code in the else statement.

    In an if-else ladder, if all conditions are FALSE, the final block of code executed is in the ______.

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

    What is the primary purpose of the if statement in C?

    <p>To make a decision based on a condition</p> Signup and view all the answers

    What is a nested if statement?

    <p>An if statement inside another if or else statement.</p> Signup and view all the answers

    What output would result from the following code if number is 0? if (number < 0) { printf(“You entered negative number: %d”, number); } else { printf(“You entered zero”); }

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

    Study Notes

    Decision Control Statements in C

    • Decision control statements are used to execute code based on a condition.
    • The three main decision control statements in C are if, if-else, and switch-case.

    if Statement

    • The if statement evaluates a condition and executes a block of code if the condition is TRUE.
    • Conditions are evaluated as either TRUE or FALSE.
    • If the expression results in zero, or the direct value is zero, the condition is FALSE.
    • Examples:
      • if(10) is TRUE.
      • if(0) is FALSE.
      • if(x + y) is TRUE if (x+y) is not equal to zero.
      • if(num1 > 5) is TRUE if the value of num is greater than 5.

    Simple if Statement

    • The if statement evaluates the condition.
    • If the condition is TRUE, it executes the block of code that follows.
    • If the condition is FALSE, it skips the code.
    • Syntax:
      if (condition) {
          // Statements to be executed if condition is true
      }
      
    • Example:
      if (number < 0) { // Checks if number is negative
          printf("You entered negative number: %d", number);
      }
      

    if - else Statement

    • The if-else statement evaluates a condition.
    • If the condition is TRUE, it executes the first block of code.
    • If the condition is FALSE, it executes the second block of code.
    • Syntax:
      if (condition) {
          // Statements to be executed if condition is true
      } else {
          // Statements to be executed if condition is false
      }
      
    • Example:
      if (number < 0) { // Checks if number is negative
          printf("You entered negative number: %d", number);
      } else {
          printf("You entered zero or positive number: %d", number);
      }
      

    Nested if Statement

    • A nested if statement is an if-else statement within another if-else statement.
    • Syntax:
      if (condition1) {
          if (condition2) {
              // Statements to be executed if condition1 and condition2 are TRUE
          } else {
              // Statements to be executed if condition1 is TRUE and condition2 is FALSE
          }
      } else {
          // Statements to be executed if condition1 is FALSE
      }
      
    • Example:
      if (number < 0) { // Checks if number is negative
          printf("You entered negative number: %d", number);
      } else { 
          if (number == 0) { // Checks if number is equal to zero
              printf("You entered zero");
          } else {
              printf("You entered positive number: %d", number);
          }
      }
      

    if – else – if Statement (if-else Ladder)

    • The if-else-if ladder allows for multiple conditions to be checked.
    • Each condition is evaluated sequentially.
    • The first condition that is TRUE results in its corresponding code being executed.
    • If none of the conditions are TRUE, the code in the final 'else' block is executed.
    • Syntax:
      if (condition1) {
              // Statements to be executed if condition1 is TRUE
      } else if (condition2) {
          // Statements to be executed if condition1 is FALSE and condition2 is TRUE
      } else if (condition3) {
          // Statements to be executed if condition1 and condition2 are FALSE, and condition3 is TRUE
      } ... else {
          // Statements to be executed if all conditions are FALSE
      }
      
    • Example:
      if(number < 0) { // Checks if number is negative
          printf("You entered negative number: %d", number);
      } else if (number > 0) { // Checks if number is positive
          printf("You entered positive number: %d", number);
      } else { 
          printf("You entered zero");
      }
      

    Studying That Suits You

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

    Quiz Team

    Related Documents

    5_if_else.pdf

    Description

    This quiz focuses on decision control statements used in C programming. It covers the if, if-else, and switch-case statements, demonstrating how conditions are evaluated and executed based on their truthiness. Ideal for learners looking to solidify their understanding of control flow in C.

    More Like This

    If Statement in Programming
    20 questions
    Use Quizgecko on...
    Browser
    Browser