B&R Automation Studio IF ELSE Statement in C Programming

Published on May22, 2023 | Category: C Programming
Share this article:

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.

B&R Automation Studio Syntax of an "if-else" Statement

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:

B&R Automation Studio Conditional IF ELSE Example in ANSI C

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.

img/brautoifelse/br-automation-studio-conditional-if-else-example-in-ansi-c.webp

B&R Automation Studio IF Statement Example

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.

img/brautoifelse/br-automation-studio-conditional-if-statement-example.webp

B&R Automation Studio Logical Operators in IF ELSE Statement in ANSI C

Logical operators allow combining multiple conditions into a single expression. The common logical operators used in C are:

Below is an example of using logical operators in an if-else condition to implement a motor start/stop control using push buttons.

img/brautoifelse/br-automation-studio-use-of-logical-operator-in-if-else-statement-in-ansi-c.webp

B&R Automation Studio Multiple IF ELSE Statements in ANSI C

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:

img/brautoifelse/br-automation-studio-multiple-if-else-statements-in-ansi-c.webp

B&R Automation Studio Nested IF ELSE Statements in ANSI C

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:

If the limit switch is not operated, the feeding motor continues to run until the condition is met.

img/brautoifelse/br-automation-studio-nested-if-else-statements-in-ansi-c.webp

Nested if-else and multiple if-else structures differ in design:

Using these logical structures allows better control and accurate decision-making in automation systems.