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 statement used in programming to make decisions based on certain conditions. It allows the program to execute different blocks of statements based on whether a given condition is true or false. The general syntax of an "if-else" statement is as follows:
in this example we find entered value is positive or negative if the entered value is positive than postive_value set to 1 and negative_value set to zero. or if the entered value is negative than negative_value set to one and positive value set to zero. In this example, program first check if condition, if the condition is false than else statement executed.
above is the simple example of IF Statement in ANSI C program, in this example if the water level is more than 12.45 than low alarm signal set to true. in this example else condition is not used so code execute only if condition is true. once the alarm is set true and water level change and below to 12.45 than alarm is not reset because we don't declare any reset condition.
Logical operators are used to combine two or more condition and evaluate them as a single expression. AND, OR, NOT are main logical operator. Below is the example of logical operator in if else statement In ANSI C. in this example we evaluate a two momentary push button and motor on output to single expression. it is a example of motor start stop using a start stop push button in ansi C. || is logical Or operator, && logical and operator and ! logical not operator.
we can use multiple if else statement in our program. Multiple if else statements are used when we have several conditions and execute different blocks of code depending on which condition is true. below is the example of Multiple if else statements In ANSI C if water level of tank from 0.20 to 1.0 than motor 1 and motor 2 start if the water level between 1.01 to 4.0 than only motor1 start and motor 2 stop if the water level more 4.0 than all motor stop and exhaust valve open for maintain water level.
Nested if else statements are used when we want to check a condition inside another condition and execute different blocks of code depending on the outcome of both conditions. Below is the example of nested if else statement B&R automation studio in ANSI C if the first limit switch is operated than gate 1 open command set to 1 and feeding motor stop. we define a condition for check gate1 open status inside the limit switch if condition. if the gate 1 open status is true than gate1_open_sts tag set to 1 otherwise else statement execute condition and set gate1_open_sts tag set to 0.
if the limit switch not operated than feeding motor continue to run until the limit switch not operated. Nested if else and multiple if else statement are different in multiple if else statement if the first condition is not true than second if condition execute if all condition is false than finally else statement executed. in a nested if else statement condition inside condition.