Вы находитесь на странице: 1из 21

CHAPTER 5:

CONDITIONAL
STATEMENTS
CSCC 102 KATHRINA T. DACANAY
Course Outline

1. if Statement
2. if-else Statement
3. if-else if Statement
4. switch Statement
1 if Statement
if Statement Syntax

if (condition)
statement_TRUE;
if Statement Examples
if (age >= 18)
printf(“Allowed to Vote!”);

if (grade >= 75)


printf(“You Passed!”);
2 if-else Statement
if-else Statement Syntax

if (condition)
statement_TRUE;
else
statement_FALSE;
if-else Statement Examples
if (age >= 18)
printf(“Allowed to Vote!”);
else
printf(“Not allowed to Vote!”);

if (grade >= 75)


printf(“You Passed!”);
else
printf(“You Failed!”);
3 if-else if Statement
if-else if Statement Syntax

if (condition1)
statement_TRUE1;
else if(condition2)
statement_TRUE2;
:
else
statement_FALSE;
if-else if Statement Example
if ((age >= 18) && (age <= 100))
printf(“Allowed to Vote!”);
else if (age >= 101)
printf(“6 feet under!”);
else if ((age >= 12) && (age <= 17))
printf(“Too young to vote!”);
else if ((age >= 7) && (age <= 11))
printf(“Kid!”);
else if ((age >= 0) && (age <= 6))
printf(“Toddler!”);
else
printf(“Age does not exist!”);
4 switch Statement
switch Statement Syntax
switch(variable) {
case value1:statement_val1;
break;
case value2:statement_val2;
break;
:
default: statement_FALSE;
}
switch Statement Example
switch (num) {
case 1: printf(“ONE”);
break;
case 2: printf(“TWO”);
break;
case 3: printf(“THREE”);
break;
default: printf(“Bye!”);
}
Review Exercises
Directions: Give the output of the program below if the
respective values are entered for variables num1 and
num2. #include<stdio.h>
int num1, num2;
1.num1 = 90; num2 = 80;

main() 2.num1 = 50; num2 = 30;


{
scanf("%d%d", &num1, &num2); 3.num1 = -1; num2 = 200;
if(num1 >= 0 && num2 <= 100)
{ 4.num1 = -1; num2 = 10;
if(num1 < 50 || num2 > 80)
printf("Don Mariano Marcos"); 5.num1 = 0; num2 = -1;
else
printf("Memorial");
}
else
{
if(num2 >=0 && num2 <= 100)
printf("State");
else
printf("University");
}
}
Programming Exercises
Directions: Write a C program for the following:
1. A program that will display in words the number entered by the
user. Consider numbers from 1 to 20 only.
Sample Output:
Enter a number from 1-20: 17
The number in words is seventeen.
Programming Exercises
2. Input grade in each subject in BSCS First Year:
 Introduction to Computing
 Computer Programming 1
 Art Appreciation
 Purposive Communication
 Mathematics in the Modern World
 Kontekstwalisadong Komunikasyon sa Filipino
 Fundamentals of Physical Fitness

Then display the summary of numerical ratings and remarks of


all subjects. Use the following table for the numerical rating for
each grade.
1.00 = 98-100 2.50 = 80-82
1.25 = 95-97 2.75 = 77-79
1.50 = 92-94 3.00 = 75-76
1.75 = 89-91 5.00 = Failed
2.00 = 86-88 INC = Incomplete
2.25 = 83-85 D - Dropped
Programming Exercises
Sample Output:
ACADEMIC RECORD
*******************************
Introduction to Computing : 88
Fundamentals of Programming : 90
Art Appreciation : 95
Purposive Communication : 90
Mathematics in the Modern World : 70
Kontekstwalisadong Komunikasyon sa Filipino : 92
Fundamentals of Physical Fitness : 94
*******************************

SUMMARY
Subject Grade Numerical Rating Remarks
Introduction to Computing 88 2.00 Passed
Fundamentals of Programming 90 1.75 Passed
Art Appreciation 95 1.25 Passed
Purposive Communication 90 1.75 Passed
Mathematics in the Modern World 70 5.00 Failed
Kontekstwalisadong Komunikasyon sa Filipino 92 1.50 Passed
Fundamentals of Physical Fitness: 94 1.50 Passed
Programming Exercises
3. A simple Body Mass Index (BMI) Calculator app. Create a BMI
calculator application that reads the user’s weight in pounds (ok
kilograms) and height in inches, then calculates and displays the
user’s BMI. The formulas for calculating BMI are:

Also, the application should display the following information


from the Department of Health and Human Services/National
Institutes of Health so the user can evaluate his/her BMI:
Underweight: less than 18.5
Normal: between 18.5 and 24.9
Overweight: between 25 and 29.9
Obese: 30 or greater
Programming Exercises
Sample Output:
Enter weight in kilograms : 67.5
Enter height in meters : 1.63
BMI : 25.21
Remarks : Overweight
END OF CHAPTER 5

Вам также может понравиться