Chapter 03 · C Programming Series
If-Else in C
In C programming, the if-else statement is a fundamental control structure that allows you to execute different blocks of code based on the evaluation of a condition.
Syntax of if-else
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
How it works
- Condition : The condition is an expression that evaluates to either true (non-zero) or false (zero).
- If Block : If the condition evaluates to true, the code inside the if block is executed.
- Else Block : If the condition evaluates to false, the code inside the else block is executed.
Example
C Code
#include <stdio.h>
main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
if (n > 0) {
printf("The number is positive");
} else {
printf("The number is negative.");
}
}
Output
Enter an integer: 5
The number is positive
Enter an integer: -5
The number is negative.
Enter an integer: 5
The number is positive
Enter an integer: -5
The number is negative.
Explanation :
n > 0
If the condition is true, the first block of code is executed.
If the condition is false, the else block of code is executed.
Nested if-else
Nested if else statements are used to perform different actions based on multiple conditions.
Example of nested if-else
C Code
#include <stdio.h>
main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0) {
printf("The number is positive.");
} else if (number == 0) {
printf("The number is zero.");
} else {
printf("The number is negative.");
}
}
Output
Enter a number: 5
The number is positive.
Enter a number: 0
The number is zero.
Enter a number: -7
The number is negative.
Enter a number: 5
The number is positive.
Enter a number: 0
The number is zero.
Enter a number: -7
The number is negative.
Problems & Solutions
Write a program to find largest of two numbers
#include<stdio.h>
main()
{
int a,b;
printf("Enter the two no : ");
scanf("%d%d",&a,&b);
if(a>b)
printf("Largest=%d",a);
else
printf("Largest=%d",b);
}
Output
Enter the two no : 5
7
Largest=7
Enter the two no : 5
7
Largest=7
Write a program to accept a number and check whether the number is Automorphic n
#include<stdio.h>
main()
{
int n;
printf("Enter the value:");
scanf("%d",&n);
if(n*n%10==n || n*n%100==n || n*n%1000==n)
printf("Automorphic no",n);
else
printf("Not=%d",n);
}
Output
Enter the value:25
Automorphic no
Enter the value:25
Automorphic no
Write a program to accept a Character and check whether it is a Vowel or Consona
#include <stdio.h>
main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
printf("%c is a vowel.", c);
} else {
printf("%c is a consonant.", c);
}
}
Output
Enter a character: s
s is a consonant.
Enter a character: a
a is a vowel.
Enter a character: s
s is a consonant.
Enter a character: a
a is a vowel.
Importance of if-else
- Flexibility : The if-else statement provides the flexibility to handle various scenarios within a program.
- Decision Making : It is essential for decision-making processes, allowing programs to respond to different conditions and inputs.
- Readability and Maintainability : Proper use of if-else statements can enhance the readability and maintainability of the code, making it easier to understand and modify.
Conclusion
Understanding and using if-else statements effectively allows you to create more complex and responsive programs by making decisions based on different conditions.