
If-Else
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.
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
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.
Output :
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 statements are used to perform different actions based on multiple conditions.
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.
Output :
Enter the two no : 5
7
Largest=7
Output :
Enter the value:25
Automorphic no
Output :
Enter a character: s
s is a consonant.
Enter a character: a
a is a vowel.
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.
Understanding and using if-else statements effectively allows you to create more complex and responsive programs by making decisions based on different conditions.