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

Module 5. Conditional Statement

The document discusses different conditional statements in C programming including if-else statements, if-else-if statements, and switch statements. It provides the syntax and examples of each statement. The if-else statement executes one set of code if the condition is true and another if it is false. The if-else-if statement allows testing multiple conditions in a single statement. The switch statement compares a value to multiple case values and executes the code for the matching case.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Module 5. Conditional Statement

The document discusses different conditional statements in C programming including if-else statements, if-else-if statements, and switch statements. It provides the syntax and examples of each statement. The if-else statement executes one set of code if the condition is true and another if it is false. The if-else-if statement allows testing multiple conditions in a single statement. The switch statement compares a value to multiple case values and executes the code for the matching case.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CMPRG

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

Conditional statements require one or


more conditions to be evaluated or tested by
the program, along with a statement or
statements to be executed if the condition is
determined to be true, and optionally, other
statements to be executed if the condition is
determined to be false.
if…else Statement
An if statement can be followed by an else statement, which
executes when the Boolean expression is false
Syntax:
if(condition)
{
/* statement(s) will execute if the condition is true */
}
else
{
/* statement(s) will execute if the condition is false */
}

If the Boolean expression evaluates to true, then the block of


code inside the ‘if’ statement will be executed. If the Boolean
expression evaluates to false, then the first set of code after the
end of the ‘if’ statement (after the closing curly brace) will be
executed.
if…else Statement (example)

#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

C has a multiple branch selection Syntax:


statements called switch. It tests switch (expression)
{
the value of an expression against case constant1;
a list of integer or character Statement sequence
constants. When a match is found break;
the statements associated with that case constant2;
constants are executed. Break is Statement sequence
break;
used to limit the execution of the case constant3;
program for a specific case only. It Statement sequence
forces an early exit from one layer break; . . .
of looping constructs. When no default
match was found among the case Statement sequence
}
stated, the default operation will
be executed.
Switch Statement (example)
Write a program to check # include <stdio.h>
void main()
whether the enter character {
is a vowel or consonant char ch;
printf("\n Enter a lower case alphabeet (a-z);");
Sample Output: scanf("%c" , &ch);
Enter a lower case alphabeet (a-z);”); O switch(ch)
Entered Character is vowel {
case 'a': case 'A':
case 'e': case 'E':
case 'i' : case 'I':
case 'o' : case 'O':
case 'u' : case 'U':
printf("\n Entered Character is vowel");
break;
default:printf("\n Entered Character is
consonant");
}
getch();
}
11

You might also like