Branching Instructions1 Conditional Statements There are two forms of the IF instruction %if(x,y,z) –if condition x is true, then y, else z –Example: SET.

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
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.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter 8 . Sequence Control
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Estimating a VAR After making the necessary data transformations, you must define the equations to use in the VAR. Typically, you will use the following.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
UNIT II Decision Making And Branching Decision Making And Looping
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Conditional Statement
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Flow of Control Part 1: Selection
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
sequence of execution of high-level statements
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Control Structures sequence of execution of high-level statements.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Introduction to Java Java Translation Program Structure
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Control statements Mostafa Abdallah
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Control Flow Statements
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
CPS120: Introduction to Computer Science Decision Making in Programs.
Fortran: Control Structures Session Three ICoCSIS.
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.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
JavaScript, Sixth Edition
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.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 4 – C Program Control
Chapter 4 - Program Control
JavaScript: Control Statements.
Arrays, For loop While loop Do while loop
Chapter 4 - Program Control
Chapter 4 - Program Control
Chap 7. Advanced Control Statements in Java
REPETITION Why Repetition?
Looping and Repetition
Presentation transcript:

Branching Instructions1 Conditional Statements There are two forms of the IF instruction %if(x,y,z) –if condition x is true, then y, else z –Example: SET flag = %if(resids > 0, 1, 0) SET dummy = %if(t.GE.1992:1,1,0)

Branching Instructions2 If-Else IF condition 1 next statement or block executed if condition is true Examples 1. If x = = 0 display ‘Zero encountered’ 2. If x = = 0 { program statements } end if (needed when IF is not within a compiled section) ELSE IF condition n ELSE RATS performs the first IF or ELSE IF condition that is true. If all are false, RATS will perform ELSE (if present).

Branching Instructions3 Relational Operators RATS uses the standard relational operators –A == B A.EQ.B –A <> B A.NE.B –A > B A.GE.B –A < B A.LT.B –A >= B A.GE.B –A <= B A.LE.B –A.AND.B –A.OR.B

Branching Instructions4 If-Else Examples Example 1 IF COUNT>=5 DISPLAY 'COUNT IS GREATER THAN OR EQUAL TO 5' ELSE DISPLAY 'COUNT IS LESS THAN 5' END IF Example 2 Examples of both types of IF are in PHI_NO.PRG Example 3: Bootknown.prg

Branching Instructions5 Notes Series vs. Constants There is an important distinction between a series and a constant. A constant is a single number (i.e., a scalar). A series is a ‘vector’ of numbers. –compute a = 5 –compute a = 5.0 –set a = 5 –set a = 5.0 The first crates an integer a, the second creates the floating point number 5.0, the third creates a series with each element equal to the integer 5, the last creates a series with each element equal to the floating point number 5.0. You cannot switch a ‘character’ between a series and a constant. You can use multiple assignments on a COM statement –e.g., com a = 5, b = 3.2, c = d = f = 4. COM can perform all standard numerical operations

Branching Instructions6 Dates as Integers If CAL is used, each date can be expressed as an integer or in RATS date form. It is not necessary to use CAL. You can begin a program with: –ALLOCATE 100

Branching Instructions7 DATES as Integers: Example cal all 2003:2 open data c:\\gdp.xls data(format=xls,org=obs) / There are 226 observations between 1947:1 and 2003:2. The following statements are equivalent to those above: all 226 open data c:\\gdp.xls data(format=xls,org=obs) / To estimate a regression over the first 100 observations use: LIN rcons # constant trend t2

Branching Instructions8 Series as Integers Each series has its own sequence number. The number can be determined from the position of the series obtained from TABLE / Example from gdp.xls table Series Obs Mean Std Error Minimum Maximum RGDP RCONS RINV Here rcons is series 2

Branching Instructions9 Series as Integers II Anywhere RATS expects a series name, you can use the sequence number. –The series number is an integer –EXAMPLES using gdp.xls print / 1 3 [prints rgdp and rinv] print / 1 to 3[prints rgdp, rcons and rinv] lin 2 # constant 2{1} –To check the series number, you can also use: com snum = rgdp ; dis snum

Branching Instructions10 Series as Integers III Care must be taken if a series is on the right hand-side of a FRML, SET or COM instruction since RATS will interpret the integer as a scalar. On the right hand side use: [series]number –Example: You want to SET y equal to the log of series 2 –NOT: set y = log(2) –USE: set y = log([series]2)

Branching Instructions11 Rats Programming Language Loops 1.Do i = 1,100,increment program statements end do i Notes: The increment is optional BUT i must always take on integer values. The increment can be negative Loops can be nested BUT each must have its own end do. end do can be used instead of end do i Example: DO REGEND=1994:1,1994:12 LINREG Y 1980:1 REGEND # CONSTANT X1 X2 END DO

Branching Instructions12 Loops 1.Do i = 1,100,increment program statements end do i Notes: The increment is optional BUT i must always take on integer values. The increment can be negative Loops can be nested Example: DO LAST = 1994:1,1994:12 LINREG Y 1980:1 LAST # CONSTANT X1 X2 END DO

Branching Instructions13 Loops II Example 2 do ar = 1,3 do ma = 1,3 box(constant,ar=ar,ma=ma) y end do ma end do ar 2. DOFOR index = list of values end dofor The index can be almost any type of variable Example dofor j = rgdp rcons lin j ; # constant j{1} end dofor

Branching Instructions14 Loops III 3. WHILE condition { program statements } end while 4. UNTIL condition { program statements } end while 5. Branch label :label This is an unconditional jump 6. LOOP break end loop This will cycle forever until break is encountered

Branching Instructions15 Loops and Date Manipulation 1. DO REGEND=1994:1,1994:12 LINREG Y 1980:1 REGEND # CONSTANT X1 X2 END DO Is Equivalent to: 2. DO I =169,180 LINREG Y 1980:1 I # CONSTANT X1 X2 END DO I

Branching Instructions16 Loops and Series Manipulation Example 1: do i = 1,5 lin i # constant i{1} x end do i Example 2: Loops.prg: The program illustrates date and series manipulations using real exchange rate data.

Branching Instructions17 Introduction to Monte Carlo Analysis The goal is to recreate a random outcome many times is order to assess the probability distribution of the outcome. –In RATS you can create random numbers using: %ran(x) %uniform(x1,x2) Example: –SET x = %ran(1) –You can obtain the fractiles using: STATISTICS(FRACTILES) or FRACT.SRC NOTE: The DENSITY Instruction will estimate an entire density function –EXAMPLE: MONTE.PRG

Branching Instructions18 Introduction to Bootstrapping In bootstrapping, we typically perform a type of Monte Carlo analysis but we use a particular data set to generate the random variables. –Examples: bootgdp.prg

Branching Instructions19 Simulating a Single Equation SIMULATE 1 steps start # equation forecasts forstart Supplementary Information equation: the equation name or number forecasts: series for simulated values newstart: starting entry for storing values (default: start) Example: lin(define=eq1) y ; # constant y{1} SIMULATE :1 # eq1 fores Simulates a model with random Normally distributed shocks. An error in RATS 5.0 See Simulations.prg (USE RATSW)

Branching Instructions20 Simulating Multiple Equations You can create a model using SYSTEM or GROUP. and them use SIMULATE. One way to use SIM is: SIMULATE(model=model,results=output) * steps start VCM model = model to simulate results = vector[series] for result series This provides a VECTOR of SERIES which will be filled with the results. For instance, RESULTS=SIMULS will make SIMULS(i) the series of simulations for the ith equation in the model.

Branching Instructions21 Example Using SIMULATE system(model=var1) 1 to 3 vars x1 x2 x3 lags 1 to 4 det constant end(system) estimate(noprint,noftests,outsigma=v) do i = 1,1000 SIMULATE(Model=var1,results=outpt) 3 24 %sigma program statements involving outpt end do i