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

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
true (any other value but zero) false (zero) expression Statement 2
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible.
Chapter 4: Control Structures: Selection
1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 5 Structured Program.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Computer Science Department Relational Operators And Decisions.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving,
Selection Structures (if & switch statements) (CS1123)
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Basic Control Structures
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Control Structures Selection or Decision or Branching.
Selection Structures: if and switch Statements. 2 Selection Statements –In this chapter we study statements that allow alternatives to straight sequential.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Week 4 Program Control Structure
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
CPS120: Introduction to Computer Science Decision Making in Programs.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Chapter 4 Selection Structures: if and switch Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.
Chapter 4: Control Structures I
Chapter 4 – C Program Control
CNG 140 C Programming (Lecture set 3)
The if…else Selection Statement
Chapter 4 C Program Control Part I
Chapter 4: Control Structures I
Flow of Control.
Decisions Given hours worked and pay rate, calculate total pay
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
DKT121: Fundamental of Computer Programming
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Decisions Given hours worked and pay rate, calculate total pay
How to develop a program?
Control Structures: Selection Statement
Chapter 4: Control Structures I (Selection)
Control Structures Selection or Decision Branching.
Week 3 – Program Control Structure
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Control Structures: Selection Statement
REPETITION Why Repetition?
Presentation transcript:

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

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; }

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; }

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 OperatorMeaning <Less than >Greater than >= Greater than or equal <= Less than or equal ==equal

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

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.

(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 OperatorMeaning &&logical AND | |logical OR !Logical NOT For example: (x > 5) | | (x < -5) (x + y 10) (a + b) != (c - d)

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;

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; … }

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:

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:

if hours is less than 40, then do the following: 2.1 Set overtime equal to Calculate regular_pay = hour*rate. 2.3 Set overtime_pay equal to 0. else do the following: 2.4 Calculate overtime = hours 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.

#include 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;

overtime = hours ; 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); }

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;

For example: if (x>0) if (y != 0) z = x/y; else z = x*y; else z = y/x;

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;

For example: if(score =90) grade = ‘A’; else if(score =80) grade = ‘B’; else if(score =70) grade = ‘C’; else if(score =60) grade = ‘D’; else if(score <60) grade = ‘F’; else grade = ‘*’;

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;

case constant3: statement3; break; … default: default statements: } For example: switch(choice) { case 1: matrix_add(); break; case 2: matrix_subtract(); break;

case 3: matrix_multiply(); break; default: printf(“\n Invalid input”); } Any Questions