Control Flow IF Statement Visualisation of the IF Statement IF... THEN... ELSE Construct Visualisation of the IF... THEN Construct Visualisation of the.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
ITEC113 Algorithms and Programming Techniques
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
ISBN Chapter 8 Statement-Level Control Structures.
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
Control Flow C and Data Structures Baojian Hua
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Statement-Level Control Structures Sections 1-4
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
UNIT II Decision Making And Branching Decision Making And Looping
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 4 Program Control Statements
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Conditional Control Flow Constructs. Sequential Control Flow Execution order follows the textual order straight line flow Many simple problems can not.
7/10/2007DSD,USIT,GGSIPU1 Basic concept of Sequential Design.
Flow of Control Part 1: Selection
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
ISBN Chapter 8 Statement-Level Control Structures.
sequence of execution of high-level statements
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
ITEC113 Algorithms and Programming Techniques
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 05 (Part III) Control Statements: Part II.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Decision Making and Branching (cont.)
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
CSI 3125, Preliminaries, page 1 Control Statements.
Fortran: Control Structures Session Three ICoCSIS.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Unsur-unsur Algoritma
The if…else Selection Statement
Fundamentals of PL/SQL part 2 (Basics)
Def: A control structure is a control statement and
Python: Control Structures
Chapter 5 - Control Structures: Part 2
Control Structures.
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Structured Program Development in C
Control Structures: for & while Loops
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Statement-Level Control Structures
Boolean Expressions to Make Comparisons
Loop Construct.
Repetition (While Loop) LAB 9
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
REPETITION Why Repetition?
LOOP Basics.
Presentation transcript:

Control Flow IF Statement Visualisation of the IF Statement IF... THEN... ELSE Construct Visualisation of the IF... THEN Construct Visualisation of the IF... THEN... ELSE Construct IF... THEN.... ELSEIF Construct Visualisation of IF... THEN.. ELSEIF Construct Nested and Named IF Constructs Conditional Exit Loops Conditional Cycle Loops Named and Nested Loops DO... WHILE Loops Indexed DO Loops Examples of Loop Counts Scope of DO Variables SELECT CASE Construct I Visualisation of SELECT CASE SELECT CASE Construct II

Control Flow Control constructs allow the normal sequential order of execution to be changed. Fortran 90 supports: conditional execution statements and constructs, (IF... and IF... THEN... ELSE... END IF); loops, (DO... END DO); multi-way choice construct, (SELECT CASE);

Control Flow IF Statement Example, IF (bool_val) A = 3 The basic syntax is, IF( ) If evaluates to.TRUE. then execute otherwise do not. For example, IF (x.GT. y) Maxi = x means `if x is greater than y then set Maxi to be equal to the value of x'. More examples, IF (a*b+c <= 47) Boolie =.TRUE. IF (i.NE. 0.AND. j.NE. 0) k = 1/(i*j) IF (i /= 0.AND. j /= 0) k = 1/(i*j) ! same

Control Flow Visualisation of the IF Statement Consider the IF statement IF (I > 17) Print*, "I > 17" this maps onto the following control flow structure,

Control Flow IF... THEN... ELSE Construct The block-IF is a more flexible version of the single line IF. A simple example, IF (i.EQ. 0) THEN PRINT*, "I is Zero" ELSE PRINT*, "I is NOT Zero" ENDIF note the how indentation helps. Can also have one or more ELSEIF branches: IF (i.EQ. 0) THEN PRINT*, "I is Zero" ELSE IF (i.GT. 0) THEN PRINT*, "I is greater than Zero" ELSE PRINT*, "I must be less than Zero" ENDIF Both ELSE and ELSEIF are optional.

Control Flow Visualisation of the IF... THEN Construct Consider the IF... THEN construct IF (I > 17) THEN Print*, "I > 17" END IF this maps onto the following control flow structure,

Control Flow Visualisation of the IF... THEN... ELSE Construct Consider the IF... THEN... ELSE construct IF (I > 17) THEN Print*, "I > 17" ELSE Print*, "I <= 17" END IF this maps onto the following control flow structure,

Control Flow IF... THEN.... ELSEIF Construct The IF construct has the following syntax, IF( )THEN [ ELSEIF( )THEN... ] [ ELSE ] END IF The first branch to have a true is the one that is executed. If none are found then the, if present, is executed. For example, IF (x.GT. 3) THEN CALL SUB1 ELSEIF (x.EQ. 3) THEN A = B*C-D ELSEIF (x.EQ. 2) THEN A = B*B ELSE IF (y.NE. 0) A=B ENDIF IF blocks may also be nested.

Control Flow Visualisation of IF... THEN.. ELSEIF Construct Consider the IF... THEN... ELSEIF construct IF (I > 17) THEN Print*, "I > 17" ELSEIF (I == 17) Print*, "I == 17" ELSE Print*, "I < 17" END IF this maps onto the following control flow structure,

Control Flow Nested and Named IF Constructs All control constructs can be both named and nested. outa: IF (a.NE. 0) THEN PRINT*, "a /= 0" IF (c.NE. 0) THEN PRINT*, "a /= 0 AND c /= 0" ELSE PRINT*, "a /= 0 BUT c == 0" ENDIF ELSEIF (a.GT. 0) THEN outa PRINT*, "a > 0" ELSE outa PRINT*, "a must be < 0" ENDIF outa The names may only be used once per program unit and are only intended to make the code clearer.

Control Flow Conditional Exit Loops Can set up a DO loop which is terminated by simply jumping out of it. Consider, i = 0 DO i = i + 1 IF (i.GT. 100) EXIT PRINT*, "I is", i END DO ! if i>100 control jumps here PRINT*, "Loop finished. I now equals", i this will generate I is 1 I is 2 I is I is 100 Loop finished. I now equals 101 The EXIT statement tells control to jump out of the current DO loop.

Control Flow Conditional Cycle Loops Can set up a DO loop which, on some iterations, only executes a subset of its statements. Consider, i = 0 DO i = i + 1 IF (i >= 50.AND. i <= 59) CYCLE IF (i > 100) EXIT PRINT*, "I is", i END DO PRINT*, "Loop finished. I now equals", i this will generate I is 1 I is I is 49 I is I is 100 Loop finished. I now equals 101 CYCLE forces control to the innermost active DO statement and the loop begins a new iteration.

Control Flow Named and Nested Loops Loops can be given names and an EXIT or CYCLE statement can be made to refer to a particular loop. 0|outa: DO 1| inna: DO 2|... 3| IF (a.GT.b) EXIT outa ! jump to line 9 4| IF (a.EQ.b) CYCLE outa ! jump to line 0 5| IF (c.GT.d) EXIT inna ! jump to line 8 6| IF (c.EQ.a) CYCLE ! jump to line 1 7| END DO inna 8| END DO outa 9|... The (optional) name following the EXIT or CYCLE highlights which loop the statement refers to. Loop names can only be used once per program unit.

Control Flow DO... WHILE Loops If a condition is to be tested at the top of a loop a DO... WHILE loop could be used, DO WHILE (a.EQ. b)... END DO The loop only executes if the logical expression evaluates to.TRUE.. Clearly, here, the values of a or b must be modified within the loop otherwise it will never terminate. The above loop is functionally equivalent to, DO; IF (a.NE. b) EXIT... END DO

Control Flow Indexed DO Loops Loops can be written which cycle a fixed number of times. For example, DO i1 = 1, 100, 1... ! i is 1,2,3,..., ! 100 iterations END DO The formal syntax is as follows, DO =, [, ] END DO The number of iterations, which is evaluated before execution of the loop begins, is calculated as MAX(INT(( - + )/ ),0) If this is zero or negative then the loop is not executed. If is absent it is assumed to be equal to 1.

Control Flow Examples of Loop Counts A few examples of different loops, upper bound not exact, loopy: DO i = 1, 30, 2... ! i is 1,3,5,7,...,29... ! 15 iterations END DO loopy negative stride, DO j = 30, 1, ! j is 30,28,26,...,2... ! 15 iterations END DO a zero-trip loop, DO k = 30, 1, 2... ! 0 iterations... ! loop skipped END DO missing stride -- assume it is 1, DO l = 1,30... ! i = 1,2,3,...,30... ! 30 iterations END DO

Control Flow Scope of DO Variables I is recalculated at the top of the loop and then compared with, if the loop has finished, execution jumps to the statement after the corresponding END DO, I retains the value that it had just been assigned. For example, DO i = 4, 45, 17 PRINT*, "I in loop = ",i END DO PRINT*, "I after loop = ",i will produce I in loop = 4 I in loop = 21 I in loop = 38 I after loop = 55 An index variable may not have its value changed in a loop.

Control Flow SELECT CASE Construct I Simple example SELECT CASE (i) CASE (3,5,7) PRINT*,"i is prime" CASE (10:) PRINT*,"i is > 10" CASE DEFAULT PRINT*, "i is not prime and is < 10" END SELECT An IF.. ENDIF construct could have been used but a SELECT CASE is neater and more efficient. Another example, SELECT CASE (num) CASE (6,9,99,66) ! IF(num==6.OR....OR.num==66) THEN PRINT*, "Woof woof" CASE (10:65,67:98) ! ELSEIF((num >= 10.AND. num <= 65).OR.... PRINT*, "Bow wow" CASE DEFAULT ! ELSE PRINT*, "Meeeoow" END SELECT ! ENDIF

Control Flow Visualisation of SELECT CASE Consider the SELECT CASE construct SELECT CASE (I) CASE(1); Print*, "I==1" CASE(2:9); Print*, "I>=2 and I<=9" CASE(10); Print*, "I>=10" CASE DEFAULT; Print*, "I<=0" END SELECT CASE this maps onto the following control flow structure,

Control Flow SELECT CASE Construct II This is useful if one of several paths must be chosen based on the value of a single expression. The syntax is as follows, [ : ] SELECT CASE ( ) [ CASE ( ) [ ] ]... [ CASE DEFAULT [ ] ] END SELECT [ ] Note, the must be scalar and INTEGER, LOGICAL or CHARACTER valued; the is a parenthesised single value or range, for example, (.TRUE.), (1) or (99:101); there can only be one CASE DEFAULT branch; control cannot jump into a CASE construct.