0% found this document useful (0 votes)
7 views

Conditional Statements

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Conditional Statements

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Conditional Statements / Decision Making statements

Conditions and If Statements

You have already learned that C supports the usual logical conditions from mathematics:

 Less than: a < b


 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b
 Equal to a == b
 Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

C has the following conditional statements:

 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
The if in C is a decision-making statement that is used to execute a block of
code based on the value of the given expression.
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.
In the example below, we test two values to find out if 20 is greater than 18. If the condition
is true, print some text:
Example
if (20 > 18) {
printf("20 is greater than 18");
}

Example
int x = 20;
int y = 18;
if (x > y) {
printf("x is greater than y");
}

/ C Program to demonstrate the syntax of if statement


#include <stdio.h>

void main()
{

int gfg = 9;
// if statement with true condition
if (gfg < 10) {
printf("%d is less than 10", gfg);
}

// if statement with false condition


if (gfg > 20) {
printf("%d is greater than 20", gfg);
}

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
int time = 20;
if (time < 18) {
printf("Good day.");
} else {
printf("Good evening.");
}
// Outputs "Good evening."

// C Program to check if the number is even or odd


#include <stdio.h>
int main()
{
int n = 4956;

// condition to check for even number


if (n % 2 == 0) {
printf("%d is Even", n);

// condition to check for odd number


else {
printf("%d is Odd", n);

}
return 0;
}

The else if Statement


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 condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}

Example
int time = 22;
if (time < 10) {
printf("Good morning.");
} else if (time < 20) {
printf("Good day.");
} else {
printf("Good evening.");
}
// Outputs "Good evening."

C program to find maximum between two numbers


#include <stdio.h>
int main()
{
int num1, num2;

/* Input two numbers from user */


printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);

/* Compare num1 with num2 */


if(num1 > num2)
{
/* True part means num1 > num2 */
printf("%d is maximum", num1);
}
else
{
/* False part means num1 < num2 */
printf("%d is maximum", num2);
}

return 0;
}

C program to check Leap Year

#include <stdio.h>

int main()
{
int year;

/* Input year from user */


printf("Enter year : ");
scanf("%d", &year);

/*
* If year is exactly divisible by 4 and year is not divisible by 100
* or year is exactly divisible by 400 then
* the year is leap year.
* Else year is normal year
*/
if(((year % 4 == 0) && (year % 100 !=0)) || (year % 400==0))
{
printf("LEAP YEAR");
}
else
{
printf("COMMON YEAR");
}

return 0;
}

C program to check positive negative or zero using simple if


statement
#include <stdio.h>

int main()
{
int num;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

if(num > 0)
{
printf("Number is POSITIVE");
}
if(num < 0)
{
printf("Number is NEGATIVE");
}
if(num == 0)
{
printf("Number is ZERO");
}

return 0;
}

C program to check whether a character is alphabet or not


#include <stdio.h>

int main()
{
char ch;

/* Input a character from user */


printf("Enter any character: ");
scanf("%c", &ch);

if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("Character is an ALPHABET.");
}
else
{
printf("Character is NOT ALPHABET.");
}

return 0;
}

C program to check whether a character is vowel or consonant

#include <stdio.h>

int main()
{
char ch;

/* Input character from user */


printf("Enter any character: ");
scanf("%c", &ch);

/* Condition for vowel */


if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ||
ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
printf("'%c' is Vowel.", ch);
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
/* Condition for consonant */
printf("'%c' is Consonant.", ch);
}
else
{
//If it is neither vowel nor consonant then it is not an alphabet.

printf("'%c' is not an alphabet.", ch);


}

return 0;
}

You might also like