Arko Barman COSC 6335 Data Mining University of Houston.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Control Structure There are two kind of control structure in GWBASIC one is iteration/loop or repetitive and second make decision/condition. Iteration/Loop.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Do Loop The syntax of DO loop: DO variable = initial_value, final_value[, increment] [statements] END DO Example: PROGRAM LINES ! Illustration of DO-loops.
CS100J October 07, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Python Control of Flow.
Simple Python Loops Sec 9-7 Web Design.
The foreach LooptMyn1 The foreach Loop The foreach loop gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
CPS120 Introduction to Computer Science Iteration (Looping)
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.
6.2 For…Next Loops General Form of a For…Next Loop
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CSI 3125, Preliminaries, page 1 Control Statements.
3:00. 2:59 2:58 2:57 2:56 2:55 2:54 2:53 2:52.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
Learning Javascript From Mr Saem
Loop Control อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 8.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Chapter 9 Repetition.
Chapter 6: Loops.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Unit-1 Introduction to Java
Loops in Java.
loops for loops iterate over a given sequence.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Think What will be the output?
Top Fire Protection Services Ottawa available on Dubinskyconstruction
The Linux Command Line Chapter 29
Chapter 5 Repetition.
Decision statements. - They can use logic to arrive at desired results
For Loops October 12, 2017.
Outline Altering flow of control Boolean expressions
CSC115 Introduction to Computer Programming
Chapter 9 Control Structures.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Programs as Directions
A LESSON IN LOOPING What is a loop?
Program Flow.
Introduction to Computer Science
PROGRAM FLOWCHART Iteration Statements.
Stage 6 Maze: Loops.
Python While Loops.
Gavin Restifo March 1, 2019 Today: Repetition Part 2 - For Loops
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
Similarities Differences
Topic: Iterative Statements – Part 2 -> for loop
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
How to allow the program to know when to stop a loop.
Presentation transcript:

Arko Barman COSC 6335 Data Mining University of Houston

 Commonly used loop structures in R -  for  while  apply  repeat Other important keywords –  break  next (similar to continue in C/C++)  stop

Syntax: for(variable in sequence) { statements } Example 1: mydf <- iris myve <- NULL # Creates empty storage container for(i in seq(along=mydf[,1])) { myve <- c(myve, mean(as.numeric(mydf[i, 1:3]))) } myve

Example 2: x <- 1:10 z <- NULL for(i in seq(along=x)) { if (x[i]<5) { z <- c(z,x[i]-1) } else { stop("values need to be <5") } z Note the error message produced by stop. We can also use break.

 Syntax: while(condition) statements Example 1: z <- 0 while(z < 5) { z <- z + 2 print(z) } z

Example 2: z <- 0 while(z >= 0) { if(z >= 5) { break } z <- z + 2 print(z) } z Note break statement Beware of infinite loops!

 Syntax: apply(X, MARGIN, FUN, ARGs) X – array, matrix or data.frame MARGIN – 1 for rows, 2 for columns, c(1,2) for both FUN – one or more functions ARGs – possible arguments for functions Example 1: ## Example for applying predefined mean function apply(iris[,1:3], 1, mean)

Example 2: x <- 1:10 test <- function(x) { # Defines some custom function if(x < 5) { x-1 } else { x / x } } apply(as.matrix(x), 1, test) Example 3: x <- 1:10 apply(as.matrix(x), 1, function(x) { if (x<5) { x-1 } else { x/x } })

Syntax: repeat { statements break or stop with condition } Example: z <- 0 repeat { z 100) break() } Remember to insert loop terminating condition!

Syntax to define functions: myfct <- function(arg1, arg2,...) { function_body } Syntax to call functions: myfct(arg1=..., arg2=...)

 Things to remember:  Assignment is done with keyword function  Avoid using names of existing functions  Useful to provide default values for arguments Example 1: myfct <- function(x1, x2=5) { z1 <- x1/x1 z2 <- x2*x2 myvec <- c(z1, z2) return(myvec) }

Example 1 (continued): Find out the output for each! myfct myfct(x1=2, x2=5) myfct(2, 5) myfct(x1=2)

 Control utilities in functions:  return – terminates a function  stop – stops execution of function and prints error message  warning – prints warning message in unexpected situations Example: myfct =0) print(x1) else stop("This function did not finish, because x1 0") } myfct(x1=2) myfct(x1=-2)

Questions?