You are on page 1of 11

Conditional Statements

Conditional Statements are statements that check an expression then


may or may not execute a statement or group of statements depending on the
result of the Boolean expression or condition.
Boolean Expression is an expression, which are answerable by TRUE or
FALSE. A Boolean Expressions must have a relational operator to evaluate
these things.

TYPES OF CONDITIONAL STATEMENTS:

IF Statement

The if statement is the simplest of C’s selection structure.

The general form of the if statement is:

if (<condition>)
{
<statement/s A>;
}

where if is a reserve word in C.

In an if statement, if the expression evaluates to TRUE (1), the statement or the


block of statements that forms the target of the if statement will be executed.
Otherwise, the program will ignore the statement or the block of statements.

Note: Never place a semicolon after the expression in an if statement.

EXAMPLE:

1. Write a C program that will output “Congratulations you PASSED” if the


student’s grade is greater than or equal to 75.

#include<stdio.h>
#include<conio.h>
float grade;
main()
{
printf("Enter your student grade= ");
scanf("%f", &grade);
if (grade>=75)
printf("CONGRATULATIONS YOU PASSED");
getch();
}

2. Write a program that will ask for an item and a price. If the price is greater
than 1000, compute a 10% discount from the original price. Display the
computed discount.

#include<stdio.h>
#include<conio.h>
float price, discount=0.00;
char item[100];
main()
{
printf("Enter any item= ");
gets(item);
printf("Enter the price= ");
scanf("%f", &price);
if (price > 1000)
discount=price*0.1;
printf("THE DISCOUNT IS Php %.2f", discount);
getch();
}

2
IF-ELSE STATEMENT

The general form of the if-else statement is:

if (<condition>)
{
<statement/s A>; }
else
{
<statement/s B>; }

where if and else are reserve words in C.

In an if-else statement, if the expression evaluates to TRUE (1), the statement or


the block of statements after the if statement will be executed; otherwise, the
statement or block of statements in the else statement will be executed.

Note: Only the code associated with the if or the code that is associated with the
else executes, never both.

3. Write a program that will output “Congratulation you PASSED” if the student’s
grade is greater than or equal to 75. Otherwise output, “Sorry you FAILED!”

#include<stdio.h>
#include<conio.h>
float grade;
main()
{
printf("Enter your student grade= ");
scanf("%f", &grade);
if (grade>=75)
printf("CONGRATULATIONS YOU PASSED");
else
printf("SORRY YOU FAILED!");
getch();
}

3
4.
/* Write a program that will determine whether a given number is ODD or
EVEN */

#include<stdio.h>
#include<conio.h>
int num;
main()
{
printf("Enter any number= ");
scanf("%i", &num);
if (num % 2 == 0)
printf("The number %i is EVEN",num);
else
printf("The number %i is ODD",num);
getch();
}

4
IF-ELSE-IF LADDER

The general form is:

if (<condition_1>)
<statement/s A>;
else if (<condition_2 >)
<statement/s B>;
else if (<condition_3 >)
<statement/s C>;
.
.
.

else
<statement/s>;

In an if-else-if ladder statement, the expressions are evaluated from the top
downward. As soon as a true condition is found, the statement associated with it
is executed, and the rest of the ladder will not be executed. If none of the
condition is true, the final else is executed.

The final else acts as a default condition. If all other conditions are false, the last
else statement is performed. If the final else is not present, then no action takes
place.

5. Write a C program that will ask the user to input an integer then output the
equivalent day of the week. 1 is Sunday, 2 is Monday, and so on. If the inputted
number is not within 1-7, output “Day is not available!”

#include<stdio.h>
#include<conio.h>
main()
{
int day;
printf("Enter any integer [1-7]= ");
scanf("%d", &day);
if (day==1)
printf("SUNDAY");
else if (day==2)
printf("MONDAY");
else if (day==3)
printf("TUESDAY");

5
else if (day==4)
printf("WEDNESDAY");
else if (day==5)
printf("THURSDAY");
else if (day==6)
printf("FRIDAY");
else if (day==7)
printf("SATURDAY");
else
printf("DAY IS NOT AVAILABLE!");
getch();

SWITCH Statement

The switch statement is a built – in multiple – branch decision statement


where variable is successively tested against a list of integer or character constants.
When a match is found, the statement or statement associated with the constant is
executed. The syntax for the switch statement is

6
switch (<variable>)
{
case <value 1>:
<statement sequence >;
break;

case <value 2 >:


<statement sequence >;

break;

.
.

case <value n >:


<statement sequence >;

break;

default:
<statement sequence>;
}

where:
variable – an int or char expression also called selector
value – an expression to be matched with a case label
statement sequence – action associated with preceding cases
break statement – used to terminate the statement sequence associated with
each value. If break is omitted, execution will continue until the next case
statement.
default statement – is the main statement to be executed if no matches are
found in the case. It is optical and therefore if it is not present, no action takes
place if all matches fail.

6. Write a C program that will ask the user to input an integer then output the
equivalent day of the week. 1 is Sunday, 2 is Monday, and so on. If the inputted
number is not within 1-7, output “Day is not available!”

#include<stdio.h>
#include<conio.h>
main()
{
int day;

7
printf("Enter any integer [1-7]= ");
scanf("%d", &day);
switch (day)
{
case 1:
printf("SUNDAY"); break;
case 2:
printf("MONDAY"); break;
case 3:
printf("TUESDAY"); break;
case 4:
printf("WEDNESDAY"); break;
case 5:
printf("THURSDAY"); break;
case 6:
printf("FRIDAY"); break;
case 7:
printf("SATURDAY"); break;
default:
printf ("DAY IS NOT AVAILABLE!!!");
}
getch();

8
More examples:

1. Make a C program that will input two integers and display the larger
number.
#include<stdio.h>
#include<conio.h>
int num1,num2;
main()
{
printf("Enter num1: ");
scanf("%d",&num1);
printf("Enter num2: ");
scanf("%d",&num2);

if (num1>num2)
printf("Larger number is %d",num1);
else if(num1<num2)
printf("Larger number is %d",num2);
else
printf("You entered equal numbers");

getch();
}

9
2. Construct a program in C language that will input a character (“R”, “G”, or
“B”). Do the following output respect to the input.
CHARACTER MESSAGE
“R” Good Morning
“G” Good Afternoon
“B” Good Evening

#include<stdio.h>
#include<conio.h>
char color;
main()
{
printf("Enter character [R,G,B]: ");
scanf("%c",&color);
if ((color=='R')||(color=='r'))
printf("Good Morning");
else if((color=='G')||(color=='g'))
printf("Good Afternoon");
else if((color=='B')||(color=='b'))
printf("Good Evening");
else
printf("R,G,B only!");
getch();
}

10
11

You might also like