Module 5. Conditional Statement
Module 5. Conditional Statement
C Programming
Control Structures
(Conditional Statements)
Learning Objectives:
At the end of the session, students should be
able to:
• Impose programming conditional statements
on solutions to programming problems
• Utilize built in programming functions on
programming solutions
Conditional Statements
#include<stdio.h>
Determine if the input age is main()
qualified to vote pr not. The {
qualifying age is 18 years old int age;
clrscr();
and above.
printf(“\n Enter age: “);
scanf(“%d”, &age);
if (age>=18)
Sample Output:
{
Enter age: 20 printf(“\n Your age is %d and qualified
Your age is 20 and qualified to vote to vote”, age);
}
else
{
printf(“\n Your age is %d and qualified
to vote”, age);
}
}
if…else Statement (example)
Displays a question of YES/NO
type to the user and reads the #include <stdio.h>
user's response in a single main()
character (Y or N). If the response {
is Y, it outputs the message char answer;
My name is BUMBLE BEE printf("Would you like to know my name?\n");
otherwise, outputs. printf("Type Y for YES and N for NO: ");
Thank you and have a blessed answer = getchar();
day! if(answer == 'Y' || answer == 'y')
printf("\n\nMy name is BUMBLE BEE \n");
Sample Output: else
Would you like to know my name? printf("\nThank you and have a blessed day!
Type Y for YES and N for NO: Y \n");
}
My name is BUMBLE BEE
if...else..if Statement
Syntax:
An if statement if(condition1)
{
can be followed /* Executes when the condition1 is true */
by an optional }
else if( condition2)
else if...else {
statement, which /* Executes when the condition2 is true */
}
is very useful to .
test various .
.
conditions using else if( condition n)
{
single if...else if /* Executes when the condition n is true */
statement. }
else
{
/* executes when the none of the above condition is
true */
}
if...else if... (example)
Write a program to assist a #include<stdio.h>
void main()
teacher in giving the grade
{
description based on the float grade;
grade input clrscr();
Grade Description printf(“Input a grade value: “);
95 – 100 Excellent scanf(“%f”, &grade);
85 – 94 Very Good if ((grade>=95) && (grade<=100))
printf(“\n Wow you are EXCELLENT!”);
80 – 84 Good
else if ((grade>=85) && (grade<=94))
75 – 79 Fair printf(“\n Keep it up, you are VERY GOOD!”);
74 below Failed else if ((grade>=80) && (grade<=84))
printf(“\n Keep on studying you are GOOD!”)
Sample Output: else if ((grade>=75) && (grade<=79))
printf(“\n Need more concentration, you got FAIR!”);
Input a grade value: 85
else
Keep it up, you are VERY GOOD! printf(“\n Practice a lot coz you failed!”);
getch();
}
Switch Statement