If statement will only perform if the condition inside the parentheses is evaluated to true. if statements are used if are used to start a process or system at specific condition if condition is true. if statements compares two or more than two expression result if the result is true than condition inside the parentheses is executed. if the condition is false than else statement executed.
The "if-else" statement is a conditional control structure used in programming to make decisions based on specific conditions. It allows the program to execute different blocks of code depending on whether a given condition is true or false. The general syntax of an "if-else" statement in ANSI C is as follows:
In this example, the program checks if the entered value is positive or negative. If the value is positive, the positive_value is set to 1 and negative_value is set to 0. If the value is negative, the logic reverses. The program first evaluates the if condition. If it evaluates to false, the else block is executed.
The following example demonstrates a simple if condition in ANSI C. It checks if the water level is above 12.45. If the condition is true, the low_alarm signal is set to true. This example does not include an else block, so no action is taken when the condition is false. Note that the alarm remains active even if the level drops later, as there is no reset condition declared.
Logical operators allow combining multiple conditions into a single expression. The common logical operators used in C are:
Multiple if-else statements are used when evaluating more than two conditions. Depending on which condition is satisfied, a different block of code is executed. In the example below:
Nested if-else statements are used to evaluate a condition inside another if condition. This structure helps when decision-making depends on multiple, hierarchical criteria.
In the example shown:
Nested if-else and multiple if-else structures differ in design: