| by Arround The Web | No comments

If Then Else Statement in C

The “if-then-else” statement in the C programming language is supposed to operate on different conditions. If the provided condition evaluates to true, then the “if” part of the statement is executed. Otherwise, the “else” part is executed. In this tutorial, we will try to learn the usage of the “if-then-else” statement in the C programming language with the help of a few examples.

Usage of the If Then Else Statement in the C Programming Language

The if-then-else statement in the C programming language is primarily used to decide based on a comparison. This is depicted in the following three examples:

Example # 1: The Equality Comparison

In this example, we will be using the equality operator with the “if-then-else” statement to see how it really works in this scenario. The C script for this example is shown in the image below:

In this program, we have defined an integer “binary” and have assigned to it the value “1”. After that, we have used the “if-then-else” statement for an equality comparison of this variable. After which, we will be able to tell whether the switch is on or off.

The command that follows is used to compile this C program:

$ gcc ifelse.c –o ifelse

To execute this compiled program, we will use the command shown below:

$ ./ifelse

Since the value assigned to our “binary” variable was equal to “1”, therefore, the “switch on” output appeared on the terminal as shown in the following image:

Example # 2: The Lesser Comparison

In this example, we will be using the “less than” comparison operator with the “if-then-else” statement. The image shown below presents the script that we will be using for this example:

In this example, we have defined two different integers. After that, we have compared their values by using the “less than” comparator in the C programming language. If the first number will be less than the second, then the “if” part of the statement will be executed. Otherwise, its “else” part will be executed.

Since the value of the first integer was lesser than the second one, hence the following output was displayed on the terminal:

Example # 3: The Inequality Comparison

In this example, we will use the inequality comparison for comparing the ages of two persons. The C script displayed below will explain this functionality:

In this example, we have defined the ages of two different persons as integers. Then, we have used the inequality comparison operator to find out if the two persons are of the same age or not with the help of an “if-then-else” statement in C.

Since the ages of the two persons were not equal, therefore the following output was printed on our terminal:

Conclusion

By going through this guide, you will easily be able to understand the working of the “if-then-else” statement in the C programming language. After that, it will not be a problem for you to write scripts in C comprising this particular statement.

Share Button

Source: linuxhint.com

Leave a Reply