Control Structures Control structures z Control structures are
- Slides: 65
Control Structures
Control structures z. Control structures are statements that determine the flow of information through your program. z. There are not very many of them but they are VERY IMPORTANT z. They are the logical building blocks of every program you will ever write.
Outline of control structures z. Sequential z. Decision (selection) z. Loops (repetition)
Sequential control structures z. These are the simplest of all z. A program with sequential structure y. Does each statement in order y. It skips no statements y. 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 z. A decision, or selection, structure is one that allows you to choose from among several options. z. Decision structures allow you to skip some statements.
Types of decision/selection structures z. A. Logical IF z. B. Block IF y 1. Single alternative IF y 2. Double alternative IF y 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 z. It is all on one line z. It handles only one alternative z. It performs only one task z. Unlike other forms of the IF statement it does not use the keyword THEN or the keyword ENDIF
Algorithm example 1. 2. 3. 4. 4. 1 5. Declare variables Prompt for score Read score If (score >= 60) then print ‘Pass’ End
Flowchart Prompt for score Read score true Print ‘PASS’ Score >= 60 false
The single alternative IF z. Handles only one alternative (like the logical IF z. May include many tasks z. 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 z. Algorithm numbering y 3. 1 (first alternative) y 3. 1. x (associated tasks) y 3. 2 (second alternative) y 3. 2. x (associated tasks) z. The idea is that the program will execute the first alternative that has a true condition. z. Only 1 alternative is selected
Flowchart Prompt for score Read score true Print ‘PASS’ Score >= 60 false Print ‘FAIL’
Program example INTEGER score PRINT*, “Please enter test score” READ*, score IF (score. ge. 60) THEN 1 st alternative PRINT*, “PASS” ELSE 2 nd alternative PRINT*, “FAIL” ENDIF END
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. 0)) THEN PRINT*, “ERROR - negative number” ELSE ! Process good data in here ENDIF END
Nested loops z. Nested IF’s are situations where one IF statement is inside of another one. z. This is very common z. Make sure the inner IF is ended before the outer IF is yyou should not have overlapping structures!
Good nesting IF ((days. le. 0). or. (people. 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” ELSE PRINT*, “Pay as you go” ENDIF END
Invalid nesting IF ((days. le. 0). or. (people. 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” ELSE 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 Print ‘B’ Score >= 80 false true Print ‘C’ Score >= 70 false true Print ‘D’ Score >= 60 false Print ‘F’
Multiple alternative IF IF (score. ge. PRINT*, “A” ELSE IF (score PRINT*, “B” ELSE IF (score PRINT*, “C” ELSE IF (score PRINT*, “D” ELSE PRINT*, “F” ENDIF END 90) THEN. ge. 80) THEN. ge. 70) THEN. ge. 60) THEN
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. PRINT*, “A” ELSE IF (score PRINT*, “B” ELSE IF (score PRINT*, “C” ELSE IF (score PRINT*, “D” ELSE PRINT*, “F” ENDIF 90) THEN. ge. 80) THEN. ge. 70) THEN. ge. 60) THEN
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” ELSE PRINT*, “Pay as you go” ENDIF
Parking problem example IF ((days. le. 0). or. (people. 0)) THEN PRINT*, “ERROR - negative number” ELSE 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” ELSE PRINT*, “Pay as you go” ENDIF
Parking problem example IF ((days. le. 0). or. (people. 0)) THEN PRINT*, “ERROR - negative number” ELSE 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” ELSE PRINT*, “Pay as you go” ENDIF
Parking problem example IF ((days. le. 0). or. (people. 0)) THEN PRINT*, “ERROR - negative number” ELSE 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” ELSE PRINT*, “Pay as you go” ENDIF
Read permit, people, days true t f people >2 Cost =. 25 data > 0 t people =2 Cost =. 50 ERROR message f Cost =. 75 Total = days * cost t Permit Total > permit false f Paylot
A good test question: What is printed? INTEGER 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 z. A. z. B. z. C. z. D. z. E. z z num is less than 10 num equals 5 num is greater than 0 All of the above num is less than 10 num equals 5 num is greater than 0
Correct answer z. A. z. B. z. C. z. D. z. E. z z num is less than 10 num equals 5 num is greater than 0 All of the above num is less than 10 num equals 5 num is greater than 0
Why? z. Remember that with multiple alternative structures the first true choice is the one that gets done. z. All others get skipped. z. This makes more sense if you see a diagram of the process.
num = 5 true num < 10 false Print < 10 true Print = 5 num =5 false true Print > 0 num >0 false Print all
num = 5 true num < 10 The only way you can get here is if the first condition if false! false Print < 10 true Print = 5 num =5 false true Print > 0 num >0 false Print all
num = 5 true num < 10 false Print < 10 true Print = 5 num =5 false true Print > 0 num >0 false Print all
How selection decisions are made z. Selection decisions are based around comparisons of the relationships between the data values. z. Comparisons are made using relational operators.
Relational operators z OPERATOR y. Greater than or equal to y. Less than or equal to y. Equal to y. Not equal to f 77. gt. . ge. . lt. . le. . eq. . ne. f 90 > >= < <= == /=
Logical operators OPERATOR z y y y and or not f 77 and f 90. and. . or. . not.
Conditions and relational operators z. All IF statements work by evaluating a condition. z. Example: y. IF (num. gt. 0) THEN y. The condition is ‘num. gt. 0’ z. Every IF statement must have one set of parentheses around the entire condition. z. The condition evaluates to either true or false.
Conditional evaluations INTEGER num 1, num 2 num 1 = 10 num 2 = 20 IF (num 1. lt. num 2) THEN (10 < 20) (true)
Evaluation results INTEGER num 1, num 2 num 1 = 10 num 2 = 20 IF (num 1. lt. num 2) THEN PRINT*, “num 1 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 Output if condition is false num 1 is larger program over
Evaluation results INTEGER num 1, num 2 num 1 = 10 num 2 = 20 IF (num 1. lt. num 2) THEN PRINT*, “num 1 is larger” ELSE PRINT*, “num 1 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 Output if condition is false num 1 is larger program over num 1 is not larger program over
Complex expressions z. Sometimes it is necessary to mix both relational and logical operators in expressions. z. 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)) (false) . or. truth table true false true 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)) (true) . or. truth table true false true false
Evaluation results INTEGER length, height length = -10 height = 20 IF ((length. lt. 0). or. (height. lt. 0)) THEN ((true). or. (false)) (true) . or. truth table true false true false
Evaluation results INTEGER length, height length = -10 height = -20 IF ((length. lt. 0). or. (height. lt. 0)) THEN ((true). or. (true)) (true) . or. truth table true false true false
Evaluation results INTEGER length, height length = -10 height = -20 IF ((length. lt. 0). or. (height. lt. 0)) THEN ((true). or. (true)) (true) . or. truth table true false true false
Summary of. or. z. The only way you can get false is if both operands are false. z. If either one, or both are true, then the whole thing is true. . or. truth table true false true 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)) (false) . and. truth table true false true 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)) (false) . and. truth table true false true false
Evaluation results INTEGER length, height length = -10 height = 20 IF ((length. lt. 0). and. (height. lt. 0)) THEN ((true). and. (false)) (false) . and. truth table true false true 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)) (true) . and. truth table true false true false
Evaluation results INTEGER length, height length = -10 height = -20 IF ((length. lt. 0). and. (height. lt. 0)) THEN ((true). and. (true)) (true) . and. truth table true false true false
Summary of. and. z. The only way you can get true is if both operands are true. z. If either one, or both are false, then the whole thing is false. . and. truth table true false true false
- Antigentest åre
- Homology
- Hardware and control structures
- Statement level control structures
- Intro.php?aid=
- Setprecision in c++
- Repetition control structure in c++
- Types of control structures
- Loop in assembly language 8086
- Statement level control structures
- Control structures in php
- Iteration control structures
- If a do…while structure is used:
- Iteration control structures
- Entity life history
- Algorithm control structures
- Visual basic control structures
- Translational research institute on pain in later life
- Product vs process
- Fluids mechanics
- Stock control e flow control
- Control volume vs control surface
- Is lac operon positive or negative control
- Negative control definition
- Jelaskan tentang error control pada data link control?
- Control de errores
- Negative control vs positive control examples
- Flow and error control
- Scalar control vs vector control
- Komponen pada ltspice
- Wjec gcse computer science
- While loops and if-else structures
- Froude's number
- Virtualization structures
- Great saphenous vein course
- What are the main parts of flowering plants
- Episiotomy structures cut
- Bureaucratic structures
- Forces on structures grade 5
- What are homologous analogous and vestigial structures
- Mac group nims
- Analogous structures vs homologous
- Transition words for description text structure
- Art-labeling activity: figure 13.2
- A scientist plans to cut a segment of dna
- Content of popliteal fossa
- Ovarian structures
- Social structure examples
- Nervous sysytem
- Lower respiratory tract
- Thalamic nuclei
- Where is the limbic system located
- Text structures vs text features
- Struts and frame structures
- Teaching market structures with a competitive gum market
- Superior steel structures
- Understanding business management
- What are structures
- Structure definition in c
- Circular motion examples
- Natural mass structures
- Btechsmartclass c
- Stable structure examples
- Tongue epithelium
- Social structure examples
- Air supported pneumatic structures