# Conditional Statements In C.

If you have completed my last tutorials then you are ready to move some difficult but very important parts of c programming. So, today’s C programming tutorial for beginners post will be for you about conditional statements in C. Also you will learn about what is the conditional operator with examples. So, let’s get started.

Post Contents

### What Is Conditional Statement With Example?

In our real life, we have to face so many problems and we have to define some things depending on one or more conditions.

Suppose, I told you to go to your friend’s house and then watch a movie with him in the cinema hall.

You see in the whole story each of the next steps goes depending on different conditions. In our programming life we will have to face so many problems which depend on various conditions like the above. And we have to solve them using our programming language. For solving these kinds of problems we have to use conditional statements. In C there are actually four types of conditional statements these are as follows:

### 4)go to statement.

In the next tutorials, we will learn about numbers 1,2, and 4th types of conditional statements.
Now, we will learn and try to write a simple program using a conditional operator.

### Conditional Operator With Example:

The conditional operator works upon 3 operands.
Syntax:

exp1?exp2:exp3
Here exp1 is the condition, and exp2 is the statement that is performed if exp1 is true. And exp3 performs if the exp1 is false. Look at the following code.

#include <stdio.h>
int main ()
{
int x,y;
scanf(“%d%d”,&x,&y);
y=(y>=x)?y-x:y+x;

printf(“%dn”,y);

return 0;
}

Sample input: 9 19
Output: 10
Sample input: 19 9
Output: 28

Here, at first, we take 2 variables as input x and y. Then we check if y is larger than or equal to x then the current value of y is replaced by x-y. If it is not then the current value of y is replaced by x+y.

Here, (x>y)? at first check the condition then after the ? means y-x execute if the condition is true. Otherwise after the : means y+x execute and returns the value to y. And by the printf function, we just print the value of y after the operation.

I hope you understand the conditional operator. Now try to solve the following problem using the conditional operator by yourself.

Write a program to take an integer value x as input range from -20000 to +200000 then check is it an even number or an odd number? If it is even then print even otherwise print odd.

N.B. if the reminder of any number after divided by 2 is 0 (n%2==0) then it is even otherwise it is an odd number.
Try to solve this by yourself and post your code in the comment box. In the next tutorial, I will try to discuss all types of if statements with examples.