Control Structures.

Slides:



Advertisements
Similar presentations
IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
Advertisements

Decisions If statements in C.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Visual C++ Programming: Concepts and Projects
Decision Structures and Boolean Logic
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Chapter#3 Part1 Structured Program Development in C++
Week 4 Program Control Structure
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.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Decision making If.. else statement.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
CHAPTER 4 Selection CSEG1003 Introduction to Computing
EGR 2261 Unit 4 Control Structures I: Selection
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
The Selection Structure
Chapter 5: Control Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 4: Control Structures
Boolean Expressions and If
DKT121: Fundamental of Computer Programming
Topics The if Statement The if-else Statement Comparing Strings
SELECTION STATEMENTS (1)
Control Statement Examples
Lecture 2: Logical Problems with Choices
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Control Structure Senior Lecturer
Control Structures: for & while Loops
3 Control Statements:.
Decision making If statement.
Selection Control Structure
Summary Two basic concepts: variables and assignments Basic types:
Chapter 5: Control Structure
Chapter 4: Control Structures I (Selection)
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Decision I (if Statement)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Week 3 – Program Control Structure
Structured Program Development in C++
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Branching statements Kingdom of Saudi Arabia
Relational and Logical Operators
Repetition (While Loop) LAB 9
Relational and Logical Operators
DATA TYPES AND OPERATIONS
Selection Control Structure
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
ICS 101 Lab 3 Hossain Arif ICS Dept
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Control Structures

Control structures Control structures are statements that determine the flow of information through your program. There are not very many of them but they are VERY IMPORTANT They are the logical building blocks of every program you will ever write.

Outline of control structures Sequential Decision (selection) Loops (repetition)

Sequential control structures These are the simplest of all A program with sequential structure Does each statement in order It skips no statements It repeats no statements

Sequential control example

Program example: INTEGER score PRINT*, “Please enter test score” READ*, score PRINT*, “The score entered was”, score END

Flow chart START Prompt for score Read score Print score START

Decision structures A decision, or selection, structure is one that allows you to choose from among several options. Decision structures allow you to skip some statements.

Types of decision/selection structures A. Logical IF B. Block IF 1. Single alternative IF 2. Double alternative IF 3. Multiple alternative IF

Program example: the LOGICAL IF INTEGER score PRINT*, “Please enter test score” READ*, score IF (score .ge. 60) PRINT*, “PASS” END

Characteristics of the logical IF It is all on one line It handles only one alternative It performs only one task Unlike other forms of the IF statement it does not use the keyword THEN or the keyword ENDIF

Algorithm example 1. Declare variables 2. Prompt for score 3. Read score 4. If (score >= 60) then 4.1 print ‘Pass’ 5. End

Flowchart Prompt for score Read score true Score >= 60 Print ‘PASS’ false

The single alternative IF Handles only one alternative (like the logical IF May include many tasks Has block structure (IF…THEN, ENDIF)

Program example: The single alternative block IF INTEGER score PRINT*, “Please enter test score” READ*, score IF (score .ge. 60) THEN PRINT*, “PASS” ENDIF END BLOCK IF

Single alternative IF with multiple tasks INTEGER score PRINT*, “Please enter test score” READ*, score IF (score .ge. 60) THEN PRINT*, “Congratulations!” PRINT*, “You earned a passing grade.” PRINT*, “Time to email mom about” PRINT*, “how well you are doing in” PRINT*, “college this semester.” ENDIF END

Double alternative IF 1. Declare variables 2. Prompt for score 3. Read score 4. Process score 4.1 If (score >= 60) then 4.1.1 print ‘Pass’ 4.2 else 4.2.1 print ‘Fail’ 5. End

alternatives Algorithm numbering 3.1 (first alternative) 3.1.x (associated tasks) 3.2 (second alternative) 3.2.x (associated tasks) The idea is that the program will execute the first alternative that has a true condition. Only 1 alternative is selected

Flowchart Prompt for score Read score true false Score >= 60 Print ‘PASS’ Print ‘FAIL’

Program example INTEGER score PRINT*, “Please enter test score” READ*, score IF (score .ge. 60) THEN PRINT*, “PASS” ELSE PRINT*, “FAIL” ENDIF END 1st alternative 2nd alternative

Parking problem example INTEGER people, days REAL permit, cost, total PRINT*, “Please enter cost of permit” READ*, permit PRINT*, “How many days” READ*, days PRINT*, “Number of people in the car” READ*, people IF ((days .le. 0).or.(people .le. 0)) THEN PRINT*, “ERROR - negative number” ELSE ! Process good data in here ENDIF END

Nested loops Nested IF’s are situations where one IF statement is inside of another one. This is very common Make sure the inner IF is ended before the outer IF is you should not have overlapping structures!

Good nesting IF ((days .le. 0).or.(people .le. 0)) THEN PRINT*, “ERROR - negative number” ELSE calculate cost of daily pass calculate total cost of pay lot IF (total .gt. Permit) THEN PRINT*, “Buy permit” PRINT*, “Pay as you go” ENDIF END

Invalid nesting Intersecting control structures IF ((days .le. 0).or.(people .le. 0)) THEN PRINT*, “ERROR - negative number” ELSE calculate cost of daily pass calculate total cost of pay lot IF (total .gt. Permit) THEN PRINT*, “Buy permit” PRINT*, “Pay as you go” ENDIF END Intersecting control structures

Multiple alternative IF 1. Declare variables 2. Prompt for score 3. Read score 4. Process score 4.1 If (score >= 90) then 4.1.1 print ‘A’ 4.2 else if (score >= 80) then 4.2.1 print ‘B’ 4.3 else if (score >= 70) then 4.3.1 print ‘C’ 4.4 else if (score >= 60) then 4.4.1 print ‘D’ 4.5 else 4.5.1 print ‘F’ 5. End

Read score true Score >= 90 false Print ‘A’ true Score >= 80 false Print ‘B’ true false Score >= 70 Print ‘C’ true false Score >= 60 Print ‘D’ Print ‘F’

Multiple alternative IF IF (score .ge. 90) THEN PRINT*, “A” ELSE IF (score .ge. 80) THEN PRINT*, “B” ELSE IF (score .ge. 70) THEN PRINT*, “C” ELSE IF (score .ge. 60) THEN PRINT*, “D” ELSE PRINT*, “F” ENDIF END

Algorithm, program correspondence 4.1 If (score >= 90) then 4.1.1 print ‘A’ 4.2 else if (score >= 80) then 4.2.1 print ‘B’ 4.3 else if (score >= 70) then 4.3.1 print ‘C’ 4.4 else if (score >= 60) then 4.4.1 print ‘D’ 4.5 else 4.5.1 print ‘F’ IF (score .ge. 90) THEN PRINT*, “A” ELSE IF (score .ge. 80) THEN PRINT*, “B” ELSE IF (score .ge. 70) THEN PRINT*, “C” ELSE IF (score .ge. 60) THEN PRINT*, “D” ELSE PRINT*, “F” ENDIF

Parking problem: Processing good data IF (people .gt. 2) THEN cost = 0.25 ELSE IF (people .eq. 2) THEN cost = 0.50 ELSE cost = 0.75 ENDIF total = days * cost IF (total .gt. permit) THEN PRINT*, “Buy the permit” PRINT*, “Pay as you go”

Parking problem example IF ((days .le. 0).or.(people .le. 0)) THEN PRINT*, “ERROR - negative number” ELSE IF (people .gt. 2) THEN cost = 0.25 ELSE IF (people .eq. 2) THEN cost = 0.50 cost = 0.75 ENDIF total = days * cost IF (total .gt. permit) THEN PRINT*, “Buy the permit” PRINT*, “Pay as you go”

Parking problem example IF ((days .le. 0).or.(people .le. 0)) THEN PRINT*, “ERROR - negative number” ELSE IF (people .gt. 2) THEN cost = 0.25 ELSE IF (people .eq. 2) THEN cost = 0.50 cost = 0.75 ENDIF total = days * cost IF (total .gt. permit) THEN PRINT*, “Buy the permit” PRINT*, “Pay as you go”

Parking problem example IF ((days .le. 0).or.(people .le. 0)) THEN PRINT*, “ERROR - negative number” ELSE IF (people .gt. 2) THEN cost = 0.25 ELSE IF (people .eq. 2) THEN cost = 0.50 cost = 0.75 ENDIF total = days * cost IF (total .gt. permit) THEN PRINT*, “Buy the permit” PRINT*, “Pay as you go”

Read permit, people, days true false data > 0 t f ERROR message people > 2 Cost = .25 t f people = 2 Cost = .50 Cost = .75 Total = days * cost t Total > permit f Permit Paylot

A good test question: What is printed? INTEGER num num = 5 IF (num .lt. 10) THEN PRINT*, “num is less than 10” ELSE IF (num .eq. 5) THEN PRINT*, “num equals 5” ELSE IF (num .gt. 0) THEN PRINT*, “num is greater than 0” ELSE PRINT *, “all of the above” ENDIF

Multiple choice A. num is less than 10 B. num equals 5 C. num is greater than 0 D. All of the above E. num is less than 10 num equals 5 num is greater than 0

Correct answer A. num is less than 10 B. num equals 5 C. num is greater than 0 D. All of the above E. num is less than 10 num equals 5 num is greater than 0

Why? Remember that with multiple alternative structures the first true choice is the one that gets done. All others get skipped. This makes more sense if you see a diagram of the process.

num = 5 true num < 10 false Print < 10 true num = 5 false Print = 5 true false num > 0 Print > 0 Print all

num = 5 true num < 10 false Print < 10 true num = 5 false The only way you can get here is if the first condition if false! true num < 10 false Print < 10 true num = 5 false Print = 5 true false num > 0 Print > 0 Print all

num = 5 true num < 10 false Print < 10 true num = 5 false Print = 5 true false num > 0 Print > 0 Print all

How selection decisions are made Selection decisions are based around comparisons of the relationships between the data values. Comparisons are made using relational operators.

Relational operators OPERATOR f77 f90 Greater than .gt. > Greater than or equal to .ge. >= Less than .lt. < Less than or equal to .le. <= Equal to .eq. == Not equal to .ne. /=

Logical operators OPERATOR f77 and f90 and .and. or .or. not .not.

Conditions and relational operators All IF statements work by evaluating a condition. Example: IF (num .gt. 0) THEN The condition is ‘num .gt. 0’ Every IF statement must have one set of parentheses around the entire condition. The condition evaluates to either true or false.

Conditional evaluations INTEGER num1, num2 num1 = 10 num2 = 20 IF (num1 .lt. num2) THEN (10 < 20) (true)

Evaluation results INTEGER num1, num2 num1 = 10 num2 = 20 IF (num1 .lt. num2) THEN PRINT*, “num1 is larger” ENDIF PRINT*, “program over” END If the condition is true the task is performed. If the condition is false the task is not performed. Output if condition is true num1 is larger program over Output if condition is false program over

Evaluation results INTEGER num1, num2 num1 = 10 num2 = 20 IF (num1 .lt. num2) THEN PRINT*, “num1 is larger” ELSE PRINT*, “num1 is not larger” ENDIF PRINT*, “program over” END If the condition is true the task is performed. If the condition is false the false task is performed. Output if condition is true num1 is larger program over Output if condition is false num1 is not larger program over

Complex expressions Sometimes it is necessary to mix both relational and logical operators in expressions. In this case, it can be very difficult to decide what the computer is going to do.

Evaluation results INTEGER length, height length = 10 height = 20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN PRINT*, “ERROR - negative numbers” ELSE PRINT*, “The area is: “, length * height, “square feet” ENDIF END Output if condition is true ERROR - negative numbers Output if condition is false The area is: 200 square feet

Evaluation results INTEGER length, height length = 10 height = 20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN ((false) .or. (false)) .or. truth table (false) true true true true false true true false true false false false

Evaluation results INTEGER length, height length = 10 height = 20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN PRINT*, “ERROR - negative numbers” ELSE PRINT*, “The area is: “, length * height, “square feet” ENDIF END Output if condition is false The area is: 200 square feet

Evaluation results INTEGER length, height length = -10 height = 20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN ((true) .or. (false)) .or. truth table (true) true true true true false true true false true false false false

Evaluation results INTEGER length, height length = -10 height = 20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN ((true) .or. (false)) .or. truth table (true) true true true true false true true false true false false false

Evaluation results INTEGER length, height length = -10 height = -20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN ((true) .or. (true)) .or. truth table (true) true true true true false true true false true false false false

Evaluation results INTEGER length, height length = -10 height = -20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN ((true) .or. (true)) .or. truth table (true) true true true true false true true false true false false false

Summary of .or. The only way you can get false is if both operands are false. If either one, or both are true, then the whole thing is true. .or. truth table true true true true false true true false true false false false

Evaluation results of .and. INTEGER length, height length = 10 height = 20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN PRINT*, “ERROR - negative numbers” ELSE PRINT*, “The area is: “, length * height, “square feet” ENDIF END Output if condition is true ERROR - negative numbers Output if condition is false The area is: 200 square feet

Evaluation results INTEGER length, height length = 10 height = 20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN ((false) .and. (false)) .and. truth table (false) true true true true false false false false true false false false

Evaluation results INTEGER length, height length = 10 height = 20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN PRINT*, “ERROR - negative numbers” ELSE PRINT*, “The area is: “, length * height, “square feet” ENDIF END Output if condition is false The area is: 200 square feet

Evaluation results INTEGER length, height length = -10 height = 20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN ((true) .and. (false)) .and. truth table (false) true true true true false false false false true false false false

Evaluation results INTEGER length, height length = -10 height = 20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN ((true) .and. (false)) .and. truth table (false) true true true true false false false false true false false false

Now we have got a problem INTEGER length, height length = -10 height = 20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN PRINT*, “ERROR - negative numbers” ELSE PRINT*, “The area is: “, length * height, “square feet” ENDIF END Output if condition is false The area is: -200 square feet

Evaluation results INTEGER length, height length = -10 height = -20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN ((true) .and. (true)) .and. truth table (true) true true true true false false false false true false false false

Evaluation results INTEGER length, height length = -10 height = -20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN ((true) .and. (true)) .and. truth table (true) true true true true false false false false true false false false

Summary of .and. The only way you can get true is if both operands are true. If either one, or both are false, then the whole thing is false. .and. truth table true true true true false false false false true false false false