C programming - if else statement

In C programming, the if...else statement is used for decision-making purposes. It allows the program to execute different statements based on a condition. The basic syntax of the if...else statement is as follows:

if (condition)
{
    // execute these statements if condition is true
}
else
{
    // execute these statements if condition is false
}
S‮uo‬rce:www.theitroad.com

Here, the condition is an expression that evaluates to either true or false. If the condition is true, the statements within the first block are executed, otherwise, the statements within the second block (i.e. the else block) are executed.

In some cases, it may be necessary to test multiple conditions. In such cases, the if...else statement can be extended with else if clauses. Here's an example:

if (condition1)
{
    // execute these statements if condition1 is true
}
else if (condition2)
{
    // execute these statements if condition1 is false and condition2 is true
}
else
{
    // execute these statements if both condition1 and condition2 are false
}

In this example, if condition1 is true, the statements within the first block are executed. If condition1 is false and condition2 is true, the statements within the second block are executed. Finally, if both condition1 and condition2 are false, the statements within the third block are executed.

It's important to note that the if...else statement only executes one of the two blocks. If you want to execute multiple statements within a block, you need to use curly braces to create a compound statement.