Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Slides:



Advertisements
Similar presentations
Pemrograman Dasar Control Flow Statements: Decision Making or Selection PTIIK - UB 1.
Advertisements

While loops.
Decisions If statements in C.
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
Visual Basic Statements Chapter 5. Relational Operators  OperationSymbol  Equal  =  Less than  <  Greater than  >  Not equal   Less than.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Chapter 3 Program Design And Branching Structures.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
ALGORITHMS AND FLOWCHARTS
 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 4. Loops and Character Manipulation Loops in FORTRAN are constructs that permits us to execute a sequence of statements more than once. Type of.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Chapter 6 Control Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3 Applications & Logical Constructs. 2 Application 1 Temperature Conversion: Write a program that will convert a Celsius temperature to the corresponding.
Chapter 3 Planning Your Solution
Review Algorithm Analysis Problem Solving Space Complexity
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Software Life Cycle What Requirements Gathering, Problem definition
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Branches and Program Design
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.
MATLAB 及其工程应用 MATLAB and It’s Engineering Application 主讲教师: 胡章芳
Chapter Making Decisions 4. Relational Operators 4.1.
Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester.
Quadratic Equations. PROGRAM QuadraticEquations_1 IMPLICIT NONE REAL A, B, C, Discriminant, Root_1, Root_2 ! Get the coefficients PRINT *, "Enter the.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
Concepts of Algorithms CSC-244 Unit Zero Pseudo code, Flowchart and Algorithm Master Prince Computer College Qassim University K.S.A.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
ALGORITHMS AND FLOWCHARTS. A typical programming task can be divided into two phases: Problem solving phase  produce an ordered sequence of steps that.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
Lecture 2: Introduction to Programming EEE2108: 공학프로그래밍 서강대학교 전자공학과 2011 학년도 2 학기 - Algorithms and Flowcharts -
Program design Program Design Process has 2 phases:
Chapter 4: Making Decisions.
ALGORITHMS AND FLOWCHARTS
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
Algorithms and Flowcharts
Complex integers? Here a and b are integers.
Chapter 4: Making Decisions.
ALGORITHMS AND FLOWCHARTS
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
2300 (11PM) September 21 Blue line is meridian..
Chapter 5: Control Structure
15-110: Principles of Computing
Flowcharts and Pseudo Code
Programming Concepts and Database
Loop Construct.
REPETITION Why Repetition?
Control Structures.
2015 January February March April May June July August September
Presentation transcript:

Chapter 3 IF & SELECT

Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting or skipping certain blocks in a program Branching can be done using: IF Statement SELECT CASE

IF.. statement IF construct A block of code is executed if-and-only-if a certain logical expression is true. IF (logical_expr) THEN Statement 1 Statement 2 … END IF If the logical_expression is true, the program executes the block of codes between IF & END IF

IF.. statement.FALSE..TRUE. IF (logical_expr) THEN Statement 1 Statement 2 … END IF

IF.. statement Example: Solving quadratic equation PROGRAM QUADRATIC IMPLICIT NONE …… IF (b**2 – 4*a*c < 0) THEN WRITE (*,*) There are two complex roots to this equation. END IF …… END PROGRAM

IF.. statement Example: Solving quadratic equation PROGRAM QUADRATIC IMPLICIT NONE …… IF (b**2 – 4*a*c < 0) THEN WRITE (*,*) There are two complex roots to this equation. END IF …… END PROGRAM.FALSE..TRUE.

IF, ELSE IF and ELSE IF: if true then execute, if false then skip What if we had other situations? Use ELSE IF & ELSE IF (logical_expr1) THEN Statement 1 Statement 2 … ELSE IF (logical_expr2) THEN Statement 1 Statement 2 … ELSE Statement 1 Statement 2 … END IF

IF, ELSE IF and ELSE.TRUE..FALSE. END IF Example:

IF, ELSE IF and ELSE Example: IF (b**2-4*a*c < 0.0) THEN WRITE(*,*) This equation has complex roots. ELSE IF ( b**2-4*a*c > 0.0 ) THEN WRITE(*,*) This equation has two distinct real roots. ELSE WRITE(*,*) This equation has two identical real roots. END IF

Example 3-2 (The Quadratic Equation) Problem Design and write a program to solve for the roots of a quadratic equation regardless of type. Reminder: (Design Procedure) 1.Problem statement 2.Defining inputs and outputs 3.Algorithm design 1.Task subtasks 2. pseudo code or/and flowchart 4.Turn algorithm into Fortran statements 5.Test

Example 3-2 (The Quadratic Equation) Problem Design and write a program to solve for the roots of a quadratic equation regardless of type. 1. Problem statement (make it more clear) Design and write a program that calculates the two roots of the quadratic equation a.x 2 +b.x+c=0. Depending on the three coefficients entered by the user the roots might be real or complex. Furthermore, the two real roots might be distinct or identical. The program should solve for all of these conditions and display the two roots and clearly state the type of the roots.

Example 3-2 (The Quadratic Equation) 2. Defining inputs and outputs Inputs Equation coefficients (real) a, b, c Outputs Roots (real) and statement that describe their type x 1, x 2 (complex, real distinct or identical)

Example 3-2 (The Quadratic Equation) 3. Design Algorithm Main tasks Read the input data (a,b,c) Calculate the roots Write the roots (x 1, x 2 )

Example 3-2 (The Quadratic Equation) 3. Design Algorithm Main tasks Read the input data (a,b,c) Calculate the roots Write the roots (x 1, x 2 ) Ask user to enter coefficients Read coefficient a, b, c Calculate discriminant: disc b * a * c IF disc > 0 THEN Calculate two distinct real roots Write the distinct real roots ELSE IF disc < 0 THEN Calculate complex roots Write the two complex roots ELSE Calculate one real root Write the repeated root END IF

Ask user to enter coefficients Read coefficient a, b, c Calculate discriminant: disc b * a * c IF disc > 0 THEN Calculate two distinct real roots Write the distinct real roots ELSE IF disc < 0 THEN Calculate complex roots Write the two complex roots ELSE Calculate one real root Write the repeated root END IF Ask user to enter coefficients Read coefficient a, b, c Calculate discriminant: disc b * a * c IF disc > 0 THEN x1 (-b+sqrt(disc))/(2. * a) x2 (-b-sqrt(disc))/(2. * a) Write message that equation has two distinct real roots Write x1 and x2 ELSE IF disc < 0 THEN real_part -b/(2. * a) Imag_part sqrt(abs(disc))/(2. * a) Write message that equation has two complex roots Write the two complex roots ELSE x1 -b/(2. * a) Write message that equation has two identical real roots Write x1 END IF

Ask user to enter coefficients Read coefficient a, b, c Calculate discriminant: disc b * a * c IF disc > 0 THEN x1 (-b+sqrt(disc))/(2. * a) x2 (-b-sqrt(disc))/(2. * a) Write message that equation has two distinct real roots Write x1 and x2 ELSE IF disc < 0 THEN real_part -b/(2. * a) Imag_part sqrt(abs(disc))/(2. * a) Write message that equation has two complex roots Write the two complex roots ELSE x1 -b/(2. * a) Write message that equation has two identical real roots Write x1 END IF

Ask user to enter coefficients Read coefficient a, b, c Calculate discriminant: disc b * a * c IF disc > 0 THEN x1 (-b+sqrt(disc))/(2. * a) x2 (-b-sqrt(disc))/(2. * a) Write message that equation has two distinct real roots Write x1 and x2 ELSE IF disc < 0 THEN real_part -b/(2. * a) Imag_part sqrt(abs(disc))/(2. * a) Write message that equation has two complex roots Write the two complex roots ELSE x1 -b/(2. * a) Write message that equation has two identical real roots Write x1 END IF 4. Turn it into Fortran statements

PROGRAM roots IMPLICIT NONE REAL :: a, b, c, disc, imag_part, real_part, x1,x2 WRITE (*,*) 'Enter the three coefficints a,b, and c : ' READ (*,*) a, b, c disc = b** *a * c IF (disc > 0.) THEN x1 = (-b + SQRT(disc))/(2. * a) x2 = (-b - SQRT(disc))/(2. * a) WRITE (*,*) ' The equation has two distict real roots.' WRITE (*,*) ' X1= ', x1, ' X2= ', x2 ELSE IF (disc < 0.) THEN real_part = (-b)/(2. *a) imag_part = SQRT(ABS(disc))/(2. * a) WRITE (*,*) ' The equation has two complex roots.' WRITE (*,*) ' X1= ', real_part, '+ i', imag_part, ' X2= ', real_part, '- i', imag_part ELSE x1 = (-b)/(2. * a) WRITE (*,*) ' The equation has two identical real roots.' WRITE (*,*) ' X1= X2 = ', x1 END IF END PROGRAM 4. Turn it into Fortran statements

GRADES EXAMPLE PROGRAM GRADES IMPLICIT NONE REAL :: GRADE WRITE (*,*) "ENTER YOUR GRADE (out of 100)" WRITE (*,*) "" READ (*,*) GRADE IF (GRADE > 95.0) THEN WRITE (*,*) 'THE GRADE IS A.' ELSE IF (GRADE > 86.0) THEN WRITE (*,*) 'THE GRADE IS B.' ELSE IF (GRADE > 76.0) THEN WRITE (*,*) 'THE GRADE IS C.' ELSE IF (GRADE > 66.0) THEN WRITE (*,*) 'THE GRADE IS D.' ELSE WRITE (*,*) 'THE GRADE IS F.' END IF END PROGRAM

IF, ELSE IF and ELSE READING ASSIGNMENT: EXAMPLE 3-3 (PAGE 97) You should read the above example which illustrates the steps of designing a program using IF statements Problem statement Defining inputs and outputs Algorithm design Task subtasks pseudocode and flowchart Turn algorithm into Fortran statements Test

Naming & Nested IFs PROGRAM mixup … IF (test1) THEN … END IF IF (test2) THEN … END IF IF (test3) THEN … END IF END PROGRAM mixup

Naming & Nested IFs PROGRAM mixup … outer: IF (test1) THEN … middle: IF (test2) THEN … inner: IF (test3) THEN … END IF inner … END IF middle … END IF outer … END PROGRAM mixup Nested IF ( one or more IF block inside another one)

Naming & Nested IFs PROGRAM mixup … outer: IF (test1) THEN … middle: IF (test2) THEN … inner: IF (test3) THEN … END IF inner … END IF middle … END IF outer … END PROGRAM mixup Naming IF ( up to 31 alphanumeric)

Naming & Nested IFs outer: IF (test1) THEN … middle: IF (test2) THEN … inner: IF (test3) THEN … END IF inner … END IF middle … END IF outer outer: IF (test1) THEN … middle: IF (test2) THEN … inner: IF (test3) THEN … END IF inner … END IF middle … END IF outer What is the advantage of naming IF blocks?

Naming & Nested IFs outer: IF (test1) THEN … middle: IF (test2) THEN … inner: IF (test3) THEN … END IF inner … END IF middle … END IF outer outer: IF (test1) THEN … middle: IF (test2) THEN … inner: IF (test3) THEN … END IF inner … END IF middle … END IF outer What is the advantage of naming IF blocks? END IF missing Compiler

Naming & Nested IFs PROGRAM GRADES IMPLICIT NONE REAL :: GRADE WRITE (*,*) "ENTER YOUR GRADE (out of 100)" WRITE (*,*) "" READ (*,*) GRADE IF (GRADE > 95.0) THEN WRITE (*,*) 'THE GRADE IS A.' ELSE IF (GRADE > 86.0) THEN WRITE (*,*) 'THE GRADE IS B.' ELSE IF (GRADE > 76.0) THEN WRITE (*,*) 'THE GRADE IS C.' ELSE IF (GRADE > 66.0) THEN WRITE (*,*) 'THE GRADE IS D.' ELSE WRITE (*,*) 'THE GRADE IS F.' END IF END PROGRAM PROGRAM GRADES IMPLICIT NONE REAL :: GRADE WRITE (*,*) "ENTER YOUR GRADE (out of 100)" WRITE (*,*) "" READ (*,*) GRADE IF (GRADE > 95.0) THEN WRITE (*,*) 'THE GRADE IS A. ELSE IF (GRADE > 86.0) THEN WRITE (*,*) 'THE GRADE IS B. ELSE IF (GRADE > 76.0) THEN WRITE (*,*) 'THE GRADE IS C. ELSE IF (GRADE > 66.0) THEN WRITE (*,*) 'THE GRADE IS D. ELSE WRITE (*,*) 'THE GRADE IS F. END IF END PROGRAM

Special IF construct One line statement that is equivalent to if block with one statement IF (logical _expr) Statement IF (mark > 95) grade= A

SELECT CASE Another branching method Used to select a block of code to execute based on the value of a single integer, character, or logical expression General Form: SELECT CASE (Case_expr) CASE (selector_1) Statement 1 Statement 2 … CASE (selector_2) Statement 1 Statement 2 … CASE DEFAULT Statement 1 Statement 2 … END SELECT

GRADES EXAMPLE PROGRAM GRADES IMPLICIT NONE INTEGER :: GRADE = 98 WRITE (*,*) "ENTER YOUR GRADE" WRITE (*,*) "" READ (*,*) GRADE SELECT CASE (NINT(GRADE)) CASE (101:) WRITE (*,*) 'GRADE CANNOT EXCEED 100%' CASE (95:100) WRITE (*,*) 'THE GRADE IS A.' CASE (86:94) WRITE (*,*) 'THE GRADE IS B.' CASE (76:85) WRITE (*,*) 'THE GRADE IS C.' CASE (66:75) WRITE (*,*) 'THE GRADE IS D.' CASE DEFAULT WRITE (*,*) 'THE GRADE IS F.' END SELECT END PROGRAM

SELECT CASE Forms of case selectors: (RANGES) CASE ( value1 : value2 ) value1 to value2 CASE ( value1 : ) value1 and above CASE ( : value2 ) value2 and below CASE ( value ) a single value CASE (value1, value2, value3, value4) a list of values

SELECT CASE Forms of case selectors: (RANGES) CASE ( 1 : 10 ) CASE ( 10 : ) CASE ( : 10 ) CASE ( 7 ) CASE ( 3, 4, 7) Reading assignment: Example 3-5 (Page 107): Selecting day of the week

Example: Month s Name

program month implicit none CHARACTER(LEN=9) :: month_name INTEGER :: month_number WRITE (*,*) "Enter the month of the year (1-12)" WRITE (*,*) "" READ (*,*) month_number SELECT CASE (month_number) CASE (1) WRITE (*,*) 'JANUARY' CASE (2) WRITE (*,*) 'FEBRAURY' CASE (3) WRITE (*,*) 'MARCH' CASE (4) WRITE (*,*) 'APRIL'

Example: Month s Name CASE (5) WRITE (*,*) 'MAY' CASE (6) WRITE (*,*) 'JUNE' CASE (7) WRITE (*,*) 'JULY' CASE (8) WRITE (*,*) 'AUGUST' CASE (9) WRITE (*,*) 'SEPTEMBER' CASE (10) WRITE (*,*) 'OCTOBER' CASE (11) WRITE (*,*) 'NOVEMBER CASE (12) WRITE (*,*) 'DECEMBER' CASE DEFAULT WRITE (*,*) Error: out of range' END SELECT end program