And other languages….  Selection Statements  Iterative Statements  Unconditional Branching  Not covered Guarded Commands.

Slides:



Advertisements
Similar presentations
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Statement-Level Control Structures
Chapter 8 Statement-Level Control Structures. 1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Python quick start guide
Statement-Level Control Structures
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
ISBN Chapter 8 Statement-Level Control Structures Sections 1-4.
CPS120 Introduction to Computer Science Iteration (Looping)
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
Control Structures. if-elsif-else semantically the same as C/C++ syntactically, slightly different. if ($a > 0){ print “\$a is positive\n”; } elsif ($a.
Control Structures Programs have 4 basic control structures:
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
ISBN Chapter 8 Statement-Level Control Structures.
1 CS Programming Languages Class 11 September 26, 2000.
sequence of execution of high-level statements
第八章 敘述層級的控制結構 (Statement-Level Control Structures)
Chapter 8: Statement-Level Control Structures
Control Structures sequence of execution of high-level statements.
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 7 Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
CPTG286K Programming - Perl Chapter 4: Control Structures.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
CPS120 Introduction to Computer Science Iteration (Looping)
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
W E E K F I V E Statement-Level Control Structures.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Def: A control structure is a control statement and
Control Flow Constructs: Conditional Logic
Loops in Java.
8.1 Introduction - Levels of Control Flow: 1. Within expressions
Dr. Vamsi Paruchuri University of Central Arkansas
Statement-Level Control Structures
Chapter 8: Control Structures
Flow of Control.
Statement-Level Control Structures
Flow of Control.
Statement-Level Control Structures
Control statements Simple statements Basic structured statements
Control Structures In Text: Chapter 8.
CIT 383: Administrative Scripting
Flow of Control.
Control Structures Programs utilize 4 basic control structures
Statement-Level Control Structures
Chapter8: Statement-Level Control Structures April 9, 2019
Program Flow.
Statement-Level Control Structures
Chapter 8: Statement Level Control Structures
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

and other languages…

 Selection Statements  Iterative Statements  Unconditional Branching  Not covered Guarded Commands

if expr code end # newline after expr # false or nil, don’t execute, else do # no () around expr # end required even if only one statement in code if expr then code end if expr code else code end if expr1 code elsif expr2 code2 … else code end

 Return value is last expression executed OR nil x=5 # topic: return values name = if x==1 then "Cyndi" else "Nancy" end puts name

if expr then code end  equivalent to: code if expr  Best practice: use when condition is trivial or normally true Perl also has this syntax

unless expr code end code unless expr tax = case income when income * 0.1 when income * 0.15 when income * 0.25 else income * 0.9 end also other forms of case, not covered Compare to switch. Consider language readabilty.

while expr do code end code while expr until expr do code end code until expr Pascal had repeat… until.

for var in collection do code end # do is optional, can use newline hash.each do |key, value| puts “#{key} => #{value}” end

 Integer.times  Enumerable.each  Enumerable.Map  Integer.upto  make use of yield (next slide)  Examples 2.times { puts “OK” } array.each {|x| puts x } [5,10,15].map { |x| x*x*x } factorial = 1 2.upto(20) { |x| factorial *= x}

 yield temporarily returns control from iterator to calling method  QUICK EXERCISE:  Trace the code on the next two slides. Turn in for class participation.  Format flexible… draw arrows etc, just show you understand

 yield temporarily returns control from iterator to calling method def test puts "You are in the method" yield puts "You are again back to the method" yield end test {puts "You are in the block"}  method must be invoked with a block (otherwise, what would you yield to?) from: You are in the method You are in the block You are again back to the method You are in the block

def test yield 5 puts "You are in the method test" yield 100 end test {|i| puts "You are in the block #{i}"} You are in the block 5 You are in the method test You are in the block 100 Java = pull (caller controls iteration), Ruby = push (iterator controls iteration)

 Silly code example. Write code that will display the modulo 15 of all numbers from 100 to 90. Your yield block should return two values (look up the syntax).  Example output: 100 modulo 15 is modulo 15 is 9 98 modulo 15 is 8 97 modulo 15 is 7 96 modulo 15 is 6 95 modulo 15 is 5 94 modulo 15 is 4 93 modulo 15 is 3 92 modulo 15 is 2 91 modulo 15 is 1  Nothing to submit.

 What’s really going on here? Language design: importance of blocks  Read:

Unconditional Branching  Transfers execution control to a specified place in the program  Represented one of the most heated debates in 1960’s and 1970’s  Well-known mechanism: goto statement  Major concern: Readability  Some languages do not support goto statement (e.g., Module-2, Java, Python, Ruby)  C# offers goto statement (can be used in switch statements)  Loop exit statements are restricted and somewhat camouflaged goto ’s

 Language Concepts selection/conditionals return value iteration unconditional branching  Ruby if/elsif conditional return value expression modifier unless while/until for each yield