Apply for Zend Framework Certification Training

c

< Purpose of int main(),char main(),void main() with examples 10 Scenario-Based Questions on Decision-Making Statements in C >




Decision-Making Statements in C
Programs often need to make decisions while they are running. For example, a program may need to determine whether a student has passed, whether a person is old enough to vote, or whether a number is positive or negative.
In C, decision-making statements allow a program to examine a condition and choose which instructions should be executed.
A condition generally produces either:
True — the required condition is satisfied.
False — the condition is not satisfied.
These statements are an important part of controlling the flow of execution in a C program.
Simple Example
#include <stdio.h>
int main() {
    int marks = 75;
    if (marks >= 40) {
        printf("Student has passed");
    }
    return 0;
}
Output:
Student has passed
How It Works
The program checks the condition:
marks >= 40
Since marks contains 75, the condition is true, so the statement inside the if block is executed.
If the value of marks were less than 40, the condition would be false and the printf() statement would be skipped.
Types of Decision-Making Statements in C
C provides several ways to make decisions:
if statement
if-else statement
Nested if / Nested if-else
if-else-if ladder
switch statement
Conditional operator (?:)
Each method is useful for a different type of decision.
1. if Statement in C
The if statement is the most basic decision-making statement in C.
It executes a particular section of code only when its condition evaluates to true.
Syntax
if (condition) {
    // statements
}
Example
#include <stdio.h>
int main() {
    int age = 21;
    if (age >= 18) {
        printf("You are eligible to vote");
    }
    return 0;
}
Output:
You are eligible to vote
Explanation
Here:
age >= 18
is the condition being tested.
Because age is 21, the condition is true. Therefore, the statement inside the if block is executed.
If age were 16, the condition would be false and nothing inside the if block would execute.
Important Point
When an if block contains only one statement, braces can technically be omitted:
if (age >= 18)
    printf("Eligible to vote");
However, using {} is generally recommended because it makes the program easier to read and maintain.
2. if-else Statement in C
Sometimes a program needs to perform one action when a condition is true and another action when it is false.
For this situation, C provides the if-else statement.
Syntax
if (condition) {
    // executed when condition is true
}else {
    // executed when condition is false
}
Example
#include <stdio.h>
int main() {
    int number = 15;
    if (number % 2 == 0) {
        printf("The number is even");
    } else {
        printf("The number is odd");
    }
    return 0;
}
Output:
The number is odd
Explanation
The expression:
number % 2 == 0
checks whether the number is completely divisible by 2.
Since 15 is not divisible by 2, the condition is false. Therefore, the else block runs.
The important difference between if and if-else is that if-else provides an alternative path when the condition fails.
3. Nested if Statement
An if statement can be placed inside another if or else block. This arrangement is called a nested if statement.
Nested conditions are useful when one decision depends on another decision being satisfied first.
Example
#include <stdio.h>
int main() {
    int age = 25;
    int citizen = 1;
    if (age >= 18) {
        if (citizen == 1) {
            printf("You are eligible to vote");
        }  else {
            printf("Citizenship is required");
        }
    }  else {
        printf("You must be at least 18 years old");
    }
    return 0;
}
Output:
You are eligible to vote
Explanation
The program performs the checks in stages:
Step 1:
It checks whether:
age >= 18
is true.
Step 2:
Only if the first condition is true, it checks:
citizen == 1
Since both conditions are satisfied, the final message is displayed.
This approach is useful when conditions have a parent-child relationship.
4. if-else-if Ladder
When a program needs to compare a value against several different conditions, an if-else-if ladder can be used.
The conditions are evaluated sequentially, starting from the top.
As soon as a condition becomes true, its corresponding block executes and the remaining conditions are ignored.
Syntax
if (condition1) {
    // statements
}else if (condition2) {
    // statements
}else if (condition3) {
    // statements
}else {
    // statements
}
Example
#include <stdio.h>
int main() {
    int marks = 82;
    if (marks >= 90) {
        printf("Grade A+");
    } else if (marks >= 80) {
        printf("Grade A");
    } else if (marks >= 70) {
        printf("Grade B");
    } else if (marks >= 40) {
        printf("Grade C");
    } else {
        printf("Fail");
    }
    return 0;
}
Output:
Grade A
Explanation
The program checks the conditions from top to bottom:
marks >= 90  → False
marks >= 80  → True
As soon as marks >= 80 becomes true, "Grade A" is printed. The remaining conditions are not checked.
When to Use It
An if-else-if ladder is appropriate when:
Several conditions need to be evaluated.
The conditions are related.
Only one result should be selected.
5. switch Statement in C
The switch statement is useful when a program needs to select an action based on the value of an expression.
It is commonly used when there are multiple fixed choices.
Syntax
switch (expression) {
    case value1:
        // statements
        break;
    case value2:
        // statements
        break;
    default:
        // statements
}
Example
#include <stdio.h>
int main() {
    int choice = 2;
    switch (choice) {
        case 1:
            printf("You selected Home");
            break;
        case 2:
            printf("You selected Profile");
            break;
        case 3:
            printf("You selected Settings");
            break;
        default:
            printf("Invalid choice");
    }
    return 0;
}
Output:
You selected Profile
How switch Works
The value of choice is compared with each case.
Since:
choice = 2
matches:
case 2:
the corresponding statement is executed.
What Does break Do?
The break statement terminates the switch block.
Without break, execution can continue into the following cases. This behavior is known as fall-through.
Important Note
The expression used with switch must produce an integral value, such as an integer or character type. Floating-point values such as float and double cannot be directly used as switch expressions.
6. Conditional Operator (?:) in C
The conditional operator, also known as the ternary operator, provides a compact way to perform a simple two-way decision.
It is often used as a shorter alternative to a basic if-else statement.
Syntax
condition ? expression_if_true : expression_if_false;
It contains three parts:
A condition
The expression executed when the condition is true
The expression executed when the condition is false
Example
#include <stdio.h>
int main() {
    int age = 20;
    int result = (age >= 18) ? 1 : 0;
    printf("Result = %d", result);
    return 0;
}
Output:
Result = 1
Explanation
The condition:
age >= 18
is true because age is 20.
Therefore:
1
is assigned to result.
If age were less than 18, the value:
0
would be assigned instead.
The same logic using if-else would require more lines:
if (age >= 18)
    result = 1;
else
    result = 0;
The conditional operator is therefore convenient for short and simple decisions.
Comparison of Decision-Making Statements
Statement Best Used For
if Checking a single condition
if-else Choosing between two possibilities
Nested if Making dependent or sequential decisions
if-else-if Checking several related conditions
switch Selecting from multiple fixed values
?: Writing a short two-way decision
Key Points to Remember
Decision-making statements control which part of a program gets executed.
if executes code only when its condition is true.
if-else provides two possible execution paths.
A nested if places one decision inside another.
An if-else-if ladder is useful for evaluating multiple conditions.
switch is convenient when choices depend on fixed integral or character values.
The conditional operator ?: is a compact alternative for simple if-else decisions.
Use braces {} even when they are optional to make code clearer and safer.
In a switch, break prevents unintended execution of subsequent cases.

< Purpose of int main(),char main(),void main() with examples 10 Scenario-Based Questions on Decision-Making Statements in C >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top