### If and else in C Programming

# If and else in C Programming

Video content of Introduction to C Programming Season 1, Chapter 4 Can we found at Amazon, YouTube.

In this chapter we will study If and else in C Programming.

Till now whatever examples you have written they were plain simple statements. Now we will study conditional programming. We generally call it if and else statement.

When we talk about conditions it like less than, greater, equal and many others.

## Take an example

Suppose you run a shop which offers a discount of 10% if the bill amount is greater than 1000 Dollars.

Now here the computer needs to make a decision based upon the bill amount.

Let’s write execute the program. As done earlier, we will write the main program,

main followed by curve brackets and we will. write our logic inside these curly brackets.

As per the logic, only input. We need is the total bill amount.

So, let’s declare a float variable for the bill amount,

```
main()
{
float bill_amount;
}
```

and an integer variable to store the discount. And again two variables to store final bill amount and actual discount.

```
main()
{
float bill_amount;
float discount;
float final_bill;
float actual_discount;
}
```

After declaring the variable we need to take bill amount as input. Let’s write a scanf statement for the same.

```
main()
{
float bill_amount;
float discount;
float final_bill;
float actual_discount;
printf("Calculate Discount Price");
printf("\nEnter Bill Amount : ");
scanf("%f",&bill_amount);
}
```

Here comes the important part. Condition to check whether the bill amount is greater than 1000 or not.

To write the condition we put if statement. “If” followed by curve brackets.

```
main()
{
float bill_amount;
float discount;
float final_bill;
float actual_discount;
printf("Calculate Discount Price");
printf("\nEnter Bill Amount : ");
scanf("%f",&bill_amount);
if()
}
```

We will have our bill amount in the variable bill_amount.

```
main()
{
float bill_amount;
float discount;
float final_bill;
float actual_discount;
printf("Calculate Discount Price");
printf(“\nEnter Bill Amount : ”);
scanf(“%f”,&bill_amount);
if(bill_amount)
}
```

In curve brackets, we will write bill_amount > 1000.

```
main()
{
float bill_amount;
float discount;
float final_bill;
float actual_discount;
printf("Calculate Discount Price");
printf("\nEnter Bill Amount : ");
scanf("%f",&bill_amount);
if(bill_amount > 1000)
}
```

Then we will have curly brackets after “if” and

```
main()
{
float bill_amount;
float discount;
float final_bill;
float actual_discount;
printf("Calculate Discount Price");
printf("\nEnter Bill Amount : ");
scanf("%f",&bill_amount);
if(bill_amount > 1000)
{
}
}
```

in that we will write discount =10.

```
main()
{
float bill_amount;
float discount;
float final_bill;
float actual_discount;
printf("Calculate Discount Price");
printf("\nEnter Bill Amount : ");
scanf("%f",&bill_amount);
if(bill_amount > 1000)
{
discount=10;
}
}
```

After curly brackets, we are writing else.

```
main()
{
float bill_amount;
float discount;
float final_bill;
float actual_discount;
printf("Calculate Discount Price");
printf("\nEnter Bill Amount : ");
scanf("%f",&bill_amount);
if(bill_amount > 1000)
{
discount=10;
}
else
{
}
```

Inside the else, we will write discount=0

```
main()
{
float bill_amount;
float discount;
float final_bill;
float actual_discount;
printf("Calculate Discount Price");
printf("\nEnter Bill Amount : ");
scanf("%f",&bill_amount);
if(bill_amount > 1000)
{
discount=10;
}
else
{
discount=0;
}
}
```

We will discuss this if statement in details. First, just write down our formula to calculate the discount.

So discount_amount = bill_amount * discount_percentage/100. you know this is a pretty basic logic.

```
main()
{
float bill_amount;
float discount;
float final_bill;
float actual_discount;
printf("Calculate Discount Price");
printf("\nEnter Bill Amount : ");
scanf("%f",&bill_amount);
if(bill_amount > 1000)
{
discount=10;
}
else
{
discount=0;
}
actual_discount = bill_amount * discount/100;
final_bill=bill_amount-actual_discount;
}
```

Then we will calculate the final amount. Now its time to display the result a bit nicely.

Inside printf, we will write Discount on Bill Amount %f is %d. Final Bill is %f. Then the name of respective variables.

```
main()
{
float bill_amount;
float discount;
float final_bill;
float actual_discount;
printf("Calculate Discount Price");
printf("\nEnter Bill Amount : ");
scanf("%f",&bill_amount);
if(bill_amount > 1000)
{
discount=10;
}
else
{
discount=0;
}
actual_discount = bill_amount * discount/100;
final_bill=bill_amount-actual_discount;
printf("\nDiscount on Bill Amount is %f \nactual discount is %f.\nFinal Bill is %f ",bill_amount, actual_discount, final_bill);
}
```

This program might look big for freshers, but believe me, it’s not even 0.1% of code that you will write in real life. Execute the program.

You can view the source code from Newtum github repository link over here.

## Output :

```
Calculate Discount Price
Enter Bill Amount : 2000
Discount on Bill Amount is 2000.000000 is 200.000000.
Final Bill is 1800.000000
```

Let’s run the program again,

## Output :

```
Calculate Discount Price
Enter Bill Amount : 900
Discount on Bill Amount is 900.000000 is 0.000000.
Final Bill is 900.000000
```

From the output we can see, the program calculates the final bill amount based on the bill amount.

Let’s try to understand the code. You know about few first lines that are the variable declaration, scanf, and printf statements. Please observe that we have chosen variable names so that they signify the purpose. Most of the new developers use variables like a,b,c but its not the right way of doing.

Then comes the if condition. If and else are keywords in C Programming.

Again telling you, keywords are reserved words which have their own meaning in C language. With scanf functions,

we have taken input in variable bill_amount.

IN curve brackets of if, we are writing condition whether bill amount is greater than 1000 or not.

The conditions give the output true or false. If the result is true logic inside curly brackets is executed if the result is false else part is executed.

We have given 2000 as input, the condition was successful, and it set the discount variable value =10. When the condition is true the computer never looks for else statement and its skipped.

With the true condition, the program first set the discount rate over this line and it directly jumps to expression to calculate the discount. With the condition, we can decide what needs to be executed and what not.After calculating the discount amount, it calculates the final amount.

Finally, the result is displayed. I guess you got some idea about if and else statements.

Now suppose, you enter value 1000,

## Output :

```
Calculate Discount Price
Enter Bill Amount : 1000
Discount on Bill Amount is 1000.000000 is 0.000000.
Final Bill is 1000.000000
```

The discount will be zero. Because let’s look at the condition it says bill_ammount > 1000. But 1000 > 1000 is false.

Now if you want to give a discount on an amount greater than and equal to 1000, you will have to modify the condition. The new condition will be bill_amount >=0.

```
main()
{
float bill_amount;
float discount;
float final_bill;
float actual_discount;
printf("Calculate Discount Price");
printf("\nEnter Bill Amount : ");
scanf("%f",&bill_amount);
if(bill_amount >= 1000)
{
discount=10;
}
else
{
discount=0;
}
actual_discount = bill_amount * discount/100;
final_bill=bill_amount-actual_discount;
printf("\nDiscount on Bill Amount is %f is %f.\nFinal Bill is %f ",bill_amount, actual_discount, final_bill);
}
```

Let’s execute the program again. Now we will give the input as 1000. Here is our output no discount at all.

Now again go back to the program and modify the condition like this. Its >= now.

Let’s execute the program again. Enter the bill amount as 1000. Look at the output,

the discount is applied.

## Output :

**Calculate Discount Price**

**Enter Bill Amount :**

**Output Screen :**

```
Calculate Discount Price
Enter Bill Amount : 1000
Discount on Bill Amount is 1000.000000 is 0.000000.
Final Bill is 1000.000000
```

Again I am telling you the computer will do your work, but it’s the software engineer who defines the logic.

If the computer is giving the wrong output people never shouts at the computer they run for the Software Developer.

Here we have seen how these two condition works, But

## C Programming supports the following conditions

- Greater than — x > y
- Greater than equal to — x>= y
- Less than — x < y
- Less than equal to — x<= y
- Equal to — x==y
- Not equal to — x!=y

We need to run a few more examples to get hold of if and else statement. Do you know

**what is odd and even numbers?**

Let me revise it for few. If a number is divisible by 2 it’s an even number, if not its called odd number.

Like 2,4,6,8 are even numbers , 1,3,5,7 are odd numbers. It’s simple. But now we need to make our computer to do this for you

## odd or even

Lets write a program for this. Aim of our program is to give a number to the computer, and he will say whether is odd or even. So start writing.

declare an integer variable to hold the input

```
main()
{
int a;
int remainder;
}
```

and one more variable to store the remainder,

```
main()
{
int a;
}
```

write a printf statement saying please enter a number,

```
main()
{
int a;
int remainder;
printf("Program to check number is Even or Odd");
printf("\nPlease Enter Number : ");
}
```

then we will have scanf to take input from the user.

```
main()
{
int a;
int remainder;
printf("Program to check number is Even or Odd");
printf("\nPlease Enter Number : ");
scanf("%d",&a);
}
```

Now is the trick, we need to tell computers to predict if this number is even or odd.

In a layman language, we need to know if a number is divisible by 2.

The number is divisible by 2 when the remainder is 0. So if we divide 6/2, the remainder is 0 .

```
main()
{
int a;
int remainder;
printf("Program to check number is Even or Odd");
printf("\nPlease Enter Number : ");
scanf("%d",&a);
}
```

Now C language has a special operator called mod. Its gives the output as remainder. We use the % symbol for that

Now we are writing an expression, a mod 2, and storing the result in remainder variable.

```
main()
{
int a;
int remainder;
printf("Program to check number is Even or Odd");
printf("\nPlease Enter Number : ");
scanf("%d",&a);
remainder = a %2;
}
```

Let’s print the remainder variable and run the program.

```
main()
{
int a;
int remainder;
printf("Program to check number is Even or Odd");
printf("\nPlease Enter Number : ");
scanf("%d",&a);
remainder = a %2;
printf("Remainder of a & 2 is : %d",remainder);
}
```

So we have our output screen.

## Output :

**Program to check number is Even or Odd**

**Please Enter Number :**

Let’s give the input as 8

## Output :

```
Program to check number is Even or Odd
Please Enter Number : 8
Remainder of a & 2 is 0
```

Run again.

Now give the input as 5,

## Output :

```
Program to check number is Even or Odd
Please Enter Number : 5
Remainder of a & 2 is 1
```

## Output :

```
Program to check number is Even or Odd
Please Enter Number : 8
Remainder of a & 2 is 1
```

If you find this difficult to understand you need to go to schooling book quickly. Well, you are learning

C over here, but the logic has to come from you.

Now we know, the remainder gives the result if the number is exactly divisible.

So we will take the remainder using this expression and will store the remainder in the variable “remainder”.

We will check whether remainder is equal to zero or not,

```
main()
{
int a;
int remainder;
printf("Program to check number is Even or Odd");
printf("\nPlease Enter Number : ");
scanf("%d",&a);
remainder = a %2;
printf("Remainder of a & 2 is : %d",remainder);
if (remainder==0)
}
```

If true we will print the statement that Number is even

```
main()
{
int a;
int remainder;
printf("Program to check number is Even or Odd");
printf("\nPlease Enter Number : ");
scanf("%d",&a);
remainder = a %2;
printf("Remainder of a & 2 is : %d",remainder);
if (remainder==0)
printf("\n%d is Even Number",a);
}
```

else we will print the statement that the number is odd.

```
main()
{
int a;
int remainder;
printf("Program to check number is Even or Odd");
printf("\nPlease Enter Number : ");
scanf("%d",&a);
remainder = a %2;
printf("Remainder of a & 2 is : %d",remainder);
if (remainder==0)
printf("\n%d is Even Number",a);
else
printf("\n%d is Odd Number",a);
}
```

Just observe here that I have not included curly brackets after if, let’s execute the program and test it.

You can view the source code from Newtum github repository link over here.

## Output :

```
Program to check number is Even or Odd
Please Enter Number : 3
Remainder of a & 2 is 1
3 is Odd Number
```

Now run again

## Output :

```
Program to check number is Even or Odd
Please Enter Number : 8
Remainder of a & 2 is 0
8 is Even Number
```

Now let’s study the condition. Here we have used == as the condition. This is used for matching the two parameters If you want to write a condition where two parameters are exactly the same we use == operator.

## Operator

Here we have to learn one more thing, if you want to write only one condition, then there is no use of curly brackets. If you want to write more than one line inside “if” then you need curly brackets.

Consider an example, if you want to write two statements, that number you entered is 12 and in the next line, that it’s odd.

It’s must for you to enclose the two statements in the curly brackets. If it’s one line you can ignore curly brackets.

Here you need to understand one more thing. That is, not equal to operator.

Let’s execute an example.

Suppose you want to check if the number is 5 or not.

Let’s write a small program for this.

We have main, let’s declare a variable to take the input.

```
main()
{
int a;
}
```

Now write printf and scanf.

```
main()
{
int a;
printf("Program to check Number is 5 or Not");
printf("\nPlease Enter Number : ");
scanf("%d",&a);
}
```

Now we need to write the condition. We need to check whether the number is 5 or not.

We will write a condition like this. !=

```
main()
{
int a;
printf("Program to check Number is 5 or Not");
printf("\nPlease Enter Number : ");
scanf("%d",&a);
if(a!=5)
printf("Entered Number is not 5");
else
printf("Entered Number is 5");
}
```

Let’s run the program.

## Output :

```
Program to check Number is 5 or Not
Please Enter Number : 6
Entered Number is not 5
```

Whenever you want to have the negative condition you need to use != operator.

So we can say that basic if and else statements consist of these 6 basic conditions. I.e x > y, x>=y , x<y , x<=y , x==y and x!=y. These are conditional operators.

Syntax of writing if and the else statement is, “if” followed by a condition in curve brackets and statement in curly brackets. Then else statement in curly brackets.

In “if” and “else”, for the true condition “if” part is executed otherwise “else” part is executed.

if We have more than one statement then we need curly brackets, otherwise no brackets are required in C Programming.

We will study more about “if” and “else” statement in the next chapters. Please please execute the programs and examples if you want to become a developer.

You can view the coding in following link { github.com/newtum.}

If you tune into our next chapters, you will definitely become a good developer else I am sure you are going to miss out a lot.

So C you in the next chapter.