C Programming Decision Making Statements If If Else Nested If Switch Case Complete Guide
Understanding the Core Concepts of C Programming Decision Making Statements if, if else, nested if, switch case
C Programming Decision Making Statements: if
, if-else
, Nested if
, and switch-case
if
Statement
The if
statement allows a single block of code to be executed if a specified condition is true. It's the simplest form of conditional statements.
Syntax:
if (condition) {
// Statements to be executed if the condition is true
}
- Condition: This is an expression that evaluates to either true (
1
) or false (0
). - Block of Code: The code placed within the curly braces
{}
will execute only if the condition evaluates to true.
Working Principle:
- The condition inside the parentheses
()
is evaluated. - If the condition is true (non-zero), the code block within the curly braces
{}
is executed. - If the condition is false (zero), the control moves to the next statement or block of code following the
if
statement.
Example:
int number = 5;
if (number > 3) {
printf("The number is greater than 3\n");
}
if-else
Statement
The if-else
statement provides two paths for decision-making—when the condition is true, one block executes; when it's false, another block runs.
Syntax:
if (condition) {
// Statements to be executed if the condition is true
} else {
// Statements to be executed if the condition is false
}
Working Principle:
- The condition is evaluated.
- If true, the first block of code executes.
- If false, the second block of code (following
else
) executes.
Example:
int age = 20;
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
Nested if
Statement
A nested if
statement occurs when an if
or if-else
statement is placed inside another if
or if-else
statement. This structure allows for more complex conditions to be evaluated.
Syntax:
if (condition1) {
if (condition2) {
// Statements to be executed if both condition1 and condition2 are true
} else {
// Statements to be executed if condition1 is true but condition2 is false
}
} else {
// Statements to be executed if condition1 is false
}
Working Principle:
- The outer
if
statement's condition is evaluated first. - If it's true, the inner
if
condition is evaluated.- If the inner
if
condition is true, the innermost block executes. - If the inner
if
condition is false, theelse
block associated with it executes.
- If the inner
- If the outer
if
condition is false, theelse
block after the outerif
executes.
Example:
int grade = 90;
if (grade >= 60) {
if (grade >= 90) {
printf("Grade is A\n");
} else {
printf("Grade is B or C\n");
}
} else {
printf("Grade is below C\n");
}
Important Points:
- Nested
if
statements can lead to complex and difficult-to-maintain code. Use them judiciously. - Indenting nested
if
statements improves readability.
switch-case
Statement
The switch-case
statement is used when there are multiple conditions depending on the value of the same variable. It's cleaner and more readable than using multiple if-else
statements for such scenarios.
Syntax:
switch(expression) {
case constant1:
// Statements to be executed if expression == constant1
break;
case constant2:
// Statements to be executed if expression == constant2
break;
...
default:
// Statements to be executed if expression doesn't match any constant
break;
}
Working Principle:
- The
expression
is evaluated once. - The result of the
expression
is compared with the values of eachcase
. - If a match is found, the corresponding
case
's statements execute. - If no match is found, the
default
case's statements execute. - The
break
statement prevents the program from falling through to the next case.
Example:
char grade = 'B';
switch(grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
case 'C':
printf("Well done\n");
break;
case 'D':
printf("You passed\n");
break;
case 'F':
printf("Better try again\n");
break;
default:
printf("Invalid grade\n");
break;
}
Important Points:
- The
expression
must evaluate to an integer type (int, char
). - You can use the same statement or series of statements for multiple
cases
. - Use
default
to handle cases not covered by existingcase
labels. - Each
case
must have a unique integral constant or character literal. - The
break
statement is crucial to prevent fall-through.
Summary of Key Information
if
Statement:- Executes code if the condition is true.
- Single conditional path.
if-else
Statement:- Executes different blocks of code based on a single condition.
- Provides two possible execution paths.
Nested
if
Statement:- Allows multiple levels of conditions.
- Can become complex; careful planning is necessary.
- Indentation improves readability.
switch-case
Statement:- Used for evaluating multiple possible values of a single expression.
- Cleaner syntax for multiple conditions on the same variable.
- Each
case
should have a unique constant value. break
prevents fall-through to subsequent cases.default
handles unexpected values.
Online Code run
Step-by-Step Guide: How to Implement C Programming Decision Making Statements if, if else, nested if, switch case
1. Using the if
statement
The if
statement in C is used to execute a block of code only if a specified condition is true.
Example: Check if a number is positive
#include <stdio.h>
int main() {
int number;
// Ask the user to enter a number
printf("Enter a number: ");
scanf("%d", &number);
// Check if the number is positive
if (number > 0) {
printf("%d is positive.\n", number);
}
return 0;
}
Steps:
- Include the standard input-output library.
- Declare an integer variable
number
. - Prompt the user to enter a number and read the input using
scanf
. - Use an
if
statement to check if the number is greater than zero. - If the condition is true, print that the number is positive.
2. Using the if-else
statement
The if-else
statement is used to execute one block of code if a condition is true, and another block if the condition is false.
Example: Check if a number is positive or negative
#include <stdio.h>
int main() {
int number;
// Ask the user to enter a number
printf("Enter a number: ");
scanf("%d", &number);
// Check if the number is positive or negative
if (number > 0) {
printf("%d is positive.\n", number);
} else {
printf("%d is negative.\n", number);
}
return 0;
}
Steps:
- Include the standard input-output library.
- Declare an integer variable
number
. - Prompt the user to enter a number and read the input using
scanf
. - Use an
if-else
statement to check if the number is greater than zero.- If true, print that the number is positive.
- If false, print that the number is negative.
3. Using the nested if
statement
A nested if
statement is when one if
or else if
statement is inside another if
or else if
statement. This allows for more complex conditions to be checked.
Example: Determine if a number is positive, negative, or zero
#include <stdio.h>
int main() {
int number;
// Ask the user to enter a number
printf("Enter a number: ");
scanf("%d", &number);
// Determine if the number is positive, negative, or zero
if (number != 0) { // outer if
if (number > 0) { // inner if
printf("%d is positive.\n", number);
} else {
printf("%d is negative.\n", number);
}
} else {
printf("The number is zero.\n");
}
return 0;
}
Steps:
- Include the standard input-output library.
- Declare an integer variable
number
. - Prompt the user to enter a number and read the input using
scanf
. - Use a nested
if
structure:- First, check if
number
is not equal to zero (outerif
).- Inside this, check if
number
is greater than zero (innerif
).- If true, print that the number is positive.
- If false, print that the number is negative.
- Inside this, check if
- If the outer
if
condition is false (i.e., the number is zero), print that the number is zero.
- First, check if
4. Using the switch-case
statement
The switch-case
statement is used to execute one block of code among many different alternatives based on the value of a variable.
Example: Determine if a day is a weekday or weekend
#include <stdio.h>
int main() {
int day;
// Ask the user to enter a number corresponding to a day of the week
printf("Enter a number (1-7) corresponding to a day of the week: ");
scanf("%d", &day);
// Determine if the day is a weekday or weekend
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
printf("Day %d is a weekday.\n", day);
break;
case 6:
case 7:
printf("Day %d is a weekend.\n", day);
break;
default:
printf("Invalid input, please enter a number between 1 and 7.\n");
}
return 0;
}
Steps:
- Include the standard input-output library.
- Declare an integer variable
day
. - Prompt the user to enter a number corresponding to a day of the week (1 = Monday, 7 = Sunday) and read the input using
scanf
. - Use a
switch-case
statement to determine based on the value ofday
:- If
day
is 1, 2, 3, 4, or 5, it's a weekday. - If
day
is 6 or 7, it's a weekend. - If
day
is not between 1 and 7, print an error message.
- If
Top 10 Interview Questions & Answers on C Programming Decision Making Statements if, if else, nested if, switch case
Top 10 Questions and Answers on C Programming Decision Making Statements
Answer: Decision making in C programming allows a program to execute different sets of statements based on certain conditions. The primary decision-making statements in C are if
, if-else
, nested if
, and switch-case
.
2. How does the if
statement work in C?
Answer: The if
statement tests a specific condition. If this condition evaluates to true
(non-zero), the code within the if
block is executed. If the condition evaluates to false
(zero), the code within the if
block is skipped.
if (condition) {
// Code to execute if condition is true
}
3. Explain the if-else
statement in C with an example.
Answer: The if-else
statement is used when you want to execute a block of code if a certain condition is true, and another block of code if that condition is false.
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example:
int num = 10;
if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
4. Describe the nested if
statement in C.
Answer: A nested if
statement is when one if
statement is placed inside another if
statement. This structure is useful when more than one condition needs to be evaluated.
if (condition1) {
if (condition2) {
// Code to execute if both condition1 and condition2 are true
}
}
Example:
int age = 25;
char sex = 'M';
if (age >= 18) {
if (sex == 'M') {
printf("You are an adult male.\n");
}
}
5. What is the purpose of the switch-case
statement in C programming?
Answer: The switch-case
statement is used to execute one of many different code blocks based on the value of a variable. It's a more efficient alternative to if-else
statements when you have multiple conditions to check against the same variable.
switch(variable) {
case constant1:
// Code to execute when variable equals constant1
break;
case constant2:
// Code to execute when variable equals constant2
break;
// You can have any number of case statements.
default:
// Code to execute when variable does not match any of the above cases
}
6. Can the switch
statement be used with float
or double
data types in C?
Answer: No, the switch-case
statement can only be used with integral data types such as int
, char
, and enum
. Floating-point data types like float
or double
cannot be used in a switch
statement in C.
7. What is the purpose of the break
statement in a switch-case
block?
Answer: The break
statement is used to exit a switch-case
block once one of the cases has been executed. Without a break
, the program will continue executing subsequent cases, even if the conditions are not met, which is known as "fall-through."
Example:
int number = 2;
switch(number) {
case 1:
printf("Number is 1\n");
break;
case 2:
printf("Number is 2\n");
break;
default:
printf("Number is not 1 or 2\n");
}
8. Can the default
case in a switch-case
statement be omitted?
Answer: Yes, the default
case can be omitted in a switch-case
statement if there is no need to handle cases that do not match any of the case
labels.
int choice = 3;
switch(choice) {
case 1:
printf("Choice is 1\n");
break;
case 2:
printf("Choice is 2\n");
break;
// default case is omitted
}
9. What happens if no break
statement is used in a switch-case
block?
Answer: If no break
statement is used after a case
is executed, the program execution will continue into the next case, regardless of whether the condition matches, until a break
statement is encountered or the end of the switch
block is reached. This behavior is known as "fall-through."
10. How can you implement nested switch-case
statements in C?
Answer: Nested switch-case
statements can be used when you need to make decisions based on multiple variables. The switch
block of an outer case
can contain another switch-case
block.
Login to post a comment.