Simple If, Else-If, If-Else, Nested-If and Switch-Case Statements
Simple If, Else-If, If-Else, Nested-If and Switch-Case Statements
Scenario number 2: “If it rains tomorrow, I will not go out, I’ll just read a
book. Otherwise, I will go to the mall and watch a movie.”
Hint: How many conditions do you need to test? Based from the
concept presented so far, you will need one if statement for each
possible condition.
Exercise
• Write a program that will input an integer value and determine if it is
an EVEN number.
• Then modify the program such that it is also able to determine if the
input integer is an odd number.
Exercise
• Write a program that will allow the user to input an integer value
representing the days of the week. Let 1 represent Monday, 2 for
Tuesday, 3 for Wednesday, and so on. The output of the program
should be the day of the of the week in words. An example
interactions is as follows:
Input day in numeric form: 7
That day is a Sunday.
Have a nice day!
Exercise
• NOTE: The program above is just one possible solution, and actually it
is not really a good solution!
• A keen person would notice that the instruction printf(“Have a nice
day.\n”) is common to all if statements. This common instruction can
actually be factored out. Think of a better solution using simple if
statement.
Exercise
1. In temperate countries like Japan, there are four seasons. April to
June is spring, July to September is summer, October to December is
autumn and January to March is winter. We wish to write a program
that allows the user to input month in its numeric equivalent (i.e.,
January is 1, February is to 2 and so on...). The output of the program is
the season associated with that month.
2. Modify using Relational Operator and logical OR.
3. Modify using Relational Operators and logical AND.
Exercise
• Write a program that will accept an integer corresponding to a person’s age.
Thereafter, your program should print the word “BABY” is the age is up to
four years old, “CHILD” if the age is from five up to 12, “TEENAGER” if the
age is from thirteen to nineteen and “ADULT” if the age is from twenty up.
• You were hired by PAGCOR as part of a programming team in-charge of
automating its BINGO game. Your task is to write a program that will accept
an integer whose value is 1 to 75. Thereafter, your program should
determine and print the letter that corresponds to that number. That is, the
numbers from 1 to 15 correspond to ‘B’, 16 to 30 correspond to ‘I’, 31 to 45
correspond to ‘N’ , 46 to 60 correspond to ‘G’ and 61 to 75 correspond to
‘O’.
How to use an IF-ELSE statement?
• The syntax for the if-else statement is:
if (<expression>)
<statement 1>
else
<statement 2>
• The value of <expression> is first evaluated, if it is non-zero, then the condition
is specified as 1 (for true) and <statement 1> is executed. Otherwise if it is
evaluated as zero, then the condition is specified as 0 (for false), and the else
part <statement 2> is executed.
Exercise
• Write a program that will allow the user to input an integer value. If the
value is greater than or equal to zero, print the word POSITIVE, otherwise it
is a negative number, print the word NEGATIVE using if-else statement.
• Write a program that determines whether an integer is an ODD or EVEN
number using if-else statement.
• Write a program that will ask the user to input an integer value
representing the day of the week. For example 1 represents Monday, 2
represents Tuesday…, 6 for Saturday and 7 for Sunday. The program should
output the word WEEKDAY if the numeric day corresponds to a weekday,
otherwise it should output the word WEEKEND. Use if-else statement.
Exercise
• Write a program that will ask the user to input a letter. If the user
entered a lower-case letter, output the words LOWER CASE, otherwise
output the word UPPER CASE.
• You’ve been hired as a programmer by SFT (Send Fail Telecoms) – a
mobile telecommunications company. You are to write a program that
will ask the user to input the number of text messages incurred by a
SFT cellphone subscriber. The text messaging charge is then computed
based on the number of text messages. The charge is zero for the first
200 messages. Text messages beyond that are charged by 0.5 peso per
message. The program should output the charge incurred.
Can you have if/if-else within an if/if-else statement?
• YES! Since if and if-else are statements by themselves, they can actually be used as statement(s) inside an
if or if-else. The term used to refer to such a construction is nested if statements.
Sample syntax:
if <expression>
{
<statements>
if <expression>
<statement>
else
<statement>
}
else
<statements>
Nested-If Exercise
• You were hired as a programmer by TNP (Telepono ng Pilipino) a
Telephone company. TNP employs a metering scheme in computing the
telephone bill. The metering scheme is as follows:
• Calls made on a weekday between 6:00 AM to 6:00 PM are charged at 2.50 pesos
per minute. Charges made at other times during a weekday are charged a
discounted rate of 2.00 pesos per minute. Calls made anytime on a weekend are
charged a weekend rate of 1.50 pesos per minute. Your job is to write a program
that will ask the user to enter the following information: (a) an integer representing
the day the call was made, let 1 represent Monday, 2 represent Tuesday and so on,
(b) an integer representing the time (in 24 hour format) the call started (c) an
integer representing the length of time or duration of the call in minutes (assume
that all calls are rounded to the next minute, i.e. a call lasting 2 minutes and 35
seconds is billed as a 3 minute call). The rate applied depends on the day the call
was made and on the time the call was started (not when it ended). Based on the
information entered, the program should print the bill corresponding to the call.
Switch-case Statement
• The switch-case is a good alternative to cascading if-else statements.
Syntax for switch-case statement:
switch(<expression>)
{
case <value-1> : <statement – 1>
break;
case <value-2> : <statement – 2>
break;
default : <statement>
}
Exercise
• A company gives bonus to its employees depending on the number of years
of service.
Number of Years Bonus
0–5 20% of salary
6 – 10 25% of salary
11 – 15 30% of salary
16 – 20 35% of salary
> 20 50% of salary
• Make a program to input the number of years and its salary. Display the
corresponding bonus.