TTS_Module6-CC102
TTS_Module6-CC102
MODULE
College
INFORMATION SHEET MD-1.1.1
“Conditional Statement”
Conditional statements are statements that check an expression then may or may not execute a
statement or group of statements depending on the result of the condition.
The IF statement
The general form of the IF statement is:
If (expression)
statement;
where:
if is a reserve word in Turbo C
expression is relational or Boolean expression that evaluates to a TRUE(1) or FALSE(0) value
statement may either be a single Turbo C statement or a block of Turbo C statements
Example 1: Write a program that will output “Congratulations you PASSED!” if the student’s grade is
greater than or equal to 75.
#include<stdio.h>
int grade;
main() Sample Output:
{ Enter student grade: 95
clrscr(); Congratulations you PASSED!
printf(“Enter student grade:”);
scanf(“%d”, &grade);
if (grade >=75)
printf(“Congratulations you PASSED!”);
getch();
}
In the sample program above, if the inputted value for the variable grade is greater than or
equal to 75, the printf() statement that follows will be executed.
Example 2. Write a program that will ask for price. If the price is greater than 1000, compute a 10%
discount from the original price. Display the computed discount.
#include<stdio.h>
float price, discount;
Sample Output:
main()
Enter value for price: 2000
{
Discount is 200.00
clrscr();
printf(“Enter value for price:”);
scanf(“%f”, &price);
if (price > 1000)
{
discount = price * 0.10;
printf (“Discount is %.2f”, discount);
}
getch();
}
In example number 2, if the inputted value for the variable price is greater than 1000, discount
of 10 % will be computed and will be displayed on the screen.
Example 3: Write a program that will output “You are qualified to vote!” if the age that enter is greater
than or equal to 18.
#include<stdio.h>
int age;
Sample Output:
main()
Enter your Age: 18
{
You are qualified to vote
clrscr();
printf(“Enter your Age:”);
scanf(“%d”, &age);
if (age>=18)
{
printf (“You are qualified to vote”);
}
getch();
}
The IF-ELSE statement
The general form of the if-else statement is:
If (expression)
statement_1;
else
statement_2;
where:
if and else is a reserve words in Turbo C
expression is relational or Boolean expression that evaluates to a TRUE(1) or FALSE(0) value
statement_1 and statement_2 may either be a single Turbo C statement or a block of Turbo C
statements
The general form of the if-else statement with block of statements is:
If (expression)
{
statement_sequence;
}
else
{
statement_sequence;
}
In an if-else statement, if the expression is TRUE(1), the statement or block of statements after
the if statement will be executed; otherwise, the statement or block of statement in the else statement
will be executed.
Example 1. Write a program that will output “Congratulations you PASSED!” if the student’s grade is
greater than or equal to 75. Otherwise output, “Sorry you FAILED!”
#include<stdio.h>
int grade;
Sample Output:
main()
Enter student grade: 60
{
Sorry you Failed!
clrscr();
printf(“Enter student grade:”);
scanf(“%d”, &grade);
if (grade > = 75)
printf (“Congratulations you PASSED!”);
else
printf (“Sorry you FAILED!”);
getch();
}
The printf () statement under if will be executed when the inputted value for grade is greater
than or equal to 60. Otherwise, the printf () statement under else will be executed.
Example 2: Write a program that will output “You are qualified to vote!” if the age that enter is greater
than or equal to 18. Otherwise, “You are NOT qualified to vote!”
#include<stdio.h>
int age;
Sample Output:
main()
Enter your Age: 16
{
You are NOT qualified to vote
clrscr();
printf(“Enter your Age:”);
scanf(“%d”, &age);
if (age>=18)
{
printf (“You are qualified to vote”);
}
else
{
printf (“You are NOT qualified to vote”);
}
getch();
}
NESTED-IF Statement
One of the most confusing aspects of the if statement in any programming language is nested
ifs. A nested if is an if statement that is the object of either an if or else. This is sometimes referred to as
“an if within an if.”
The reason that nested ifs are so confusing is that can be difficult to know what else associates
with what if.
Fortunately, Turbo C provides a very simple rule for resolving this type of situation. In Turbo C,
the “else is linked to the closest preceding if that does not already have an else statement associated
with it.”
Situation 1 The else at number 3 is paired with the if in number 2 since it is the nearest if statement with
the else statement.
1. if ….
2. if ….
:
3. else
Situation 2 The else in number 5 is paired with the if in number 1
1. if…
2. {
3. if…
:
4. }
5. else
Notice that there is a pair of braces found in number 2 and number 4. The pair of braces defines the
scope of the if statement in number 1 starting from the { in number 2 and ends with } in number 4.
Therefore, the else statement in number 5 cannot be paired with the if statement in number 3 because
the else statement is outside the scope of the first if statement. This makes the if statement in number
1 the nearest if statement to the else statement in number 5.
Example 1: Write a program that reads in three numbers A, B, and C and determine which is the largest.
#include<stdio.h>
intA,B,C;
main() Sample Output:
{ Enter three numbers: 7 11 5
clrscr(); B is the largest
printf(“Enter three numbers:”);
scanf(“%d%d%d”, &A, &B, &C);
if (A >B)
if (A>C)
printf(“A is the largest.\n”);
else
printf(“C is the largest.\n”);
else
if (B>C)
printf(“B is the largest.\n”);
else
printf(“C is the largest.\n”);
getch();
}
The IF-ELSE-IF Ladder
A common programming construct in Turbo C is the if-else-if ladder.
The general form of the if-else-if ladder statement is:
If (expression_1)
statement_1;
else if (expression_2)
statement_2;
else if (expression_3)
statement_3;
:
:
else
statement_else;
where:
ifand elseis a reserve words in Turbo C
expression_1, expression_2, up to expression_n is relational or Boolean expression that
evaluates to a TRUE(1) or FALSE(0) value
statement_1and statement_2up to statement_else may either be a single Turbo C statement or
a block of Turbo C statements
In an if-else-if ladder statement, if the expression are evaluated from the top downward. As
soon as a true condition is found, the statement associated with is executed, and the rest of the ladder
will not be executed. if none of the condition is true, the final else is executed.
The final else acts as a default condition. If all other conditions are false, the last else statement
is performed. If the final else is not present, then no action takes place.
Note: The final else is optional, you may include this part if needed in the program or you may not
include if not needed.
Example 1.Write a program that will ask the user to input an integer then output the equivalent day of
the week. 1 is Sunday, 2 is Monday and so on. If the inputted number is not within 1-7, output “Day is
not available!”
#include<stdio.h>
main()
Sample Output:
{
Enter an integer: 4
int day;
Wednesday
clrscr();
printf(“Enter an integer:”);
scanf(“%d”, &day);
if (day == 1)
printf (“Sunday”);
else if (day == 2)
printf (“Monday”);
else if (day == 3)
printf (“Tuesday”);
else if (day == 4)
printf (“Wednesday”);
else if (day == 5)
printf (“Thursday”);
else if (day == 6)
printf (“Friday”);
else if (day == 7)
printf (“Saturday”);
else
printf (“Day is not available!”);
getch();
}
Reference:
Workbook in C Programming Computer Programming 1 by Paulino H. Gatpandan, Azenith M. Rollan
SELF CHECK MD-1.1.1
1. if statement
2. if else statement
3. if else if statement
STUDENT NAME: __________________________________ SECTION: __________________
Applications / Software:
Computer: TurboC2
Android phone: Coding C
Procedure:
Create the program of the following problem
A. Write a Turbo C program that finds the smallest among the five integers
inputted by the user
B. Write a program that will display “IT’S COLD!” if the temperature is less than
20, “IT’S HOT!” if the temperature is greater than 30, “COOL CLIMATE!”
otherwise.
C. Write a program that gives a discount of 100 pesos to a customer if the shirt
bought is XL and the price is greater than 500 pesos; a discount of 50 pesos if
the shirt bought is L and the price is greater than 400.
D. Put the screenshot of code and output here.
PRECAUTIONS:
Undertake final inspections to ensure the program conform to requirements
ASSESSMENT METHOD: PERFORMANCE TASK CRITERIA CHECKLIST
STUDENT NAME: __________________________________ SECTION: __________________
5 - Excellently Performed
4 - Very Satisfactorily Performed
3 - Satisfactorily Performed
2 - Fairly Performed
1 - Poorly Performed
_________________________________
TEACHER
Date: ______________________