Вы находитесь на странице: 1из 9 Conditional Statements | Page 1

Experiment/Practical 1: Conditional Statements

 Name Date Section

Practical Objectives/Learning Outcomes:

1. if clause

2. if - else statements

3. The nested if statements

5. The ! Operator

6. Hierarchy of operators - revisited

7. Working of all 3 logical operators :

8. Conditional operators

 Activities Remarks Signature In-lab Exercises Any Other

EL121 Computer Fundamentals Conditional Statements | Page 2

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 student’s grade is greater than or equal to 60.

#include<stdio.h>

main( )

{

getch( );

}

Output

Enter student grade: 95 Congratulations you PASSED!

printf(“Congratulations you PASSED!”);

EL121 Computer Fundamentals Conditional Statements | Page 3

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>

float price, discount; main( )

{

printf(“Enter value for price:”); scanf(“%f”, &price); if(price > 1000)

{

discount = price * 0.10; printf(“Discount is %.2f”, discount);

}

}

getch( );

Output

Enter value for price: 2000 Discount is 200.00

The IF-ELSE statement The general form of the if-else statement is:

if (expression)

statement_1;

else

statement_2;

where:

if and else are reserve words in 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 C statement or a block of 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 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.

EL121 Computer Fundamentals Conditional Statements | Page 4

Example 3.

Write a program that will output “Congratulations you PASSED!” if the

Otherwise output, “Sorry

student’s grade is greater than or equal to 60. you FAILED!”

#include<stdio.h>

main( )

{

else

printf(“Sorry you FAILED!”);

}

getch( );

Output

Enter student grade: 50 Sorry you FAILED!

The 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 it can be difficult to know what else associates with what if.Fortunately, C provides a very simple rule for resolving this type of situation. In C, the else is linked to the closest preceding if that does not already have an else statement associated with it. Consider the following situations:

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

EL121 Computer Fundamentals Conditional Statements | Page 5

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

Enter three numbers: 7 11 5 B is the largest.

A common programming construct in 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:

if and else are reserve words in 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_1, statement_2 up to statement_else may either be a single C statement or a block of C statements. 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

EL121 Computer Fundamentals Conditional Statements | Page 6

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( );

}

The ! Operator

Output

Enter an integer: 4 Wednesday

! ( y < 10 ) This means “not y less than 10”.

if ( ! flag ) This is another way of saying if ( flag == 0 ) Hierarchy of Operators Revisited EL121 Computer Fundamentals Conditional Statements | Page 7

A Word of Caution

 Common mistake #1 Common mistake #2 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" ) ; } 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" ) ; } Output of two runs of this program Enter value of i 200 You entered 5 Enter value of i 9999 You entered 5 Why? Wat will be printed?

Working of all 3 logical operators : The Conditional Operators:

The conditional operators ? and : are sometimes called ternary operators since they take three arguments

Their general form is:

expression 1 ? expression 2 : expression 3

 Example 1: int x, y ; scanf ( "%d", &x ) ; The equivalent if statement will be: if ( x > 5 ) y = ( x > 5 ? 3 : 4 ) ; y = 3 ; This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y. else y = 4 ; Example 2: char a ; int y ; scanf ( "%c", &a ) ; y = ( a >= 65 && a <= 90 ? 1 : 0 ) ; // Here 1 would be assigned to y if a >=65

Conditional Operators guidelines:

EL121 Computer Fundamentals Conditional Statements | Page 8

(a) It‟s 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.

EL121 Computer Fundamentals Conditional Statements | Page 9

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 No. of hours > 40 and <=45 No. of hours > 35 and <=40

Display the basic salary, bonus and the total salary (basic salary + bonus) for the week.

Bonus of 500 pesos Bonus of 250 pesos Bonus of 150 pesos

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.

1. Write a program that will display “I’TS COLD!” if the temperature is less than 20, “I’TS HOT!” if the temperature is greater than 30, “COOL CLIMATE!” otherwise.

2. Write the flowchart of the following C program:

#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( ); }

EL121 Computer Fundamentals

Output

Enter three numbers: 7 11 5 B is the largest.