0% found this document useful (1 vote)
639 views

Simple If, Else-If, If-Else, Nested-If and Switch-Case Statements

Here is a program that implements the nested if/else statements to determine the call charge based on the day and time of the call: #include <stdio.h> int main() { int day, time; printf("Enter day of call (1-7): "); scanf("%d", &day); printf("Enter time of call (24-hr format): "); scanf("%d", &time); if(day >= 1 && day <= 5) { // weekday if(time >= 6 && time < 18) printf("Charge is 2.50 pesos/minute\n"); else printf("Charge is 2.00 pesos/minute\n");

Uploaded by

basti
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
639 views

Simple If, Else-If, If-Else, Nested-If and Switch-Case Statements

Here is a program that implements the nested if/else statements to determine the call charge based on the day and time of the call: #include <stdio.h> int main() { int day, time; printf("Enter day of call (1-7): "); scanf("%d", &day); printf("Enter time of call (24-hr format): "); scanf("%d", &time); if(day >= 1 && day <= 5) { // weekday if(time >= 6 && time < 18) printf("Charge is 2.50 pesos/minute\n"); else printf("Charge is 2.00 pesos/minute\n");

Uploaded by

basti
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Simple If, Else-if, If-else,

Nested-if and Switch-


case Statements
Sequential control structure
• A sequential control structure is organized such that statements are
executed in sequence.
• One after the other in the order of their appearance in the source code.
Example:
a = 1; /* first statement */
b = 2; /* second statement */
c = a + b; /* third statement */
In the code above, statements will be executed always in the following order:
first statement, followed by the second and finally by the third statement.
Conditional Control Structure
First, some analogy:
Scenario number 1: You are driving a car. The road forks into two, which
of two possible paths do you take? Your choice can be influenced by
factors such distance or the amount of traffic.

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.”

What do you think is common to these two scenarios?


Conditional Control Structure
• A conditional structure is organized in such as way that there is
always a condition that has to be evaluated first.
• The condition will either evaluated to a true or false.
• In C language, remember that a condition will evaluate 1 (for true)
and 0 (for false).
• The result of the condition will then dictate the course of action to
be taken.
• The conditional control structure allows the program to make choices
depending on a condition.
What are the available conditional control
structures in C?
• If statement
• simple if and else-if
• else-if statements in C is like another if condition, it's used
in a program when if statement having multiple decisions.
• if-else
• nested if
• Switch case statements
How to you use an IF and ELSE-IF
statement?
• The syntax for the if statements is :
if (<expression>)
<statement>
• The value of <expression> is first evaluated, if it is non-zero, then the condition is specified as
true.
• If it is evaluated as zero, then the condition is specified as false.
• If the condition is true, then the <statement> is executed.
Note:
• The expression must always be enclosed within a pair of parentheses, forgetting the
parentheses will result into a syntax error.
• If there is more than one statement that need to be executed when the condition is non-
zero, then this statements must be grouped in a pair of curly brackets.
Example
• 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.
#include <stdio.h>
main()
{
int n;
printf(“Input an integer value: “);
scanf(“%d”, &n);
If (n>=0)
printf(“n = %d is POSITIVE\n”, n);
}
Exercise
Write a program that will allow the user to input a double precision
floating point value. Print the word NEGATIVE if the number is negative
value and print the word POSITIVE if the number is positive value.
Exercise
Write a program that will ask the user to input two integers. Thereafter,
the program should determine if these two numbers are equivalent. If
they are equivalent, print the word EQUIVALENT.
Exercise
Modify the program such that it will be able to determine which of the
two numbers is smaller. The program should print “FIRST NUMBER IS
SMALLER” if the first integer is indeed smaller than the second. If the
second number is the one that is smaller it should print “SECOND
NUMBER IS SMALLER”. If the two numbers are equivalent, print the
word, “TWO NUMBERS ARE EQUIVALENT”.

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.

You might also like