Conditional statements in C play a vital role in controlling the flow of a program, allowing it to make decisions based on given conditions. Imagine needing to calculate the sum of numbers from 1 to a specific number, but only if that number is greater than 1. Here, a conditional statement would help us verify if the number meets this criterion, guiding the program’s flow based on the input.
In this blog, we’ll dive into how conditional statements—like if
, else
, else if
, switch
, and the ternary operator—empower decision-making in C programming. We’ll examine their structures, explore practical examples, and discuss their applications, from error handling to program flow control. By understanding these tools, you’ll gain the ability to build more responsive, efficient, and organized C programs. Whether you’re a beginner or brushing up on C, this guide will help you harness the power of conditional statements for smart coding decisions.
-making decisions
Example:
suppose I want to perform the sum of addition of given number starting from 1
we will be checking the conditions whether the entered number is greater than 1
Example1 :
#include<stdio.h>
int main()
{
int sum=0;
printf(“enter the number “);
scanf(“%d”,&num);
if(num>1 &&num<100) // here we are making decision by using the condition
{
for(int i=0;i<num;i++)
{
sum+=num;
}
}
Example 2:
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = ‘5’;
if (isdigit(ch)) # here the decision making is used
{
printf(“‘%c’ is a digit.\n”, ch);
} else {
printf(“‘%c’ is not a digit.\n”, ch);
}
return 0;
}
Example 3:
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i = 0, is_alphanumeric = 1;
// Get user input
printf(“Enter a string: “);
fgets(str, sizeof(str), stdin);
// Check if the string is alphanumeric
while (str[i] != ‘\0’ && str[i] != ‘\n’) {
if (!isalnum(str[i])) {
is_alphanumeric = 0;
break;
}
i++;
}
if (is_alphanumeric) {
printf(“The string is alphanumeric.\n”);
} else {
printf(“The string is not alphanumeric.\n”);
}
return 0;
}
The if…else statement in C is a basic control flow statement used to execute a block of code based on whether a specified condition is true or false. It helps in making decisions in the code, allowing it to branch into different paths.
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
#include <stdio.h>
int main() {
int age;
printf(“Enter your age: “);
scanf(“%d”, &age);
if (age >= 18) {
printf(“You are eligible to vote.\n”);
} else {
printf(“You are not eligible to vote.\n”);
}
return 0;
}
Example 2:
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = ‘5’;
if (isdigit(ch)) # here the decision making is used
{
printf(“‘%c’ is a digit.\n”, ch);
} else {
printf(“‘%c’ is not a digit.\n”, ch);
}
return 0;
}
}
The if…else if statement in C (and many other programming languages) allows you to create branching logic where different conditions can lead to different outcomes. This structure is useful when you need to check multiple conditions and take different actions based on which condition is true.
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else if (condition3) {
// Code to execute if condition3 is true
} else {
// Code to execute if none of the above conditions are true
}
#include <stdio.h>
int main() {
int tot;
printf(“Enter your tot: “);
scanf(“%d”, &tot);
if (tot >= 90) {
printf(“Grade: A\n”);
} else if (tot >= 80) {
printf(“Grade: B\n”);
} else if (tot >= 70) {
printf(“Grade: C\n”);
} else if (tot >= 60) {
printf(“Grade: D\n”);
} else {
printf(“Grade: F\n”);
}
return 0;
}
It’s useful when you have multiple conditions to check based on a single value. Here’s the basic syntax:
switch (expression) {
case constant1:
break;
case constant2:
break;
default:
break;
}
#include <stdio.h>
int main() {
char operator;
int num1, num2;
printf(“Enter an operator “);
scanf(” %c”, &operator);
printf(“Enter two numbers: “);
scanf(“%d %d”, &num1, &num2);
switch (operator) {
case ‘+’:
printf(“%d”num1 + num2);
break;
case ‘-‘:
printf(“%d” num1 – num2);
break;
case ‘*’:
printf(“%d “ num1 * num2);
break;
case ‘/’:
if (num2 != 0)
printf(“%d”, num1 / num2);
else
printf(“Division by zero error!\n”);
break;
default:
printf(“Invalid operator!\n”);
}
return 0;
}
The ternary operator, also known as the conditional operator, is a shorthand way to perform conditional checks in programming languages like C, C++, Java, and Python. It is a compact alternative to an if-else statement
condition ? expression_if_true : expression_if_false;
Suppose you want to find the maximum of two numbers using a ternary operator:
int a = 10, b = 20;
int max = (a > b) ? a : b;
printf(“%d”,max);
Indian Institute of Embedded Systems – IIES