You are on page 1of 10

Workshop 4

Testing Conditions

4.1 Objective
The objective is to learn how to construct a program structures for testing conditions by using if control
statement.

4.2 if Control Statement


• The if-control statement has the following format
if(Expression)
{
Statement;
}
• and it functions as follows
– If the Expression is true, the Statement is executed.
– otherwise (i.e. if the Expression is false), the Statement is not executed.
– The flow chart given in the Figure 4.1 also describe how if-Statement works.
– Note: The Expression is true, if it evaluates to a non-zero value. The Expression is false, if it
evaluates to zero.

Figure 4.1: Flow Chart of if statement

Exercise 4.1 Execute the program in Listing 4.1.


Exercise 4.2 Explain how if control statement works in the program in Listing 4.1.
Exercise 4.3 Modify the program in Listing 4.1 to classify if a student is pass a school subject, based on the
obtained marks being above 45 (out of 100).
1
Exercise 4.4 Execute the program in Listing 4.2, which is a modification to the program in Listing 4.1 to
test if user entered keyboard character is letter A.
Exercise 4.5 User input two integers x and y. What are if-statements that would display
(a) ”SAME” if they are equal.
(b) ”X is GREATER” if x is greater than y.
(c) ”X is LESSER” if x is lesser than y.

Listing 4.1: Demonstration of if. Income tax calculator I.

columns
columns
/* Income tax calculator I . co
columns
columns
if income is greater than or equal to Rs 600000 , then tax is 10%. co
*/
columns
columns co
columns
columns
# include < stdio .h > co
columns
columns co
columns
columns
int main ( void ) co
columns
columns
{ co
double annual_income , income_tax ;
columns
columns co
columns
columns co
columns
columns
printf ( " Enter your annnual Income [ Rs ] : " ) ; co
columns
columns
scanf ( " % lf " ,& annual_income ) ; co
columns
columns co
columns
columns
if ( annual_income > 600000) co
{
columns
columns co
columns
columns
income_tax = annual_income * 10.0/100.0; co
columnsprintf ( " \ nIncome Tax = Rs . %.2 lf \ n " , income_tax ) ;
columns co
columns
columns
} co
columns
columns co
columns
columns
return 0; co
columns
columns
} co

Listing 4.2: Demonstration of if. Did your entered the key A.

columns
columns
/* Keyboard character clasification I . co
columns
columns
Check if user entered character ’A ’ co
columns
columns
*/ co
# include < stdio .h >
columns
columns co
columns
columns co
columns
columns
int main ( void ) co
columns
columns
{ co
columns
columns
char cKbd ; co
columns
columns co
columns
columns
printf ( " Enter any key : " ) ; co
columns
columns
scanf ( " % c " , & cKbd ) ; co
columns
columns co
columns
columns
if ( cKbd == ’A ’) co
columns
columns
{ co
columns
columns
printf ( " \ nFirst letter of the English alphabet . " ) ; co
columns
columns
} co
columns
columns co
columns
columns
return 0; co
columns
columns
} co

4.3 if-else
• The if-else control statement has the following format
if(Expression)
{
Statement1;
}
else
2
{
Statement2;
}
• and it functions as follows
– If the Expression is true, the Statement1 is executed.
– otherwise (i.e. if the Expression is false), the Statemen2t is executed.
– The flow chart given in the Figure 4.2 also describe how if-Statement works.

Figure 4.2: Floawchart of if-else statement

Exercise 4.6 Execute the program in Listing 4.3.


Exercise 4.7 Explain how if control statement works in the program in Listing 4.3.
Exercise 4.8 Modify the program in Listing 4.3 to classify if a student is passed or failed a school subject,
based on the obtained marks being above 45 (out of 100) or not.
Exercise 4.9 Use if-else control statement to modify the program in Listing 4.2, to classify the user entered
key is letter A or not.
Exercise 4.10 User enters two integers. Display if they are equal or not.

Listing 4.3: Demonstration of if-else. Income tax calculator II.

columns
columns
/* Income tax calculator II . co
columns
columns
if income is greater than or equal to Rs 600000 , then tax is 10%. co
columns
columns
Otherwise , no tax co
columns
columns
*/ co
columns
columns
# include < stdio .h > co
columns
columns co
columns
columns
int main ( void ) co
{
columns
columns co
columns
columns
double annual_income , income_tax ; co
columns
columns co
columns
columns
printf ( " Enter your annnual Income [ Rs ] : " ) ; co
columns
columns
scanf ( " % lf " ,& annual_income ) ; co
columns
columns co
columns
columns
if ( annual_income > 600000) co
columns
columns
{ co
columns
columns
income_tax = annual_income * 10.0/100.0; co
columns
columns
printf ( " \ nIncome Tax = Rs . %.2 lf \ n " , income_tax ) ; co
}
columns
columns co
columns
columns
else co
columns
columns
{ co
columns
columns
printf ( " \ nNO TAX \ n " ) ; co
columns
columns
} co
3
columns
columns co
columns
columns
return 0; co
}
columns
columns co

4.4 if-else if-else


• The if-else if-else is the most general form of if control statement.
• It has the following format
if(Expression1)
{
Statement1;
}
else if(Expression2)
{
Statement2;
}
..
else if(Expressioni)
{
Statementi;
}
..
else if(ExpressionN)
{
StatementN;
}
else
{
StatementN+1;
}

• and it functions as follows

– Expressions are evaluated sequentially starting from Expression1.


– Statement, which corresponds to the first identified true expression, is executed.
– Eg. If Expression1 is false but Expression2 is true, then Statement2 is executed.
– Then non of the other expressions are even evaluated, and immediately ends the execution of the
if-else if-else control statement.
– Note 1: Therefore, all statements (Statement1, Statement2, ..) are mutually exclusive. That
means, always only one of the statements is executed.
– Note 2: Except the if(){}, all other components are optional.
– The flow chart given in the Figure 4.2 also describe how if-Statement works.

.
Exercise 4.11 Execute the program in Listing 4.4.

Exercise 4.12 Explain how if control statement works in the program in Listing 4.4.
Exercise 4.13 Execute the program in Listing 4.5.
Exercise 4.14 Explain how if control statement works in the program in Listing 4.5.
Exercise 4.15 Write a program to grade a student based on the obtained marks, in accordance with the
following table.

4
Figure 4.3: Flowchar of if-else if-else statement.

Marks Range Grade


85-100 A+
75-84 A
70-74 A-
65-69 B+
60-64 B
55-59 B-
50-54 C+
45-49 C
40-44 C-
0-39 E
Exercise 4.16 Use if-else control statement to modify the program in Listing 4.2, to classify the user entered
key is letter A, B, C, D or non of the above.

Listing 4.4: Demonstration of if-else if-else. Income tax calculator III.

columns
columns
/* Income tax calculator . co
columns
columns co
columns
columns
Income Rs | Tax co
=================|======
columns
columns co
columns
columns
above 1 ,000 ,000 | 12% co
columns
columns
800000 - 1000000 | 9% co
columns
columns
500000 - 799000 | 7% co
columns
columns
300000 - 499000 | 5% co
columns
columns 0 - 299000 | 0% co
columns
columns co
columns
columns
*/ co
columns
columns
# include < stdio .h > co
columns
columns co
int main ( void )
columns
columns co
columns
columns
{ co
columns
columns
double annual_income , income_tax ; co
columns
columns co
columns
columns
/* Input : Income */ co
columns
columns
printf ( " Income Tax Calculator \ n " ) ; co
columns
columns
printf ( " = = = = = = = = = = = = = = = == = = = = \ n " ) ; co
columns
columns
printf ( " Enter your annnual Income [ Rs ] : " ) ; co
columns
columns
scanf ( " % lf " , & annual_income ) ; co
columns
columns co
columns
columns
/* Calculate tax based on the income */ co
columns
columns
if ( annual_income >= 1000000) co
columns
columns
{ co
columns
columns
income_tax = annual_income * 12.0/100.0; co
columns
columns
} co
5
columns
columns
else if ( annual_income >= 800000) co
columns
columns
{ co
income_tax = annual_income * 9.0/100.0;
columns
columns co
columns
columns
} co
else if ( annual_income >= 500000)
columns
columns co
columns
columns
{ co
columns
columns
income_tax = annual_income * 7.0/100.0; co
}
columns
columns co
columns
columns
else if ( annual_income >= 300000) co
{
columns
columns co
columns
columns
income_tax = annual_income * 5.0/100.0; co
}
columns
columns co
columns
columns
else co
{
columns
columns co
columns
columns
income_tax = annual_income * 0.0/100.0; co
}
columns
columns co
columns
columns co
columns
columns
/* Display the Tax */ co
printf ( " \ nIncome Tax = Rs . %.2 lf \ n " , income_tax ) ;
columns
columns co
columns
columns co
return 0;
columns
columns co
columns
columns
} co

Listing 4.5: Demonstration of if-else if-else. Calculator with a menu.

columns
columns
/* Calculator . Simple Menu co
columns
columns
Input : x , y co
Menu : a - Add
columns
columns co
columns
columns s - Substract co
columns
columns q - Quit program co
columns
columns
if a is chosen , then z = x + y co
columns
columns
if s is chosen , then z = x - y co
columns
columns
if q is chosen , then end the program co
*/
columns
columns co
columns
columns
# include < stdio .h > co
columns
columns co
columns
columns
int main ( void ) co
columns
columns
{ co
columns
columns
double x , y , z ; co
columns
columns
char cKbd ; co
columns
columns co
columns
columns
/* Input : x , y */ co
columns
columns
printf ( " Calculator \ n " ) ; co
columns
columns
printf ( " ==========\ n " ) ; co
columns
columns
printf ( " Enter X : " ) ; co
columns
columns
scanf ( " % lf " ,& x ) ; co
columns
columns
printf ( " \ nEnter Y : " ) ; co
columns
columns
scanf ( " % lf " ,& y ) ; co
columns
columns co
columns
columns
/* Display Menu */ co
columns
columns
printf ( " \ nMenu :\ ta - Add ( X + Y ) \ n \ ts - Substract (X - Y ) \ n \ tq - Quit co
columns
columns
Program " ) ; co
columns
columns
printf ( " \ nEnter Your Choise : " ) ; co
columns
columns co
columns
columns
/* read the user ’s choise */ co
columns
columns
scanf ( " % c " ,& cKbd ) ; co
columns
columns co
columns
columns
/* Do the calculations */ co
if ( cKbd == ’a ’)
columns
columns co
columns
columns
{ co
columns
columns
printf ( " \ n X + Y = % lf \ n " , x + y ) ; co
columns
columns
} co
columns
columns
else if ( cKbd == ’s ’) co
6
columns
columns
{ co
columns
columns
printf ( " \ n X - Y = % lf \ n " , x - y ) ; co
}
columns
columns co
columns
columns
else if ( cKbd == ’q ’) co
{
columns
columns co
columns
columns
printf ( " \ nQuit the program \ n " ) ; co
columns
columns
} co
else
columns
columns co
columns
columns
{ co
printf ( " \ nError : Your choise is not in the menu \ n " ) ;
columns
columns co
columns
columns
} co
columns
columns co
columns
columns
return 0; co
}
columns
columns co

4.5 IF - Expressions with Logical Operators


.

4.5.1 Logical Operators


• C’s logical operators allows to combine two or more relational expressions into a single expression that
evaluates to either a non-zero value (true) or zero (false).
Eg. ”If it’s 7.00am and a weekday and not my vacation, ring the alarm”
• Expression1 && Expression2 true if only if both Expression1 and Expression2 are true; false other-
wise.
• Expression1 || Expression2 true if either Expression1 or Expression2 is true; false only if both are
false.
• E.g. (5==5) && (6!=2), (5>5) && (6<1), (2==1) && (5==5), (5==4)

Expression Evaluates to
(x+1==y)&& x>2 true
(x+1==y)&& x<2 false
(x+1==y) || x<2 true
(x+1>y) || y>2 false

Exercise 4.17 Execute the program in Listing 4.6. Explain how && operator works in the program.
Exercise 4.18 Execute the program in Listing 4.7. Explain how || operator works in the program.
Exercise 4.19 Write if-else if-else statements to check if user input keyboard character is
(a) is a digit or not.
(b) a simple letter or not.
(c) Check if user input keyboard character is a capital letter or not.
(d) Check if user input keyboard character is a letter (simple or capital).
(e) Check if user input keyboard character is not a digit.
(f) Check if user input keyboard character is not a letter.
(g) Check if user input keyboard character is not a letter or digit.

Listing 4.6: Demonstration of AND operator. Job qualification.

columns
columns
/* Minimum Job Qualification I co
columns
columns
Input : Height , Chest Size co
columns
columns
Selected if Height >=160 cm AND Chest >=90 cm co
columns
columns
*/ co
columns
columns
# include < stdio .h > co
columns
columns co
columns
columns
# define MIN_HEIGHT 160 co
7
columns
columns
# define MIN_CHEST 90 co
columns
columns co
int main ( void )
columns
columns co
columns
columns
{ co
int canHeight , canChest ;
columns
columns co
columns
columns co
columns
columns
/* Input : height and chest size */ co
printf ( " Minimum Job Criteria \ n " ) ;
columns
columns co
columns
columns
printf ( " = = = = = = = = = = = = = = = == = = = \ n " ) ; co
printf ( " Enter height of candidate [ cm ] : " ) ;
columns
columns co
columns
columns
scanf ( " % d " , & canHeight ) ; co
columns
columns co
columns
columns
printf ( " \ nEnter chest size of candidate [ cm ] : " ) ; co
scanf ( " % d " , & canChest ) ;
columns
columns co
columns
columns co
/* Check the Qualificaiton */
columns
columns co
columns
columns
if ( canHeight >= MIN_HEIGHT && canChest >= MIN_CHEST ) co
columns
columns
{ co
columnsprintf ( " \ nQUALIFIED \ n " ) ;
columns co
columns
columns
} co
else
columns
columns co
columns
columns
{ co
columnsprintf ( " \ nDISQUALIFIED \ n " ) ;
columns co
columns
columns
} co
columns
columns co
columns
columns
return 0; co
columns
columns
} co

Listing 4.7: Demonstration of OR operator. Job qualification.

/* Minimum Job Qualification


columns
columns co
columns
columns
Input : age co
columns
columns
Disqualified if age < 18 OR age > 45 co
columns
columns
*/ co
# include < stdio .h >
columns
columns co
columns
columns co
columns
columns
# define MIN_AGE 18 co
columns
columns
# define MAX_AGE 45 co
columns
columns co
int main ( void )
columns
columns co
columns
columns
{ co
columns
columns
int canAge ; co
columns
columns co
columns
columns
/* Input : height and chest size */ co
columns
columns
printf ( " Minimum Job Criteria \ n " ) ; co
columns
columns
printf ( " = = = = = = = = = = = = = = = == = = = \ n " ) ; co
columns
columns co
columns
columns
printf ( " Enter age of candidate [ years ] : " ) ; co
columns
columns
scanf ( " % d " , & canAge ) ; co
columns
columns co
columns
columns
/* Check the Qualificaiton */ co
columns
columns
if ( canAge < MIN_AGE || canAge > MAX_AGE ) co
columns
columns
{ co
columns
columns
printf ( " \ nDISQUALIFIED \ n " ) ; co
columns
columns
} co
columns
columns
else co
{
columns
columns co
columns
columns
printf ( " \ nQUALIFIED \ n " ) ; co
columns
columns
} co
columns
columns co
columns
columns
return 0; co
columns
columns
} co
8
4.6 Assignments
4.1 Write a simple calculator program which performs the following steps.
Step 1 Display the following on the console
Simple Calculator
=================
Step 2 Display the menu to choose the operator or quit the program.
Menu:
a - Add
s - Subtract
m - Multiply
d - Divide
p - Power of 2
r - Square Root
n - sin(x)
c - cos(x)
q - Quit Program
Choose the Operation:

Step 3 If q is pressed, immediately exit the program. Otherwise perform the following steps.
Step 4 Prompt for appropriate user input operand or operands depending on the operator type.
That is if the operation is one of p, r, n or c, user should be prompted for single number n.
Enter the First Number (n) :
Otherwise, in case any of a, s, m or d, user should be prompted for two numbers n1 and n2.
Enter the First Number (n1) :
Enter the Second Number (n2) :
Step 5 The operation of users choice is performed with appropriate error handling.
Step 6 The result is displayed on console with appropriate description.
Eg. If n1 is 2.5 and n2 is 3.75 then the results may be displayed as
The result of 2.5 + 3.75 is 6.25.
Step 7 Exit the program by displaying
End of the Calculator Program.

4.2 Display the day of the week Monday, Tuesday, . . . Sunday, for entered number between 1, 2 . . . 7, respec-
tively. Also check if the entered number corresponds to a working day or week end.
4.3 Write a program that calculates the area of 2D objects. The user can choose any of the objects; Rectangle,
Triangle, Circle, Square, through a menu, and then user also should input the appropriate parameters
required to calculate the area of the chosen object.
4.4 Write a program that calculates the volume of 3D objects. The user can choose any of the objects; Cylinder,
Sphere, Cuboid, Triangular Prism, Cone, and then user also should input the appropriate parameters
required to calculate the volume of the chosen object.
4.5 Read a positive integer N and determine whether N is even or odd.

4.6 Read three integer values from user. Display the greatest value.
4.7 Read three integer values from user. Display the middle value.
4.8 Read three integer values from user. Display them in ascending order.

4.9 Read an integer value from user. Display ”Congrats you won!” if user entered 1, 10 or 100.
4.10 Read three integer values from user. Display ”Congrats you won!” if user entered 1, 10 and 100 in the
order.
4.11 Ask the user to enter a number between 0 and 10. Use the || operator to warn the user if the entered
number is less than or equal to zero or greater than or equal to ten. Otherwise prompt ”Good Job!”.

4.12 Ask the user to enter a number between 0 and 10. Use the && operator to warn the user if the entered
number is less than or equal to zero or greater than or equal to ten. Otherwise prompt ”Good Job!”.

9
4.13 Ask the user to enter a number between 50 and 100 which is not equal to 75. Use the || operator to warn
the user if the entered number is invalid. Otherwise prompt ”Good Job!”.
4.14 Ask the user to enter a number between 50 and 100 which is not equal to 75. Use the && operator to warn
the user if the entered number is invalid. Otherwise prompt ”Good Job!”.
4.15 Classify if the user entered keyboard character is a digit, capital letter, simple letter.
4.16 Classify if the user entered keyboard character is not a digit or a capital letter or a simple letter.

10

You might also like