C programs are executed in a sequence, but we can control the execution of program by using any control mechanism by which we can compare things and come to a decision. This involves using some operations called Relational Operators and conditional statements called if-else and loops. We use fundamental operators to compare two values in our C programs. The relational operators compare two values of any basic data type and return true or false after comparison.

These relational operators compare two values and return true or false after comparison. We can use the relational operators to compare values of any basic data type, so all we need is to modify the behavior of the program.

In C, there is another operator called ternary operator which is an expression instead of a statement i.e. you can have it on the right-hand side (RHS) of a statement. So you can write certain code statements more concisely.

The rest of the article is divided into the following main sections:

The if Statement

The if statement by itself will execute a single statement or a group of statements when the condition following if is true.

If the condition is false then a group of statements can be executed using else statement

The simple example of an if statement is:

We can also use the code block to specify the statements to be pre-executed if the given condition is true i.e.

The general form of if statement looks like this:

Here the keyword if tells the compiler that what follows, is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is true, then the statement is executed. It the condition is not true then the statement is not executed instead the program skips past it. The condition in C is evaluated using C’s relational operators. The relational operators help us to build expression which are either true or false

This expression is true if

Demonstration of if statement in C

Multiple statements within an if statement

If it is desired that more than one statement is to be executed if the condition is satisfied, then such statements must be placed within pair of braces. for e.g.

The following program demonstrate that if year of service greater than 3 then a bonus of Rs. 2500 is given to employee. The program illustrates the multiple statements used within if

The if-else Statement

The if statement by itself will execute a single statement or a group of statements when the condition following if is true. it does nothing when the condition is false. It the condition is false then a group of statements can be executed using else statement. The following program illustrates this

Nested if – else statement

It we write an entire if – else construct within the body of the if statement or the body of an else statement. This is called ?nesting? of if. For e.g.

What are the examples of Conditional Operators?

Condition operators are listed here. You can read more about conditional operators in our guide on Operators in C.

  1. < Less than
  2. > Greater than
  3. == Equal to
  4. <= Less than or equal to
  5. >= Greater than or equal to
  6. != Not equal to

If you want to further understand conditional statements,  then study the following C Programs: