Control Structures: (Conditional Statements)
Control Structures: (Conditional Statements)
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
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 int main()
qualified to vote or not. The {
int age;
qualifying age is 18 years old clrscr();
and above. printf(“\n Enter age: ”);
scanf(“%d”, age);
if (age>=18)
{
Sample Output: printf(“\n Your age is %d and qualified to vote”,
Enter age: 20 age);
}
Your age is 20 and qualified to vote else
{
printf(“\n Your age is %d and not 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
char answer;
is Y, it outputs the message
printf("Would you like to know my name?\n");
My name is BUMBLE BEE printf("Type Y for YES and N for NO: ");
otherwise, outputs. answer = getchar();
if(answer == 'Y' || answer == 'y’)
Thank you and have a blessed
{
day!
printf("\nMy name is BUMBLE BEE ");
Sample Output: }
else
Would you like to know my name? {
Type Y for YES and N for NO: Y printf("\nThank you and have a blessed day! \n");
}
My name is BUMBLE BEE }
if...else..if Statement
An if statement can be followed by
Syntax:
an optional else if...else if(condition1)
statement, which is very useful to {
test various conditions using /* Executes when the condition1 is true */
single if...else if statement. }
else if( condition2)
{
/* Executes when the condition2 is true */
}
.
.
.
else if( condition n)
{
/* Executes when the condition n is true */
}
else
{
/* executes when the none of the above condition is
true */
}
if...else if... (example)
Write a program to assist a
teacher in giving the grade #include<stdio.h>
description based on the main()
grade input {
Grade Description float grade;
clrscr();
95 – 100 Excellent
printf(“Input a grade value: “);
85 – 94 Very Good scanf(“%f”, grade);
80 – 84 Good if ((grade>=95) && (grade<=100))
75 – 79 Fair printf(“\n Wow you are EXCELLENT!”);
74 below Failed else if ((grade>=85) && (grade<=94))
printf(“\n Keep it up, you are VERY GOOD!”);
else if ((grade>=80) && (grade<=84))
Sample Output: printf(“\n Keep on studying you are GOOD!”)
Input a grade value: 85 else if ((grade>=75) && (grade<=79))
Keep it up, you are VERY GOOD! printf(“\n Need more concentration, you got FAIR!”);
else
printf(“\n Practice a lot coz you failed!”);
}
Switch Statement
C has a multiple branch selection statements called switch. It tests the value of
an expression against a list of integer or character constants. When a match is Syntax:
found the statements associated with that constants are executed. Break is
used to limit the execution of the program for a specific case only. It forces an
switch (expression)
early exit from one layer of looping constructs. When no match was found
among the case stated, the default operation will be executed.
{
case constant1;
Statement sequence
break;
case constant2;
Statement sequence
break;
case constant3;
Statement sequence
break; . . .
default
Statement sequence
}
Switch Statement (example)
Write a program to check # include <stdio.h>
whether the enter character main()
{
is a vowel or consonant char ch;
printf(“\n Enter a lower case alphabeet (a-z);”);
scanf(“ %c” , &ch);
Sample Output:
switch (ch)
Enter a lower case alphabeet (a-z);”); O {
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();
}
}
Activity 5.1
Conditional Statements
if-else statement
Problem 1. Write a program to calculate the real roots of the
quadratic equation. Note: use <math.h> preprocessor directive and
sqrt() function to get the square root value of a number, e.g. sqrt(n)
where n = 9.
Problem 2. Write a program that will read in an integer and print the
whether the number is Positive or not
if-else-if statement
Problem 3. Given two numbers X and Y. Write a program to
determine the difference between X and Y. If X – Y is negative,
compute R = X + Y; if X – Y is zero, compute R = 2X + 2Y; and if X –
Y is positive, compute R = X*Y. Print out the value of X, Y, and R.
Activity 5.1
Conditional Statements
if-else-if statement
Problem 4. Write a program that will accept the tuition fee and compute
the total tuition fee based on the mode of payments.
Mode of Payments
Cash (1) 10% discount
Two Payments (2) 5 % interest
Three Payments(3) 10% interest
Sample Output:
Enter tuition fee: 20000
(Press 1 for Cash, 2 for Two-Installment, 3 for Three-Installment)
Enter mode of payment: 2
Your total tuition fee is: 21000
Activity 5.1
Conditional Statements
Switch Statement *
Problem 5. Electric Bill: Write a program that will calculate and
show bills of the Manila Electric Company. The rates vary depending
on whether the use is residential (R), commercial( C) , or industrial
(I). Any other code should be treated as an error.
The program should accept the subscriber ID, Subscriber
Name, his total electrical consumption in a month, and the code of
the consumer type.
The rates are computed as follows:
R: 50 plus .50 per kwh used
C: 100 for the first 1000 kwh and 0.45 for each additional kwh
I: 180 for the first 1000 kwh and 0.75 for each additional kwh
Activity 5.1
Conditional Statements
Switch statement
Problem 6. A manufacturing company has classified its executives into four levels for the benefit of certain
perks. The levels and corresponding perks are shown below:
An executive's gross salary includes basic pay, house rent allowance at 25% of basic pay and other perks.
Income tax is withheld from the salary on a percentage basis as follows:
Write a program that will read an executive's job number, level number, and basic pay and then compute the
net salary after withholding income tax.
15