Learn C Programming
ifโฆelse Statement
The C programming language provides the ifโฆelse statement to perform the operations based on some certain conditions. It allows you to execute different blocks of code depending on whether a given condition is true or false.
The syntax of the ifโฆelse statement is as follows:
if (condition)
{
// Code block to be executed if the condition is true
}
else
{
// Code block to be executed if the condition is false
}
Here is how an if else statement in C works:
- The condition is a logical expression that evaluates to either true or false. The code block within the first set of curly braces following the if statement executes when the condition is true. If the condition is false, the code block within the else statement (if present) executes.
- The ifโฆelse statement has code blocks enclosed in curly braces {}. If a code block has only one statement, you can omit the braces, but itโs better to include them for clarity and to avoid problems when adding more statements later.
- You can also nest ifโฆelse statements inside each other to handle more complex decision-making scenarios.
In the C programming language, there are several variants of the if statement that you can use to control the flow of your program based on certain conditions. Here are the commonly used variants:
If Statement:
The basic if statement allows you to execute a block of code if a given condition is true.
Syntax:if (condition) { // Code to be executed if the condition is true }
How it works:ย
The program checks if the condition is true, and if it is, it runs the code inside the curly braces. If the condition is false, the program skips the code block and continues to the next statement after the if block.If-else Statement:
The if-else statement extends the basic if statement by providing an alternative block of code to execute when the condition is false.
Syntax:if (condition) { // code to be executed if the condition is true } else { // code to be executed if the condition is false }
How it works:
If the condition is true, the code block inside the curly braces of the if statement runs. If the condition is false, the code block inside the curly braces of the else statement runs instead. Only one of the two code blocks runs, depending on the condition.If else Ladder:
An ifโฆelse ladder helps to check multiple conditions in a sequence. It evaluates each condition one by one and executes the code block associated with the first true condition.
Syntax:if (condition1) { // Code to be executed if condition1 is true } else if (condition2) { // Code to be executed if condition2 is true } else if (condition3) { // Code to be executed if condition3 is true } ... else { // Code to be executed if none of the conditions are true }
How it works:
We evaluate the conditions one by one in the order they are given. First, we check if a condition is true. If it is, we execute the code block associated with it and then skip the rest of the ladder. If none of the conditions are true, however, we move on to the next condition. Finally, if none of the conditions are true, we execute the code block within the else statement.Nested ifโฆelse:
Nested ifโฆelse statements allow you to place an ifโฆelse statement inside another if or else block. This allows for more complex decision-making logic where conditions need to be evaluated in a hierarchical manner.
In the nested ifโฆelse structure, the inner if statement is only evaluated if the outer if statementโs condition is true. The inner if statement can also have its own else clause.
Syntax:if (condition1) { if (condition2) { /* Code to be executed if both condition1 and condition2 are true */ } else { /* Code to be executed if condition1 is true but condition2 is false */ } } else { // Code to be executed if condition1 is false }
How it works:
First, the code checks if the condition of the outer if statement is true. If it is, then it proceeds to evaluate the condition of the inner if statement. If both conditions are true, the code executes the corresponding block of code. However, if the outer condition is true but the inner condition is false, the code executes the else block within the outer if statement. On the other hand, if the outer condition is false, the code executes the else block outside the inner if statement.
In simple terms, the different variants of the if statement in C offer flexibility when it comes to making decisions based on certain conditions. Additionally, these variants allow for the implementation of various scenarios by evaluating different conditions.