Conditions and If Statements
C supports the usual logical conditions from mathematics:
Less than or equal to a <= b
Greater than: a > b
Less than a < b
Greater than or equal to: a >=
b
Equal to a == b
Not Equal to: a != b
C has the following conditional statements:
Conditions and If Statements 1
Use if to specify a block of code to be executed, if a specified condition is
true
Use else to specify a block of code to be executed, if the same condition is
false
Use else if to specify a new condition to test, if the first condition is false
Use switch to specify many alternative blocks of code to be executed
The if Statement
Use the if statement to specify a block of code to be executed if a condition is
true .
Syntax :
if (condition) {
// block of code to be executed if the condition is true
}
💡 Note that if is in lowercase letters. Uppercase letters (If or IF) will
generate an error.
Example:
#include <stdio.h>
int main() {
int x = 20;
int y = 18;
if (x > y)
{
printf("x is greater than y");
}
Conditions and If Statements 2
return 0;
}
The else Statement
Use the else statement to specify a block of code to be executed if the condition
is false .
Syntax:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example:
#include <stdio.h>
int main()
{
int time = 20;
if (time < 18)
{
printf("Good day.");
}
else
{
printf("Good evening.");
}
return 0;
}
The else if Statement
Conditions and If Statements 3
Use the else if statement to specify a new condition if the first condition is false .
Syntax:
if (condition1)
{
// block of code to be executed if condition1 is true
}
else if (condition2)
{
// block of code to be executed if the condition1 is false and
}
else
{
// block of code to be executed if the condition1 is false and
}
Example:
#include <stdio.h>
int main()
{
int time = 22;
if (time < 10)
{
printf("Good morning.");
}
else if (time < 20) {
printf("Good day.");
}
else
{
printf("Good evening.");
}
Conditions and If Statements 4
return 0;
}
The multiple else if Statement
#include <stdio.h>
int main()
{
int marks = 91;
if (marks <= 100 && marks >= 90)
printf("A+ Grade");
else if (marks < 90 && marks >= 80)
printf("A Grade");
else if (marks < 80 && marks >= 70)
printf("B Grade");
else if (marks < 70 && marks >= 60)
printf("C Grade");
else if (marks < 60 && marks >= 50)
printf("D Grade");
else
printf("F Failed");
return 0;
}
C Switch Statement:
Instead of writing many if..else statements, you can use the switch statement.
The switch statement selects one of many code blocks to be executed:
Syntax:
switch(expression)
{
case x:
Conditions and If Statements 5
// code block
break;
case y:
// code block
break;
default:
// code block
}
This is how it works:
The switch expression is evaluated once
The value of the expression is compared with the values of each case
If there is a match, the associated block of code is executed
The break statement breaks out of the switch block and stops the execution
The default statement is optional, and specifies some code to run if there is
no case match
Example
#include <stdio.h>
int main()
{
int day = 4;
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
Conditions and If Statements 6
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
}
return 0;
}
The break Keyword
When C reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need
for more testing.
The default Keyword
The default keyword specifies some code to run if there is no case match:
#include <stdio.h>
int main() {
int day = 4;
switch (day) {
Conditions and If Statements 7
case 6:
printf("Today is Saturday");
break;
case 7:
printf("Today is Sunday");
break;
default:
printf("Looking forward to the Weekend");
}
return 0;
}
Example 1: Simple Calculator
// Program to create a simple calculator
#include <stdio.h>
int main() {
char operation;
double n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operation);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);
switch(operation)
{
case '+':
printf("%f + %f = %f",n1, n2, n1+n2);
break;
Conditions and If Statements 8
case '-':
printf("%f - %f = %f",n1, n2, n1-n2);
break;
case '*':
printf("%f * %f = %f",n1, n2, n1*n2);
break;
case '/':
printf("%f / %f = %f",n1, n2, n1/n2);
break;
// operator doesn't match any case constant +, -, *, /
default:
printf("Error! operator is not correct");
}
return 0;
}
Output:
Enter an operator (+, -, *, /): -
Enter two operands: 32.5
12.4
32.5 - 12.4 = 20.1
Example 2:Program to check vowel or
consonant using switch case
Conditions and If Statements 9
#include <stdio.h>
int main()
{
char ch;
/* Input an alphabet from user */
printf("Enter any alphabet: ");
scanf("%c", &ch);
/* Switch value of ch */
switch(ch)
{
case 'a':
printf("Vowel %c",ch);
break;
case 'e':
printf("Vowel %c",ch);
break;
case 'i':
printf("Vowel %c",ch);
break;
case 'o':
printf("Vowel %c",ch);
break;
case 'u':
printf("Vowel %c",ch);
break;
case 'A':
printf("Vowel %c",ch);
break;
case 'E':
printf("Vowel %c",ch);
break;
case 'I':
Conditions and If Statements 10
printf("Vowel %c",ch);
break;
case 'O':
printf("Vowel %c",ch);
break;
case 'U':
printf("Vowel %c",ch);
break;
default:
printf("Vowel %c",ch);
}
return 0;
}
Conditions and If Statements 11