Home | About | Comments | Lectures | Notice | Registration
C Programming Main Page
Review Lesson Four
Introduction
This lesson will introduce the use of the if statement.
One way decision making can be accomplished in C is with the if statement.
You will learn :
A) General format of if statement.
B) Relational operators
C) Multiple if statements
D) Nested if
E) Compound if statements
F) Using Switch
Using If
The general format for the if statement is
if ( condition )
statement 1;
else
statement 2;
Braces should be used when multiple statements are involved.
if ( condition ) {
statement 1;
statement 2;
}
else
{
statement 3;
statement 4;
}
Relational Operators
The if statement is used together with relational operators.
Operator |
Meaning |
|
|
< |
Less than |
<= |
Less than or equal to |
= = |
Equal to |
> |
Greater than |
> = |
Greater than or equal to |
!= |
Not equal to |
|
|
/* Program 5. 1 using if */
#include <stdio.h>
#include <conio.h>
main( )
{
int marks;
clrscr();
printf(“Enter marks : ”);
scanf(“%d”,&marks);
if (marks > 64 )
printf(“PASS”);
else
printf(“FAIL”);
exit(0);
}
Multiple if statements
Complete program 5.2 , a skeletal program which gives students a grade depending on the marks entered.
Grade |
Range |
|
|
A |
85 - 100 |
B |
65 - 84 |
C |
50 - 64 |
D |
30 - 49 |
U |
< 30 |
|
|
/* Program 5. 2 multiple if statement */
main()
{
int marks;
printf(“Enter marks : ”);
scanf(“%d”,&marks);
if (marks > 85 )
printf(“A”);
else
if (marks > 64 )
printf(“B”);
}
Nested if statements
An if statement can be placed within another if statement thus making it a nested if statement.
The second statement will be tested only if the first is true.
/* Program 5. 3 Vacation program with Nested if statement */
#include<stdio.h>
#include<conio.h>
main( )
{
int mths;
char sex;
clrscr( );
printf("Enter gender ");
scanf("%c",&sex);
printf("Months employed :");
scanf("%d",&mths);
if( sex = = 'f'‘ )
if( mths > 12) {
printf("3 weeks leave & maternity if applicable");
}
else {
printf(" No leave");
}
if (sex = ='m')
if(mths >12)
printf("3 weeks leave only");
else
printf("No leave");
exit(0);
}
Compound If Statement
Logical operators such as and && and or | | can be combined with the if statement to form compound if statements as in program 5.4 which determines whether or not an employee is eligible for family allowance.
/* Program 5.4 Family Allowance
payable if employee has 36 mths service and is married
*/
#include<stdio.h>
#include<conio.h>
main( )
{
int mths;
char status, name[35];
clrscr( );
printf("Enter employee name ");
gets(name);
printf("Marital Status ( m or s )");
scanf("%c",&status);
printf("Enter months of service :");
scanf("%d",&mths);
if( mths >=36 && status =='m')
{
printf("Family allowance = 200.00");
}
else {
printf("No Family allowance for %s ",name);
}
exit(0);
}
Conditional Operator
A single statement can be used to express an entire condition. The format of this statement is :
condition ? true : false
An assignment statement can be written as
a = x < 2 5 ? b : c ;
if the value of x is less than 25, b will be assigned to a else c will be assigned to a.
/* Program 5.5 Positive Difference */
#include<stdio.h>
#include<conio.h>
main( )
{
float first, second, diff=0;
clrscr( );
printf("Enter a small or large value ");
scanf("%f",&first);
printf("Enter another number ");
scanf("%f",&second);
diff= ( first < second ? second - first : first - second);
printf("Positive difference = %6.2f ", diff);
exit(0);
}
Using Case and Switch
Multiple selections can be made using the case statement which can be compared to a multiple if condition. A skeletal menu program is provided to demonstrate the use of the switch statement.
/* Program 5.6 Simple Menu with case
*/
#include<stdio.h>
#include<conio.h>
main( )
{
int selection;
clrscr( );
printf("Simple Menu ");
printf("\n\n1) Find Average of Two Numbers");
printf("\n2) Find square of a number ");
printf("\n\nEnter your choice ===> ");
scanf("%d",&selection);
switch(selection)
{
case 1 : printf("\n\nYou selected Find the Average");
break;
case 2 : printf("\n\nYou selected to square a number");
break;
default : printf("\n\nChoose 1 or 2 only");
}
exit(0);
}
When using switch and case observe that break separates each option and the last option is default. In program 5.6 if the user selects any other value 1 or 2 then the message in the default statement is executed.
Assignment
Modify program 5.4 so that the program prints out weekly wages for an employee. If the employee works more than 10 hours overtime for the week he is paid double time for extra hours ( over 10 hours ). His regular overtime rate is time and a half. User must enter the number of hours worked and the hourly rate of pay for the employee.
An example
If Ralph Stewart worked 55 hours for the week and his hourly rate is $10.00 then the calculations should be as follows
Total hours worked : 55
Regular hours : 40 * 10 = 400
Regular overtime : 10 * 15 = 150
Double time : 5 * 20 = 100
Total Gross Pay = 400 + 150 + 100
The Tutor |
|
Home | About | Comments | Lectures | Notice | Registration