Podcast
Questions and Answers
Which of the following statements evaluates a condition in C?
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.
The expression if(0)
results in a TRUE condition.
False (B)
What does the if-else statement do when the condition is 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 ______.
In an if-else ladder, if all conditions are FALSE, the final block of code executed is in the ______.
What is the primary purpose of the if statement in C?
What is the primary purpose of the if statement in C?
What is a nested if statement?
What is a nested if statement?
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”); }
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”); }
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
, andswitch-case
.
if Statement
- The
if
statement evaluates a condition and executes a block of code if the condition isTRUE
. - Conditions are evaluated as either
TRUE
orFALSE
. - If the expression results in zero, or the direct value is zero, the condition is
FALSE
. - Examples:
if(10)
isTRUE
.if(0)
isFALSE
.if(x + y)
isTRUE
if(x+y)
is not equal to zero.if(num1 > 5)
isTRUE
if the value ofnum
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 anif-else
statement within anotherif-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.
Related Documents
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.