Vous êtes sur la page 1sur 9

Conditional Statements

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 Boolean expression or condition.
Boolean Expression is an expression, which are answerable by TRUE or
FALSE. A Boolean Expressions must have a relational operator to evaluate
these things.

TYPES OF CONDITIONAL STATEMENTS:


IF Statement
The if statement is the simplest of Cs selection structure.
The general form of the if statement is:
if (<condition>)
{
<statement/s A>;
}
where if is a reserve word in C.
In an if statement, if the expression evaluates to TRUE (1), the statement or the
block of statements that forms the target of the if statement will be executed.
Otherwise, the program will ignore the statement or the block of statements.
Note: Never place a semicolon after the expression in an if statement.
EXAMPLE:
1. Write a C program that will output Congratulations you PASSED if the
students grade is greater than or equal to 75.
#include <stdio.h>
#include <conio.h>
int grade;
main()
{
printf(Enter student grade=);
scanf(%d, &grade);
if (grade>=75)
printf(Congratulations you PASSED);

getch();
}
2. Write a program that will ask for an item and a price. If the price is greater
than 1000, compute a 10% discount from the original price. Display the
computed discount.
#include<stdio.h>
#include<conio.h>
char item[100];
float price, discount;
main()
{
printf(Enter item=);
gets(item);
printf(Enter value for price=);
scanf(%f,&price);
if (price > 1000)
{
discount=price*0.1;
printf(Discount is %0.2f, discount);
}
getch();
}
IF-ELSE STATEMENT
The general form of the if-else statement is:
if (<condition>)
{
<statement/s A>;
else
{
<statement/s B>;

}
}

where if and else are reserve words in C.


In an if-else statement, if the expression evaluates to TRUE (1), the statement or
the block of statements after the if statement will be executed; otherwise, the
statement or block of statements in the else statement will be executed.
Note: Only the code associated with the if or the code that is associated with the
else executes, never both.

3. Write a program that will accept any integer and will determine whether the
input number is ODD or EVEN.
#include<stdio.h>
#include <conio.h>
int num;
main()
{
printf(Enter any number=);
scanf(%d, &num);
if (num%2==0)
printf(The number %d is EVEN,num);
else
printf(The number %d is ODD,num);
getch();
}
IF-ELSE-IF LADDER
The general form is:
if (<condition_1>)
<statement/s A>;
else if (<condition_2 >)
<statement/s B>;
else if (<condition_3 >)
<statement/s C>;
.
.
.
else
<statement/s>;

In an if-else-if ladder statement, the expressions are evaluated from the top
downward. As soon as a true condition is found, the statement associated with it
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.

4. Write a C 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>
#include<conio.h>
int day;
main()
{
printf(Enter an integer=);
scanf(%i, &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();
}

SWITCH Statement
The switch statement is a built in multiple branch decision statement
where variable is successively tested against a list of integer or character constants.
When a match is found, the statement or statement associated with the constant is
executed. The syntax for the switch statement is

switch (<variable>)
{
case <value 1>:
<statement sequence >;
break;
case <value 2 >:
<statement sequence >;

EXAMPLE:
switch (no)
{
case 1:
printf (No. 1);
break;
case a:
printf (Alphabetic);
break;
case *:
printf(Neither an alpha nor a
numeric)

break;
.
.
case <value n >:
<statement sequence >;
break;

break;
default:
printf(You need to enter another
value);

default:
<statement sequence >;

}
where:
variable an int or char expression also called selector
value an expression to be matched with a case label
statement sequence action associated with preceding cases
break statement used to terminate the statement sequence associated with
each value. If break is omitted, execution will continue until the next case
statement.
default statement is the main statement to be executed if no matches are
found in the case. It is optical and therefore if it is not present, no action takes
place if all matches fail.

More examples:
1. Make a C program that will input two integers and display the larger
number.
#include<stdio.h>
#include<conio.h>
int num1,num2;
main()
{
printf("Enter num1: ");
scanf("%d",&num1);
printf("Enter num2: ");
scanf("%d",&num2);
if (num1>num2)
printf("Larger number is %d",num1);
else if(num1<num2)
printf("Larger number is %d",num2;)
else
printf("You entered equal numbers");
getch();
}
2. Construct a program in C language that will input that a character (R,
G, or B). Do the following output respect to the input.
CHARACTER
MESSAGE
R
Good Morning
G
Good Afternoon
B
Good Evening
#include<stdio.h>
#include<conio.h>
char color;
main()
{
printf("Enter character [R,G,B]: ");
scanf("%c",&color);
if (color=='R')
{
printf("Good Morning");
}
else if(color=='G')
{
printf("Good Afternoon");
else if(color=='B')

{
printf("Good Evening");
}
else
printf("R,G,B only!");
getch();
}
3. Create a program in C language that will input an integer [1..7] and display
its equivalent day.
#include<stdio.h>
#include<conio.h>
int day;
main()
{
printf("Enter a day: ");
scanf("%d",&day);
switch(day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("1 to 7 only!");
}
getch();
}

Machine Problems:
1. Construct a C program that will enter three integers and display the highest
value, assuming all inputs are different.
2. Any customer whose total PURCHASE is at least P1000 will be given a 10%
discount. Make C program that would input the customers purchase and output
his net BILL
3. A driver will be fined P500 if his SPEED goes beyond 80 kph, and P800 if it
goes beyond 100 kph. Design a program that will input the cars speed and
output the fine. (0 for NONE)
4. A student GRADE may be calculated based on his SCORE. Construct a
program that will input student name and Score and will display its equivalent
Grade.
SCORE
GRADE
Below 60:
E
At least 60 but below 70:
D
At least 70 but below 80:
C
At least 80 but below 90:
B
At least 90:
A
5. Design a C program that will input two integers and a code [A, B, C, D].
Display the sum if the code entered is A, difference if B, product if C and
quotient if D. (Otherwise, output Error)
6. Create a program in C language that will input any integer from 1 to 100 and
display the equivalent value in words. Use a shortcut way to minimize the code
for this problem.
7. Design a program that will accept three positive integer values that will
represent angles of a triangle and print the classification of the triangle. If one
angle measures exactly 90 degrees you have right triangle; if the two angles are
equal the triangle is classified as isosceles triangle; if all angles are equal the
triangle is known as Equilateral/Equiangular triangle; otherwise the triangle is a
Scalene triangle. Assume that the following angles must be positive and the total
angles must be 180 degrees.
8. Design a program that will enter any Hindu Arabic numbers from 1 to 1000 and
display the equivalent value in Roman Numerals.
9. Ding Dong is a computer programmer for the state transportation department.
His first assignment is to computerize the process of automobile drive licensing.
He decides that the first thing the computer should do is to look at the applicants
age determine what of license can be possibly issued.
Here is what Ding Dong is thinking:

a. If the applicants age is less than 18, then the computer should print:
UNDERAGE
b. If the applicant is 18, then the computer should print:
STUDENT LICENSE POSSIBLE
c. If the applicant is older than 18, then the computer should print:
PROFESSIONAL LICENSE POSSIBLE
Before the license is released the applicant need to pass the drug test
examination. If the drug test result is positive, display HOLD LICENSE
otherwise display RELEASE LICENSE.
10. The 1988 United States federal Estimated Tax Schedule is the simplest in
recent times. It has 4 categories, Single, Head of the Family, Married (joint) &
Married (Separated). Create a C program that will input the category code,
number of dependents, and the taxable income and then calculate the tax.
Category
Category Code
Tax
Single
W
12.5% of first $17,850 plus 10%
of excess
Head of the family
X
12.5% of first $23,900 plus 10%
of excess
Married (Joint)
Y
12.5% of first $29,750 plus 10%
of excess
Married (Separated)
Z
12.5% of first $14,875 plus 10%
of excess
Number of dependent
1
2
3

Additional Exemption
$1,500
$2,300
$3,100

Prepared By:
Engr. Joan P. Lazaro
Chairman, CpE Department

Vous aimerez peut-être aussi