Control Structures 2013.10.21 Hara URL:

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

R for Macroecology Aarhus University, Spring 2011.
Microsoft® Small Basic
Introduction to arrays
M AT L AB Programming: scripts & functions. Scripts It is possible to achieve a lot simply by executing one command at a time on the command line (even.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
1 4. Computer Maths and Logic 4.2 Boolean Logic Boolean Operators.
Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Sampling Distribution of the Mean Problem - 1
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Repeated Measures Designs. In a Repeated Measures Design We have experimental units that may be grouped according to one or several factors (the grouping.
Session 3: More features of R and the Central Limit Theorem Class web site: Statistics for Microarray Data Analysis.
HOMEWORK REVIEW This is an if else statement layout if (condition) { code to be executed if condition is true; } else { code to be executed if condition.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Programming in R coding, debugging and optimizing Katia Oleinik Scientific Computing and Visualization Boston University
Linux Operations and Administration
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Slide 3-1 CHAPTER 3 Conditional Statements Objectives To learn to use conditional test statements to compare numerical and string data values To learn.
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
© Jalal Kawash Programming Peeking into Computer Science 1.
1 Module 3 Conditional Statements. Objectives  Conditional test statements to compare numerical and string data values  Looping statements to repeat.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Conditions in Java. First…Boolean Operators A boolean data type is always true or false. Boolean operators always return true or false For example: (x.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Repeated Measures Designs. In a Repeated Measures Design We have experimental units that may be grouped according to one or several factors (the grouping.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
CONTROL FLOW The order in which blocks are executed is called the “control flow” of the script So far all our scripts have just executed blocks in the.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Social Science Research Design and Statistics, 2/e Alfred P. Rovai, Jason D. Baker, and Michael K. Ponton Selecting Cases PowerPoint Prepared by Alfred.
1 An Introduction to R © 2009 Dan Nettleton. 2 Preliminaries Throughout these slides, red text indicates text that is typed at the R prompt or text that.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
COMP Loop Statements Yi Hong May 21, 2015.
Learning Javascript From Mr Saem
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
CS1371 Introduction to Computing for Engineers
Expressions and Control Flow in JavaScript
Recoding III: Introducing apply()
Recoding III: Introducing apply()
Logical Operations In Matlab.
Suggested Layout ** Designed to be printed on white A3 paper.
Computer Science Core Concepts
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
‘If’ statements, relational operators, and logical operators
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Comparing Data & the ‘switch’ Statement
If-Statements and If/Else Statements
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Control Structures Hara URL:

Contents 1 Conditional execution 1.1 Selecting subsets of vector 2 Loops 2.1 Implicit loops 2.2 Explicit loops

1.Conditional execution Define condition or situation when you want to activate the script which you want to execute. The most simple way is to use “if-else” function. How to do it? If the condition is TRUE, R activates this script. If the condition is FALSE, R activates this script. Specify the condition here ( in the first brackets).

Available parameters (symbols) for the condition x == y "x is equal to y" x != y "x is not equal to y“ ( ※ exclamation mark plus equal) x > y "x is greater than y" x < y "x is less than y" x <= y "x is less than or equal to y" x >= y "x is greater than or equal to y“ You may want to combine these above conditions using & or && for AND | or || for OR.

#example 1 x <- 2 # x gets the value 2 if(x==3){ print("This is true") } else { print("This is false") } #example 2 y <- 4 # y gets the value 4 if(x==2 && y>2){ print("x equals 2 and y is greater than 2") } else { print ("FALSE") } #example 3 if(x==2 && y<2){ print("x equals 2 and y is greater than 2") } Example First, R judge if the condition is TRUE or FALSE. Then, execute the script.

To simply activate this conditional execution, you can also use “ifelse” function !! Explanation) ifelse(condition, TRUE, FALSE) # first argument is the condition # second argument is the treatment if the condition is true # and third argument is the treatment if the condition is false #example 4 x <- 1:10 x ifelse(x 8, x, 0)

1-1. Selecting subsets of vector #example 5 # Let’s make a vector x<- runif(10) # runif(n) means a n random samplings from normal distribution between 0 to 1. x #check the vector, x. x<0.5 #judge if the x is less than 0.5 or not (R returns TRUE or FALSE) x[x<0.5] #We can ask R, whether x is less than 0.5 or not. #specify a subset #First, R judges the condition (TRUE or FALSE). R returns a vector of numbers when the x is less than 0.5 (condition is TRUE).

1-1. Selecting subsets of vector #example 6 # You can specify the inclusion or exclusion place of vector using [ ], or [- ]. x<- 1:10 # assign x a vector from 1 to 10. x #check x x[5] #specify inclusion places x[c(2,4,6,8,10)] #specify inclusion places x[-1:-5] #specify exclusion places

2. Loops Loop is a set of operations in a computer program that are continuously repeated. With loop function, R execute a script again and again and again … continuously. Two ways to do loop in R ① Use built-in function, “apply” family. → 2-1. Implicit loops ② Write loops using “for”, “repeat” and “while”. → 2-2. Explicit loops

2.1 Implicit loops Explanation ) apply ( x, 1 or 2, function) # first argument is matrix (or array) # second argument is 1 or 2. 1 indicates rows, and 2 indicates colums over which the function will be applied # third argument is function #example 7 # Let’s use “apply” function # make a matrix (anything is OK) → This is NOT a today’s main topic # just understand that’s just a way it is! N <- 10 x1 <- rnorm(N, mean = 0, sd = 1) x2 <- rnorm(N, mean = 0, sd = 1) + x1 + 1 male <- rbinom(N,1, 0.48) y <- 1 + x1 + x2 + male + rnorm(N) mydata <- data.frame(y,x1,x2,male)

#example 7 (continue) #check mydata mydata # script for calculation of mean in each colum apply (mydata, 2, mean) There are several “apply family” functions… “lapply” applies a function to each column and returns results as a list. “sapply” is similar but the output is simplified. It may be a vector or a matrix “tapply” applies the function for each level of a factor and returns results as a table #example 8 lapply (mydata, mean) sapply (mydata, mean) tapply (mydata$y, mydata$male, mean)

2.2 Explicit loops explanation) for (i in vector) { statement } repeat { statement if (condition) breaks } # if there is no breaks, statement will repeat forever. while (condition) { statement } There are 3 ways to write loops : “for”, “repeat” and “while”.

#example 9 for (i in 1:5) { print(i) } #example 10 repeat { g<-rnorm(1) if(g>1.0) break cat(g, "\n") } # “\n” means line break # cat outputs the object #example 11 g<-0 while (g<1) { g<-rnorm(1) cat (g, "\n") }