CIT 383: Administrative Scripting

Slides:



Advertisements
Similar presentations
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Advertisements

And other languages….  Selection Statements  Iterative Statements  Unconditional Branching  Not covered Guarded Commands.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
The Ruby Programming Language
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
CIT 383: Administrative Scripting
Chapter 3: Data Types and Operators JavaScript - Introductory.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Writing Methods.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
If statements while loop for loop
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Methods and Hashes.
And other languages….  Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros.
© ABB University - 1 Revision C E x t e n d e d A u t o m a t i o n S y s t e m x A Chapter 11 Structured Text Course T314.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Numbers.
Working with Loops, Conditional Statements, and Arrays.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Directories.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting DateTime.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
String and Lists Dr. José M. Reyes Álamo.
Information and Computer Sciences University of Hawaii, Manoa
Control Flow Constructs: Conditional Logic
Working with Collections of Data
Sequence, Selection, Iteration The IF Statement
CS-104 Final Exam Review Victor Norman.
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
JavaScript: Control Statements.
Week#2 Day#1 Java Script Course
Chapter 8: Control Structures
CIT 383: Administrative Scripting
Chapter 14 Introduction to Ruby.
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
Stacks.
T. Jumana Abu Shmais – AOU - Riyadh
String and Lists Dr. José M. Reyes Álamo.
A Robust Data Structure
CIT 383: Administrative Scripting
Statement-Level Control Structures
Stacks.
Chapter 4: Control Structures I (Selection)
Python Primer 1: Types and Operators
Code Refresher Test #1 Topics:
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
Using Decision Structures
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
The Selection Structure
Review of Previous Lesson
CIT 383: Administrative Scripting
CIT 383: Administrative Scripting
Lisp.
Controlling Program Flow
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

CIT 383: Administrative Scripting Flow Control and Arrays CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Topics Boolean Expressions Conditionals While Loops Creating Arrays Indexing Arrays Array Methods Iterators CIT 383: Administrative Scripting

CIT 383: Administrative Scripting What are true and false? false is false, the one member of the FalseClass nil, the one member of NilClass true is true, the one member of the TrueClass anything that’s not false, including 0, “”, NaN,... CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Comparison Operators Binary operators that return true or false. == equals != not equals > greater than >= greater than or equal to < less than <= less than or equal to CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Boolean Operators Operate on Boolean values. ! not not not (lower precedence) && and and and (lower precedence) || or or or (lower precedence) CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Boolean Expressions x == 0 y > 1 z <= 5 x == 0 && y > 1 x == 0 && y > 1 || z <= 5 is_x_zero = x == 0 big_y = y > 2**16 is_x_zero && big_y CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Conditionals if statement if statement with else clause elsif if expressions unless statement post-conditionals case statement CIT 383: Administrative Scripting

CIT 383: Administrative Scripting if statement expr code expression true false if expression code end expr color = hilight x%2 == 0 true false if x % 2 == 0 color = hilight end CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Differences from Java Parentheses not required around expression expression begins after if and space expression ends with newline If statement must be terminated by an end There is no punctuation like Java’s braces. CIT 383: Administrative Scripting

CIT 383: Administrative Scripting if statement with else if expression code1 else code2 end true false expression code1 code2 CIT 383: Administrative Scripting

Multiple conditionals with elsif if expression1 code1 elsif expression2 code2 . elsif expressionN codeN else code end if x == 1 name = “one” elsif x == 2 name = “two” elsif x == 3 name = “three” elsif x == 4 name = “four” else name = “lots” end CIT 383: Administrative Scripting

CIT 383: Administrative Scripting if expressions Ruby has no statements, only expressions. Everything returns a value, even if it’s nil. if returns the value of the last expression evaluated. Example: name = if x == 1 then “one” elsif x == 2 then “two” elsif x == 3 then “three” elsif x == 4 then “four” else “many” end CIT 383: Administrative Scripting

CIT 383: Administrative Scripting unless statement unless expression code end true expression false code expr unless expression code1 else code2 end CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Post-conditionals Pre-conditional form if expression then code end Equivalent post-conditional form code if expression Examples puts message if debug > 1 puts message if !quiet && verbose CIT 383: Administrative Scripting

Equivalent if statement Case statement Case statement Equivalent if statement name = case when x == 1 then “one” else “many” end name = if x == 1 then “one” elsif x == 2 then “two” elsif x == 3 then “three” elsif x == 4 then “four” else “many” end CIT 383: Administrative Scripting

Alternate case statement format name = case x when 1 then “one” when 2 then “two” when 3 then “three” when 4 then “four” else “many” end CIT 383: Administrative Scripting

CIT 383: Administrative Scripting while loop while expression code end false expression true code expr x = 0 while x < 10 puts x x = x + 1 end expr puts x x = x + 1 x < 10 true false x = 0 CIT 383: Administrative Scripting

CIT 383: Administrative Scripting until loop until expression code end true expression false code expr x = 0 until x > 10 puts x x = x + 1 end x = 0 true x > 10 false expr puts x x = x + 1 CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Arrays in Ruby a = [10, 20, 30, 40, 50, 60] a[0] 10 20 a[1] 30 a[2] a[3] 40 a[4] 50 a[5] 60 a[-1] CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Arrays of Strings Define the same way as arrays of integers a = [‘spam’, ‘and’, ‘eggs’, ‘for’, ‘breakfast’] Alternative syntax for strings w/o spaces: a = %w[spam and eggs for breakfast] a = %w|spam and eggs for breakfast| %W works like double quoted strings a = [“\t”, “\n”, “\r”] a = %W(\t \n \r) CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Creating Empty Arrays Use the [] notation a = [] Create like any other object a = Array.new Similar to str = “” str = String.new CIT 383: Administrative Scripting

Arrays of Different Types Ruby arrays can have data of different types a=[1,2.718,‘a string’,“\t”,[3,4]] Use like any other array a[0] == 1 a[1] == 2.718 a[2] == ‘a string’ a[3] == “\t” a[4] == [3, 4] CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Indexing Arrays Single indexes a[n] is nth element from beginning starting at 0 a[-n] is nth element from end starting at -1 Double indexes (slicing) a[0,1] == a[0] a[0,2] == [ a[0], a[1] ] a[1,3] == [ a[1], a[2], a[3] ] a[-2, 2] == [ a[-2], a[-1] ] CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Index Assignments Single index a[n] = 5 sets nth element to be 5 If a[n] isn’t already defined, extends array to be long enough to have an nth element. Double index a[0,2] = [10,20] sets first two elements to 10,20 a[0,0] = [1,2,3] inserts 3 elements at beginning a[0,2] = [] deletes first two elements of array CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Array Operations Concatenation a = [1,2,3] + [4,5] # [1,2,3,4,5] a = a + [6,7,8] # [1,2,3,4,5,6,7,8] Subtraction [1,2,3,4,5,6] – [2,3,4] # [1,5,6] Append a = [] a << 1 # [1] a << 2 << 3 # [1, 2,3] a << [4,5,6] # [1,2,3,[4,5,6]] CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Array Operations Union: | Concatenates then removes duplicates. Intersection: & Array of elements in both arrays, no duplicates. Examples a = [1,1,2,2,3,3,4] b = [5,5,4,4,3,3,2] a | b # [1,2,3,4,5] b | a # [5,4,3,2,1] a & b # [2,3,4] b & a # [4,3,2] CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Array Methods reverse Reverses order of array elements. sort Sorts array elements from lowest to highest All array elements must be of same type. sort! Previous methods return altered array. Methods ending in ! change array in place. CIT 383: Administrative Scripting

Using Arrays w/o Indexing We often need just a list of items to: Add items from the list. Remove items from the list. Perform an action for each item of the list. We don’t need to deal with indices since The 5th item isn’t special. We just want to add, remove, or do something for each item of the list. Leaves only one choice: add and remove from End of array (push and pop) Beginning of array (shift and unshift) CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Push and pop push adds an element to end of array. 10 a[0] a[0] 10 20 20 a.push(40) 30 30 a[-1] 40 a[-1] CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Push and pop pop removes an element from end of array. 10 a[0] a[0] 10 20 20 x = a.pop 30 a[-1] 30 a[-1] 40 x == 40 CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Shift and unshift unshift adds an element to start of array. 5 a[0] a[0] 10 10 20 a.unshift(5) 20 30 a[-1] 30 a[-1] CIT 383: Administrative Scripting

CIT 383: Administrative Scripting Shift and unshift shift removes an element f/ start of array. 10 a[0] a[0] 5 20 10 x = a.shift 30 a[-1] 20 a[-1] 30 x == 5 CIT 383: Administrative Scripting

CIT 383: Administrative Scripting The each iterator Each executes code for each item of array Assigns current element to |variable| Executes block of code. Sets current element to next element. Example: langs = %w(python ruby scheme) langs.each do |language| puts “#{language} is good.” end CIT 383: Administrative Scripting

CIT 383: Administrative Scripting The each iterator array.each do |var| code end n = 0 var = array[n] code false is n last element? n = n + 1 true CIT 383: Administrative Scripting

CIT 383: Administrative Scripting References Michael Fitzgerald, Learning Ruby, O’Reilly, 2008. David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, 2008. Hal Fulton, The Ruby Way, 2nd edition, Addison-Wesley, 2007. Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2nd edition, Pragmatic Programmers, 2005. CIT 383: Administrative Scripting