Summary: in this tutorial, you will learn how to use C if else statement to control the flow of the program based on a given condition.
C if statement
The if statement allows you to control the execution of code based on a particular condition. The syntax of the if statement is as follows:
1 2 3 | if(expression){ /* unit of code to be executed */ } |
The body of the if statement only executes if the expression evaluates to a non-zero value or true. If the expression evaluates to false, the statements inside the body of the if statement will be ignored.
The following flowchart illustrates the if statement:
Let’s take a look at the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h> #include <stdlib.h> int main() { int x; printf("Please enter a number:"); scanf ("%d",&x); if(x > 0){ printf("\nThe number %d is greater than 0",x); } return 0; } |
The program asked users to enter a number. If the number is greater than zero, it displays a message showing that otherwise, it does nothing.
C if else statement
Sometimes you want to execute a piece of code in case of the expression in the if statement evaluates to false. You can use the second form of the if statement which is known as if else statement. The following illustrates the syntax of the if else statement:
1 2 3 4 5 | if(expression){ /* code block of if statement */ }else{ /* code block of else statement */ } |
The following example demonstrates how to use the if else statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h> #include <stdlib.h> int main() { int x; printf("Please enter a number:"); scanf ("%d",&x); if(x > 0) { printf("\nThe number %d is greater than 0",x); } else { printf("\nThe number %d is less than or equal zero",x); } return 0; } |
This program asks users to enter a number. It displays the corresponding message based on the input value.
C else if statement
If you want to use more than one condition, you can use if else-if statement. The syntax of the if else if statement is as follows:
1 2 3 4 5 6 7 8 9 | if(condition-1){ /* code block if condition-1 is true */ }else if (condition-2){ /* code block if condition-2 is true */ }else if (condition-3){ /* code block if condition-3 is true */ }else{ /* code block all conditions above are false */ } |
The else-if statement is used to make multiway decisions. The conditions in the corresponding if and else if branch evaluates in sequence from top to bottom. If a condition evaluates to true, the statements associated with it executes and terminates the whole chain.
The following flowchart illustrates the if else statement:
You can have as many branches as you want but it is not recommended because the code looks less readable and difficult to maintain.
The following example compares the input number with zero (0) and displays the corresponding message:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h> #include <stdlib.h> int main() { int x; printf("Please enter a number:"); scanf ("%d",&x); if(x > 0){ printf("%d > 0",x); }else if(x < 0){ printf("%d < 0",x); }else{ printf("zero"); } return 0; } |
Putting it all together
We will use the if else statement to develop a simple currency conversion program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include <stdio.h> /* predefined exchange rates */ #define usd_to_euro 0.77 #define usd_to_aus 0.96 #define usd_to_yen 95.90 #define usd_to_yuan 6.21 #define usd_to_rupee 54.32 int main() { float usd_amount; /* USD amount*/ float fc_amount; /* Foreign currency amount*/ float rate; int currency; /* Enter amount in USD */ printf("Please enter USD amount:\n",&usd_amount); scanf("%f",&usd_amount); printf("Please choose the currency:\n"); printf("1 - Euro.(%f)\n",usd_to_euro); printf("2 - Australia Dollar.(%f)\n",usd_to_aus); printf("3 - Japanese Yen.(%f)\n",usd_to_yen); printf("4 - Chinese Yuan.(%f)\n",usd_to_yuan); printf("5 - Indian Rupee.(%f)\n",usd_to_rupee); printf("6 - Other\n"); scanf("%d",¤cy); /* determine the rate */ if(currency == 1) rate = usd_to_euro; else if(currency == 2) rate = usd_to_aus; else if(currency == 3) rate = usd_to_yen; else if(currency == 4) rate = usd_to_yuan; else if(currency == 5) rate = usd_to_rupee; else{ printf("Enter the exchange rate USD -> Foreign Currency\n"); scanf("%f",&rate); } /* Convert currency */ if(rate > 0){ fc_amount = usd_amount * rate; } printf("Result: %.2f",fc_amount); return 0; } |
How it works.
- First, we define some popular exchange rates using constants such as USD to Euro, USD to Yen.
- Second, we ask users to input USD amount and choose the corresponding currency.
- Third, based on the user choice from the menu from 1 to 5, we calculate the foreign currency amount. If users entered unknown currency, the program asks the user to enter the exchange rate before doing the calculation.
In this tutorial, we have shown you how to use various forms of the C if else statement to control the flow of the program based on a given condition.