Documente Academic
Documente Profesional
Documente Cultură
/ccsrac 2
/ccsrac 3 /ccsrac 4
Velasco
Aquarium Gokongwei scenarios?
Mezz-1
Admission Conserv
N. Gate Eng.
Gate
S. Gate Simplified Map
S-N
Gate of DLSU
/ccsrac 5 /ccsrac 6
1
Conditional Control Structure Conditional Control Structure
• A conditional control structure is
organized in such a way that there is always a
condition that has to be evaluated first. Two basic conditional control structures:
1. if statement (including if-else statement)
• The condition will either evaluate to a true or false. 2. switch statement
/ccsrac 7 /ccsrac 8
2
Exercises Exercises
1. Write a program that displays the word 2. Modify your answer in number 1 such that
EVEN if the number entered by the user is even. when the number entered is not even,
the program will display the word ODD.
/ccsrac 13 /ccsrac 14
/ccsrac 15 /ccsrac 16
Examples Examples
/* sample program that determines if a /* sample program that determines if
person can ride the Enterprise or not */ entered number is EVEN or NOT*/
#include <stdio.h>
#include <stdio.h>
main()
{ main()
float fHeight; {
int nNumber;
printf(“Enter Height> ”);
scanf(“%f”, &fHeight);
printf(“Enter Number> ”);
if (fHeight >= 4.5)
{ scanf(“%d”, &nNumber);
printf(“Height is OK.\n”); if (nNumber % 2 == 0)
printf(“Have fun on the Enterprise”); printf(“Number = %d is EVEN”, nNumber);
} else
else
printf(“Number = %d is ODD”, nNumber);
printf(“Sorry, you’re too short ”);
} }
/ccsrac 17 /ccsrac 18
3
Exercises Exercises
4. Write a program to input two integers. 5. Write a program that will allow the user to
Thereafter, the program should determine input an integer value. If the value is greater
if these two numbers are equivalent. If they are than zero, display the word POSITIVE. If the value is
equivalent, print the word EQUIVALENT; otherwise, negative, then display the word NEGATIVE. If the
display NOT EQUIVALENT. value is zero, display the word ZERO.
/ccsrac 19 /ccsrac 20
/ccsrac 23 /ccsrac 24
4
Exercises Exercises
7. You were hired by PAGCOR as part of a 8. In temperate countries like Japan, there
programming team in-charge of automating its are four seasons. April to June is spring,
BINGO game. Your task is to write a program that will July to September is summer, October to December
accept an integer whose value is 1 to 75. Thereafter, is autumn and January to March is winter. We wish
your program should determine and print the letter to write a program that allows the user to input month
that corresponds to that number. That is, the in its numeric equivalent (i.e., January is 1, February
numbers from 1 to 15 correspond to ‘B’, 16 to 30 is to 2 and so on...).
correspond to ‘I’, 31 to 45 correspond to ‘N’ , 46 to 60
correspond to ‘G’ and 61 to 75 correspond to ‘O’. The output of the program is the season associated
with that month. An example session is as follows:
Input month: 5
It is spring.
Have a nice day.
/ccsrac 25 /ccsrac 26
/ccsrac 29 /ccsrac 30
5
Another answer Still another answer
/* Program that gives season, given the /* Program that gives season, given the
numeric month */ numeric month */
#include <stdio.h> #include <stdio.h>
main() main()
{ {
int nMonth; int nMonth;
/ccsrac 33 /ccsrac 34
Examples Examples
/* Program that gives season, given the
numeric month */
switch(nDay)
{ #include <stdio.h>
case 0: printf(“SUNDAY”); break; main()
case 1: printf(“MONDAY”); break; {
case 2: printf(“TUESDAY”); break; int nMonth;
case 3: printf(“WEDNESDAY”); break;
printf(“Input month: ”);
case 4: printf(“THURSDAY”); break; scanf(“%d”, &nMonth);
case 5: printf(“FRIDAY”); break; switch(nMonth)
case 6: printf(“SATURDAY”); {
} case 1:
case 2:
} case 3:
printf(“It is winter.\n”);
break;
/ccsrac 35 /ccsrac 36
6
Examples Be careful of waterfalls!
case 4: /* Program that displays messages
case 5: depending on the grade entered. */
case 6: #include <stdio.h>
printf(“It is spring. \n”); main()
break; {
case 7: char cGrade;
case 8: printf(“Enter the grade: ”);
case 9: scanf(“%c”, &cGrade);
printf(“It is summer.\n”); switch(cGrade)
break; {
case 10: case ‘A’: printf(“V.Good!\n”); break;
case 11: case ‘B’:
case 12: case ‘C’: printf(“Good!\n”); break;
printf(“It is autumn.\n”); case ‘D’: printf(“Fair!\n”); break;
} default: printf(“Poor!\n”);
printf(“Have a nice day.”); }
} }
/ccsrac 37 /ccsrac 38
Exercises Exercises
9. Write a program that displays the expected 10. Write a program that interacts with the user
brightness of a standard light bulb given its like this:
wattage. Use this table:
(1) Carbon monoxide
(2) Hydrocarbons
Watts Brightness (in Lumens) (3) Nitrogen oxides
15 125 (4) Nonmethane hydrocarbons
25 215 Enter pollutant number>> 2
40 500 Enter number of grams emitted per mile>> 0.35
60 880 Enter odometer reading>> 40112
75 1000 Emissions exceed permitted level of 0.31 grams/mile.
100 1675
Use the table of emissions limits in next slide to determine
Display Unknown bulb if the watts input is not in the the appropriate message.
table.
/ccsrac 39 /ccsrac 40
/ccsrac 41 /ccsrac 42
7
New C Constructs New C Constructs
CONSTRUCT EFFECT switch statement
if statement switch (cNextChar) {
Multiple-Alternative case ‘A’: Displays one of five
if (x < 0.0) { Displays one of three
case ‘a’: printf(“Excellent”); break; messages based on
messages depending case ‘B’: the value of
printf(“negative”);
on whether x is case ‘b’: printf(“Good”); break; cNextChar (type
fAbsX = -x;
case ‘C’: char). If cNextChar
} negative, positive or
zero. Sets fAbsX to case ‘c’: printf(“O.K.”); break; is ‘D’, ‘d’, ‘F’, or
else if (x == 0.0) { ‘f’, the student is
printf(“zero”); represent the absolute case ‘D’:
value or magnitude of x. put on probation. If
case ‘d’:
fAbsX = 0.0; fNextChar is not
case ‘F’:
} listed in the case
case ‘f’: printf(“Poor, student is”);
else { labels, displays an
printf(“on probation”); error message.
printf(“positive”);
break;
fAbs = x;
default: printf(“Invalid letter grade”);
}
}
/ccsrac 43 /ccsrac 44
More exercises
/ccsrac 45