C If Else

In C programming language, if statement is used to execute a block of code if a particular condition is true. If the condition is false, the block of code is skipped, and the program continues to execute the statements that follow the if block.

The if statement can be followed by an optional else statement, which is executed if the condition of the if statement is false.

Here's the syntax for if-else statement in C programming language:

 
if (condition) {
    // code to be executed if the condition is true
}
else {
    // code to be executed if the condition is false
}

The condition in the above syntax is a boolean expression that evaluates to either true or false.

Here's an example of if-else statement in C programming language:

#include <stdio.h>

int main() {
    int num = 10;
    
    if (num > 0) {
        printf("The number is positive\n");
    }
    else {
        printf("The number is negative\n");
    }
    
    return 0;
}
 

In the above example, we have declared an integer variable num and assigned a value of 10 to it. We have used an if-else statement to check if num is positive or negative, and printed the appropriate message.

Note that if the if statement has only one statement to execute, we can omit the curly braces {}. However, it is always a good practice to use the curly braces to avoid any confusion or error in the code.

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

63593