21
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures 2. Selection structures 3. Loop structures 4.1 The if Statement The if statement is a control statement that selects alternative statements for execution based on the result of a test. The simplest form of the if statement is

Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

Embed Size (px)

Citation preview

Page 1: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

Chapter 4

Control Structures: Selection

We introduced the three fundamental control structures from which all programs are developed:

1. Sequence structures

2. Selection structures

3. Loop structures

4.1 The if Statement

The if statement is a control statement that selects alternative statements for execution based on the result of a test. The simplest form of the if statement is

Page 2: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

if (condition)

statement;

The statement following the condition may be a single statement or a compound statement consisting of a number of single statements enclosed in braces.

If (condition)

{

statement1;

statement2;

...

statementN;

}

Page 3: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

An example of an if statement is

if (x < 0)

x = x -1;

An example of an if statement containing a compound statement is the following:

if ( count < max_items)

{

sum += count;

++count;

}

Page 4: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

4.2 Relational Operators

The decision regarding which statements are executed in an if statement is usually based on a comparison between two values. In addition to arithmetic operators, C provides relational operators, which allow us to compare two values.

( value1 ) relational operator ( value2 )

The relational operators are listed as follows.

Relational Operator Meaning

< Less than

> Greater than

>= Greater than or equal <= Less than or equal

== equal

Page 5: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

Example: Maximum and Minimum of Three Numbers

The program given computes the maximum and minimum of three numbers. Several if statements are employed to arrive at the maximum and minimum value.#include <stdio.h>

main()

{

float a,b,c,max,min;

printf(“\n Enter three number: “);

scanf((“%f%f%f”,&a,&b,&c);

max = a;

min = a;

if ( b > max )

max = b;

if ( c > max )

max = c;

Page 6: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

if ( b < min )

min = b;

if ( c < min)

min =c;

printf(“\n Maximum value = %f”, max);

printf(“\n Minimum value = %f”, min);

}

4.3 Logical Operators

Logical operators allow us to combine two or more relational expressions to build compound test conditions. The individual relational expressions are linked by the logical operator as shown here.

Page 7: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

(relational expression 1) logical operator (relation expression 2)

The compound expression is called a logical expression. The logical operator will be shown as follows.

Logical Operator Meaning

&& logical AND

| | logical OR

! Logical NOT

For example:

(x > 5) | | (x < -5)

(x + y < 20) && (a + b > 10)

(a + b) != (c - d)

Page 8: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

4.4 The if-else Statement

Many algorithms involve a choice between two alternative one set of statements to be executed if the condition is true and another to be executed if the condition is false. The if-else statement lets us choose between two alternative statements. The general form of the if-else statement is as follows.

If (condition)

statement1;

else

statement2;

Page 9: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

As with the if statement, the object of the if and else can be a single statement or a black of statements enclosed in braces. The general form of an if-else statement with compound statements as objects is:

if (expression)

{

statement1a;

statement1b;

}

else

{

statement2a;

statement2b;

}

Page 10: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

Example: Gross Salary of An Employee

problem statement: write a program to compute the gross salary for an employee given the number of hours worked and the hourly rate. If the number of hours worked is greater than 40, the hourly rate shall be 1.5 times the normal hourly rate for all overtime hours. The program should print the number of overtime hours, the regular salary, the overtime salary, and the gross salary for the employee.

Problem analysis: after reading in the number of hours worked and the hourly rate, we need to determine if the number of hours worked is greater than 40. If the number of hours worked is less than 40, the gross pay is obtained by simply multiplying the number of hours worked by the hourly rate. If the number of hours worked is greater than 40 we need to compute the overtime hours. The gross pay is then obtained from the following equation

Gross pay = 40*hourly rate+overtime hours*1.5*hourly rate

The data requirements for the problem are as follows:

Page 11: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

Input Variables

number of hours worked (float hours)

hourly rate (float rate)

Output Variables

number of overtime hours (float overtime)

regular salary (float regular_pay)

overtime salary (float overtime_pay)

gross salary (float gross_pay)

Algorithm

1. Read hours, rate.

2. Compute overtime hours, regular salary, and overtime salary as follows:

Page 12: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

if hours is less than 40, then do the following:

2.1 Set overtime equal to 0.

2.2 Calculate regular_pay = hour*rate.

2.3 Set overtime_pay equal to 0.

else do the following:

2.4 Calculate overtime = hours - 40.

2.5 Calculate regular_pay = 40*rate.

2.6 Calculate overtime_pay = overtime*1.5*rate.

3. Calculate gross_pay = regular_pay+overtime_pay,

4. Print rate,hours

5. Print overtime,overtime_rate,regular_pay.

Page 13: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

#include<stdio.h>

main()

{

float hours, rate, regular_pay, overtime,overtime_pay, gross_pay;

printf(“\n Enter number of hours worked: “);

scanf(“%f”,&hours);

printf(“ Enter hourly rate: “);

scanf(“%f”,&rate);

if ( hours <= 40)

{

regular_pay = hours*rate;

overtime = 0.0;

overtime_pay = 0.0;

}

else

{

regular_pay = 40.0*rate;

Page 14: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

overtime = hours - 40.0;

overtime_pay = overtime*1.5*rate;

gross_pay = regular_pay + overtime_pay;

}

printf(“\n Number of hour worked: %10.2f”, hours);

printf(“\n Hourly rate: %10.2f”, rate);

printf(“\n Number of overtime hours: %10.2f”, overtiem);

printf(“\n Overtime rate: %10.2f”, 1.5*rate);

printf(“\n Regular salary: %10.2f”, regular_pay);

printf(“\n Overtime salary: %10.2f”,overtime_pay);

printf(“\n Total salary: %10.2f”, gross_pay);

}

Page 15: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

4.5 Nested if Statements

The object of an if statement can be another if statement. A nested if statement is an if statement that is the object of either an if or and else as in:

if (expression1)

if (expression2)

statement1;

else

statement2;

else

statement3;

Page 16: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

For example:

if (x>0)

if (y != 0)

z = x/y;

else

z = x*y;

else

z = y/x;

Page 17: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

4.6 The else-if Construct

The statement following the else in an if-else statement can be another if statement. In fact, a series of if statements can be chained together, resulting in the following construction:

if (expression1)

statement1;

else if (expression2)

statement2;

else if(expression3)

statement3;

else if(expressionN)

statementN;

else

default statement;

Page 18: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

For example:

if(score<=100 && score>=90)

grade = ‘A’;

else if(score<=89 && score >=80)

grade = ‘B’;

else if(score<=79 && score>=70)

grade = ‘C’;

else if(score<=69 && score>=60)

grade = ‘D’;

else if(score <60)

grade = ‘F’;

else

grade = ‘*’;

Page 19: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

4.7 The switch And break Statements

The switch statement is a multiple branch decision statement in which one of several statement is executed depending on the value of an integer variable or expression. The need for this type of statement arises when the execution of one of many alternatives is to be selected. Although we can do this by using multiple if-else statements, it is usually more convenient to use the switch statement.

switch( integer expression)

{

case constant1:

statement1;

break;

case constant2:

statement2:

break;

Page 20: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

case constant3:

statement3;

break;

default:

default statements:

}

For example:

switch(choice)

{

case 1:

matrix_add();

break;

case 2:

matrix_subtract();

break;

Page 21: Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures

case 3:

matrix_multiply();

break;

default:

printf(“\n Invalid input”);

}

Any Questions