Академический Документы
Профессиональный Документы
Культура Документы
if clause
if - else statements
The nested if statements
The IF-ELSE-IF Ladder
The ! Operator
Hierarchy of operators - revisited
Working of all 3 logical operators :
Conditional operators
Activities
In-lab Exercises
Any Other
Remarks
Signature
Theory
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.
In C the following are the types of conditional statements:
The IF statement
The general form of the if statement is:
if (expression)
statement;
Where:
if is a reserve word in C
expression is relational or Boolean expression that evaluates to a TRUE (1) or FALSE
(0) value
statement may either be a single C statement or a block of C statements.
The general form of the if statement with block of statements is:
if (expression)
{
statement_sequence;
}
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 program that will output Congratulations you PASSED! if the
students grade is greater than or equal to 60.
#include<stdio.h>
int grade;
main( )
{
Output
Example 2.
Write a program that will ask for a price. If the price is greater than 1000,
compute a 10% discount from the original price. Display the computed
discount.
#include<stdio.h>
Output
float price, discount;
main( )
Enter value for price:
{
Discount is 200.00
printf(Enter value for price:);
scanf(%f, &price);
if(price > 1000)
{
discount = price * 0.10;
printf(Discount is %.2f, discount);
}
getch( );
}
2000
Example 3.
#include<stdio.h>
int grade;
main( )
{
printf(Enter student grade:);
scanf(%d, &grade);
if (grade >= 60)
printf(Congratulations you PASSED!);
else
printf(Sorry you FAILED!);
getch( );
}
Output
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
EL121 Computer Fundamentals
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 4. Write a program that reads in three numbers A, B and C and determine
which is the largest.
#include<stdio.h>
int A, B, C;
main( )
{
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( );
}
Output
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 5.
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( )
{
int day;
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( );
}
Output
Enter an integer: 4
Wednesday
The ! Operator
! ( y < 10 ) This means not y less than 10.
if ( ! flag ) This is another way of saying if ( flag == 0 )
Hierarchy of Operators Revisited
A Word of Caution
Common mistake #1
main( ) {
int i ;
printf ( "Enter value of i " ) ;
scanf ( "%d", &i ) ;
if ( i = 5 )
printf ( "You entered 5" ) ;
else
printf ( "You entered something other than 5" ) ;
}
Common mistake #2
Another common mistake while using the if
statement is to write a semicolon (;) after
the condition, as shown below:
main( ) {
int i ;
printf ( "Enter value of i " ) ;
scanf ( "%d", &i ) ;
if ( i == 5 ) ;
printf ( "You entered 5" ) ;
}
(a) Its not necessary that the conditional operators should be used only in arithmetic statements.:
Ex.: int i ;
scanf ( "%d", &i ) ;
( i == 1 ? printf ( "Amit" ) : printf ( "All and sundry" ) ) ;
Ex.: char a = 'z' ;
printf ( "%c" , ( a >= 'a' ? a : '!' ) ) ;
(b) The conditional operators can be nested as shown below.
int big, a, b, c ;
big = ( a > b ? ( a > c ? 3: 4 ) : ( b > c ? 6: 8 ) ) ;
(c) Check out the following conditional expression:
a>b?g=a:g=b;
This will give you an error Lvalue Required. The error can be overcome by enclosing the
statement in the : part within a pair of parenthesis. This is shown below:
a>b?g=a:(g=b);
In absence of parentheses the compiler believes that b is being assigned to the result of the
expression to the left of second =. Hence it reports an error.
Do It Yourself
1. Write a program that finds the smallest among the five integers inputted by the
user.
2. Write a program that asks the user for the hours worked for the week and the
hourly rate. The basic salary is computed as:
Salary = hours worked * hourly rate
Bonuses are given:
No. of hours > 45
Bonus of 500 pesos
No. of hours > 40 and <=45
Bonus of 250 pesos
No. of hours > 35 and <=40
Bonus of 150 pesos
Display the basic salary, bonus and the total salary (basic salary + bonus) for the
week.
3. Write a program that accepts five numbers from the user and displays the highest
and the lowest number. Assume that there are no duplicate values.
Output