Control Flow Structures

Similar documents
Data Structures STAT 133. Gaston Sanchez. Department of Statistics, UC Berkeley

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Chapter 2: Functions and Control Structures

8. Control statements

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Whenever R encounters a syntactically correct statement it executes it and a value is returned

Flow Control. CSC215 Lecture

JME Language Reference Manual

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

LOOPS. Repetition using the while statement

REPETITION CONTROL STRUCTURE LOGO

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Java+- Language Reference Manual

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Getting started with simulating data in R: some helpful functions and how to use them Ariel Muldoon August 28, 2018

Lecture 6. Statements

Typescript on LLVM Language Reference Manual

Numeric Vectors STAT 133. Gaston Sanchez. Department of Statistics, UC Berkeley

PLT Fall Shoo. Language Reference Manual

C++ Programming: From Problem Analysis to Program Design, Third Edition

Programming for Experimental Research. Flow Control

C Syntax Out: 15 September, 1995

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Decision Making in C

YOLOP Language Reference Manual

SPARK-PL: Introduction

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Unit 3 Decision making, Looping and Arrays

Programming for Electrical and Computer Engineers. Loops

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

R is a programming language of a higher-level Constantly increasing amount of packages (new research) Free of charge Website:

Control Structures in Java if-else and switch

Operators. Java operators are classified into three categories:

STUDENT LESSON A12 Iterations

LECTURE 5 Control Structures Part 2

QUIZ: What value is stored in a after this

Intro. Scheme Basics. scm> 5 5. scm>

Loopy stuff: for loops

CHAPTER 4 CONTROL STRUCTURES

CGS 3066: Spring 2015 JavaScript Reference

ITS Introduction to R course

Control Structures in Java if-else and switch

1 Lecture 5: Advanced Data Structures

Lecture 10: for, do, and switch

Chapter 4. Flow of Control

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

The SPL Programming Language Reference Manual

Python Programming: An Introduction to Computer Science

Pace University. Fundamental Concepts of CS121 1

Statistical Computing (36-350)

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

Java Basic Programming Constructs

TED Language Reference Manual

CS112 Lecture: Repetition Statements

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

C++ Programming Lecture 1 Software Engineering Group

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Stat 579: Objects in R Vectors

What is PHP? [1] Figure 1 [1]

Lecture 10. Daily Puzzle

Lecture 3: Basics of R Programming

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

MODULE 2: Branching and Looping

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

CS112 Lecture: Loops

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Theory of control structures

Programming Lecture 4

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

The Very Basics of the R Interpreter

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Programming in C++ PART 2

Statistical Programming with R

STAT 540: R: Sections Arithmetic in R. Will perform these on vectors, matrices, arrays as well as on ordinary numbers

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

SECOND EDITION SAMPLE CHAPTER. First edition by Daryl K. Harms Kenneth M. McDonald. Naomi R. Ceder MANNING

CS 106 Introduction to Computer Science I

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

STATISTICS 579 R Tutorial : Programming in R

Data Frames and Control September 2014

Language Reference Manual

Chapter 4: Control Structures I (Selection)

Introduction to C Programming

Getting Started with R

Transcription:

Control Flow Structures STAT 133 Gaston Sanchez Department of Statistics, UC Berkeley gastonsanchez.com github.com/gastonstat/stat133 Course web: gastonsanchez.com/stat133

Expressions 2

Expressions R code is composed of a series of expressions assignment statements arithmetic expressions function calls conditional statements etc 3

Simple Expressions # assignment statement a <- 12345 # arithmetic expression 525 + 34-280 # function call median(1:10) 4

Expressions One way to separate expressions is with new lines: a <- 12345 525 + 34-280 median(1:10) 5

Grouping Expressions Constructs for grouping together expressions semicolons curly braces 6

Separating Expressions Simple expressions separated with new lines: a <- 10 b <- 20 d <- 30 7

Grouping Expressions Grouping expressions with semicolons: a <- 10; b <- 20; d <- 30 8

Grouping Expressions Grouping expressions with braces: { a <- 10 b <- 20 d <- 30 9

Grouping Expressions Multiple expressions in one line within braces: {a <- 10; b <- 20; d <- 30 10

Brackets and Braces in R Symbol Use [ ] brackets Objects ( ) parentheses Functions { braces Expressions 11

Brackets and Braces # brackets for objects dataset[1:10] 12

Brackets and Braces # brackets for objects dataset[1:10] # parentheses for functions some_function(dataset) 12

Brackets and Braces # brackets for objects dataset[1:10] # parentheses for functions some_function(dataset) # brackets for expressions { 1 + 1 mean(1:5) tbl <- read.csv('datafile.csv') 12

Test yourself Which is NOT a valid option for indexing data.frames A) Numeric vectors B) Logical vectors C) Missing values D) Empty indices E) Blank spaces 13

Expressions A program is a set of instructions Programs are made up of expressions R expressions can be simple or compound Every expression in R has a value 14

Expressions # Expressions can be simple statements: 5 + 3 ## [1] 8 # Expressions can also be compound: {5 + 3; 4 * 2; 1 + 1 ## [1] 2 15

Expressions The value of an expression is the last evaluated statement: # value of an expression {5 + 3; 4 * 2; 1 + 1 ## [1] 2 The result has the visibility of the last evaluation 16

Simple Expressions We use braces { to group the statements of an expression: # simple expression {5 + 3 ## [1] 8 For simple expressions there is really no need to use braces 17

Compound Expressions Compound expressions consist of multiple simple expressions Compound expressions require braces Simple expressions in a compound expression can be separated by semicolons or newlines 18

Compound Expressions Simple expressions in a compound expression separated by semicolons: # compound expression {mean(1:10); '3'; print("hello"); c(1, 3, 4) ## [1] "hello" ## [1] 1 3 4 19

Compound Expressions Simple expressions in a compound expression separated by newlines: # compound expression { mean(1:10) '3' print("hello") c(1, 3, 4) ## [1] "hello" ## [1] 1 3 4 20

Compound Expressions It is possible to have assignments within compound expressions: { x <- 4 y <- x^2 x + y ## [1] 20 21

Compound Expressions The variables inside the braces can be used in later expressions z <- {x <- 4; y <- x^2; x + y x ## [1] 4 y ## [1] 16 z ## [1] 20 22

Compound Expressions # simple expressions in newlines z <- { x <- 4 y <- x^2 x + y x ## [1] 4 y ## [1] 16 z ## [1] 20 23

Using Expressions Expressions are typically used in Flow control structures (e.g. for loop) Functions 24

Compound Expressions Do not confuse a function call (having arguments in multiple lines) with a compound expression # this is NOT a compound expression plot(x = runif(10), y = rnorm(10), pch = 19, col = "#89F39A", cex = 2, main = "some plot", xlab = 'x', ylab = 'y') 25

Control Flow Structures 26

Control Flow There are many times where you don t just want to execute one statement after another: you need to control the flow of execution. 27

Main Idea Execute some code when a condition is fulfilled 28

Control Flow Structures if-then-else switch cases repeat loop while loop for loop 29

If-Then-Else 30

If-Then-Else If-then-else statements make it possible to choose between two (possibly compound) expressions depending on the value of a (logical) condition. # if-then-else if (condition) expression1 else expression2 If condition is true then expression1 is evaluated otherwise expression2 is executed. 31

If-Then-Else If-then-else with simple expressions (equivalent forms): # no braces if (condition) expression1 else expression2 # with braces if (condition) { expression1 else { expression2 32

If-Then-Else If-then-else with compound expressions: # compound expressions require braces if (condition) { expression1 expression2... else { expression3 expression4... 33

Equivalent forms: # simple if-then-else if (5 > 2) 5 * 2 else 5 / 2 ## [1] 10 # simple if-then-else if (5 > 2) { 5 * 2 else { 5 / 2 ## [1] 10 Example: If-Then-Else 34

Example: If-Then-Else Equivalent forms: # simple if-then-else if (x > 0) y <- sqrt(x) else y <- abs(x) # simple if-then-else if (x > 0) { y <- sqrt(x) else { y <- abs(x) 35

If-Then-Else if() takes a logical condition the condition must be a logical value of length one it executes the next statement if the condition is true if the condition is false, then it executes the false expression 36

2 types of If conditions if-then-else # if and else if (condition) { expression_true else { expression_false Just if # simple if if (condition) { expression_true It is also possible to just have the if clause (without else) 37

Just If Just if # just if if (condition) { expression1... Equivalent to: # just if (else NULL) if (condition) { expression1... else NULL 38

If and Else if() takes a logical condition the condition must be a logical value of length one it executes the next statement if the condition is true if the condition is false, and there is no else, then it stops if the condition is false, and there is an else, then it executes the false expression 39

Reminder of Comparison Operators operation less than greater than less than or equal greater than or equal equality different usage x < x x > y x <= y x >= y x == y x!= y Comparison operators produce logical values 40

Reminder of Logical Operators operation usage NOT!x AND (elementwise) x & y AND (1st element) x && y OR (elementwise) x y OR (1st element) x y exclusive OR xor(x, y) Logical operators produce logical values 41

Just if s behavior # this prints if (TRUE) { print("it is true") # this does not print if (!TRUE) { print("it is not true") # this does not print if (FALSE) { print("it is false") # this prints if (!FALSE) { print("it is not false") 42

Just if x <- 7 if (x >= 0) { print("it is positive") ## [1] "it is positive" if (is.numeric(x)) { print("it is numeric") ## [1] "it is numeric" 43

If and Else y <- -5 if (y >= 0) { print("it is positive") else { print("it is negative") ## [1] "it is negative" The else statement must occur on the same line as the closing brace from the if clause! 44

If and Else The logical condition must be of length one! if (c(true, TRUE)) { print("it is positive") else { print("it is negative") ## Warning in if (c(true, TRUE)) {: the condition has length > 1 and only the first element will be used ## [1] "it is positive" 45

If and Else What s the length of the logical condition? x <- 3 y <- 4 if (x > 0 & y > 0) { print("they are not negative") else { print("they may be negative") ## [1] "they are not negative" 46

If and Else If there s is a single statement, you can omit the braces: if (TRUE) { print("it is true") if (TRUE) print("it is true") # valid but not recommended if (TRUE) print("it is true") 47

Equivalent ways No braces: # ok if (TRUE) print("it's true") With braces: # ok if (TRUE) {print("it's true") # valid but not recommended if (TRUE) print("it's true") # recommended if (TRUE) { print("it's true") 48

If and Else If there are multiple statements, you must use braces: if (x > 0) { a <- x^(2) b <- 3 * a + 34.8 - exp(2) 49

Multiple If s Multiple conditions can be defined by combining if and else repeatedly: set.seed(9) x <- round(rnorm(1), 1) if (x > 0) { print("x is positive") else if (x < 0) { print("x is negative") else if (x == 0) { print("x is zero") ## [1] "x is negative" 50

Vectorized ifelse() if() takes a single logical value. If you want to pass a logical vector of conditions, you can use ifelse(): true_false <- c(true, FALSE) ifelse(true_false, "true", "false") ## [1] "true" "false" 51

Vectorized If # some numbers numbers <- c(1, 0, -4, 9, -0.9) # are they non-negative or negative? ifelse(numbers >= 0, "non-neg", "neg") ## [1] "non-neg" "non-neg" "neg" "non-neg" "neg" 52

Test yourself Which option will cause an error: # A if (is.numeric(1:5)) { print('ok') # B if ("TRUE") { print('ok') # C if (0) print('ok') # D if (NA) print('ok') # E if ('yes') { print('ok') 53

Switch 54

Multiple Selection with switch() When a condition has multiple options, combining several if and else can become cumbersome 55

first_name <- "harry" if (first_name == "harry") { last_name <- "potter" else { if (first_name == "ron") { last_name <- "weasley" else { if (first_name == "hermione") { last_name <- "granger" else { last_name <- "not available" last_name ## [1] "potter" 56

Multiple Selection with switch() first_name <- "ron" last_name <- switch( first_name, harry = "potter", ron = "weasley", hermione = "granger", "not available") last_name ## [1] "weasley" 57

Multiple Selection with switch() the switch() function makes it possible to choose between various alternatives switch() takes a character string followed by several named arguments switch() will match the input string with the provided arguments a default value can be given when there s no match multiple expression can be enclosed by braces 58

Multiple Selection with switch() switch(expr, tag1 = rcode_block1, tag2 = rcode_block2,... ) switch() selects one of the code blocks, depending on the value of expr 59

Multiple Selection with switch() operation <- "add" result <- switch( operation, add = 2 + 3, product = 2 * 3, division = 2 / 3, other = { a <- 2 + 3 exp(1 / sqrt(a)) ) result ## [1] 5 60

Multiple Selection with switch() switch() can also take an integer as first argument in this case the remaining arguments do not need names instead, the they will have associated integers switch( 4, "one", "two", "three", "four") ## [1] "four" 61

Empty code blocks in switch() Empty code blocks can be used to make several tags match the same code block: student <- "ron" house <- switch( student, harry =, ron =, hermione = "gryffindor", draco = "slytherin") In this case a value of "harry", "ron" or "hermione" will cause "gryffindor" 62

Loops 63

About Loops Many times we need to perform a procedure several times The main idea is that of iteration For this purpose we use loops We perform operations as long as some condition is fulfilled R provides three basic paradigms: for, repeat, while 64

Repeat Loops 65

Repeat Loops repeat executes the same code over and over until a stop condition is met. repeat { keep_doing_something if (stop_condition) break The break statement stops the loops 66

Repeat Loops value <- 2 repeat { value <- value * 2 print(value) if (value >= 40) break ## [1] 4 ## [1] 8 ## [1] 16 ## [1] 32 ## [1] 64 If you enter an infinite loop, break it by pressing ESC key 67

Repeat Loops To skip a current iteration, use next value <- 2 repeat { value <- value * 2 print(value) if (value == 16) { value <- value * 2 next if (value > 80) break ## [1] 4 ## [1] 8 ## [1] 16 ## [1] 64 ## [1] 128 68

While Loops 69

While Loops It can also be useful to repeat a computation until a condition is false. A while loop provides this form of control flow while (condition) { keep_doing_something 70

While Loops while loops are like backward repeat loops; while checks first and then attempts to execute computations are carried out for as long as the condition is true the loop stops when the condition is false If you enter an infinite loop, break it by pressing ESC key 71

While Loops value <- 2 while (value < 40) { value <- value * 2 print(value) ## [1] 4 ## [1] 8 ## [1] 16 ## [1] 32 ## [1] 64 If you enter an infinite loop, break it by pressing ESC key 72

For Loops 73

For Loops Often we want to repeatedly carry out some computation a fixed number of times. For instance, repeat an operation for each element of a vector. In R this is done with a for loop 74

For Loops for loops are used when we know exactly how many times we want the code to repeat for (iterator in times) { do_something for takes an iterator variable and a vector of times to iterate through 75

For Loops value <- 2 for (i in 1:5) { value <- value * 2 print(value) ## [1] 4 ## [1] 8 ## [1] 16 ## [1] 32 ## [1] 64 76

For Loops The vector of times does not have to be a numeric vector; it can be any vector value <- 2 times <- c('1', '2', '3', '4', '5') for (i in times) { value <- value * 2 print(value) ## [1] 4 ## [1] 8 ## [1] 16 ## [1] 32 ## [1] 64 77

For Loops and Next statement Sometimes we need to skip a loop iteration if a given condition is met, this can be done with a next statement for (iterator in times) { expr1 expr2 if (condition) { next expr3 expr4 78

For Loops and Next statement x <- 2 for (i in 1:5) { y <- x * i if (y == 8) { next print(y) ## [1] 2 ## [1] 4 ## [1] 6 ## [1] 10 79

For Loops For loop # squaring values x <- 1:5 y <- x for (i in 1:5) { y[i] <- x[i]^2 Vectorized computation # squaring values x <- 1:5 y <- x^2 y ## [1] 1 4 9 16 25 y ## [1] 1 4 9 16 25 80

Nested Loops It is common to have nested loops for (iterator1 in times1) { for (iterator2 in times2) { expr1 expr2... 81

Nested Loops # some matrix A <- matrix(1:12, nrow = 3, ncol = 4) A ## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 2 5 8 11 ## [3,] 3 6 9 12 82

Nested Loops # nested loops for (i in 1:nrow(A)) { for (j in 1:ncol(A)) { if (A[i,j] < 6) A[i,j] <- 1 / A[i,j] A ## [,1] [,2] [,3] [,4] ## [1,] 1.0000000 0.25 7 10 ## [2,] 0.5000000 0.20 8 11 ## [3,] 0.3333333 6.00 9 12 83

For Loops and Vectorized Computations R for loops have a bad reputation for being slow Experienced users will tell you to avoid for loops in R (me included) R provides a family of functions that tend to be more efficient than loops (i.e. apply() functions) 84

For Loops and Vectorized Computations For purposes of learning programming (and flow control structures in R), I won t demonize R loops You can start solving a problem using a for loop Once you solved it, try to see if you can find a vectorized alternative It takes practice and experience to find alternative solutions to for loops There are cases when using for loops is inevitable 85

Repeat, While, For If you don t know the number of times something will be done you can use either repeat or while while evaluates the condition at the beginning repeat executes operations until a stop condition is met If you know the number of times that something will be done, use for for needs an iterator and a vector of times 86

For Loop This example is just for demo purposes (not recommended in R) # empty numeric vector x <- numeric(0) x ## numeric(0) # for loop to fill x for (i in 1:5) { x[i] <- i x ## [1] 1 2 3 4 5 87

For Loop If you know the number of times # empty numeric vector x <- numeric(5) x ## [1] 0 0 0 0 0 # for loop to fill x for (i in 1:5) { x[i] <- i x ## [1] 1 2 3 4 5 88

Quiz What happens if you pass NA as a condition to if()? What happens if you pass NA as a condition to ifelse()? What types of values can be passed as the first argument to the switch() function? How do you stop a repeat loop executing? How do you jump to the next iteration of a loop? 89