Control Structures. if-elsif-else semantically the same as C/C++ syntactically, slightly different. if ($a > 0){ print “\$a is positive\n”; } elsif ($a.

Slides:



Advertisements
Similar presentations
CSC 221: Computer Programming I Fall 2001  conditional repetition  repetition for: simulations, input verification, input sequences, …  while loops.
Advertisements

Statement-Level Control Structures
And other languages….  Selection Statements  Iterative Statements  Unconditional Branching  Not covered Guarded Commands.
More Perl Control Flow Software Tools. Slide 2 Control Flow l We have already seen several Perl control flow statements: n if n while n for n last l Other.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Control Flow C and Data Structures Baojian Hua
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Statement-Level Control Structures Sections 1-4
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
ISBN Chapter 8 Statement-Level Control Structures.
More Perl Control Flow Learning Objectives: 1. To learn more commands for control flow 2. To apply the new commands learnt for writing tidier program 3.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
More Perl Control Flow Learning Objectives: 1. To learn more commands for control flow 2. To apply the new commands learnt for writing tidier program 3.
1 Chapter Four Boolean Expressions and Control Statements.
Introduction to Computer Programming in c
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
ISBN Chapter 8 Statement-Level Control Structures Sections 1-4.
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.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
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.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
ISBN Chapter 8 Statement-Level Control Structures.
1 CS Programming Languages Class 11 September 26, 2000.
sequence of execution of high-level statements
Controlling Execution Dong Shao, Nanjing Unviersity.
CSI 3120, Control, page 1 Control statements Simple statements Basic structured statements Sequence Selection Iteration The jump statement.
Control Structures sequence of execution of high-level statements.
Introduction to Unix – CS 21
CPTG286K Programming - Perl Chapter 4: Control Structures.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
Programming Perl in UNIX Course Number : CIT 370 Week 3 Prof. Daniel Chen.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Scripting Languages ● Perl, Python, Unix Shell, VB, JavaScript, etc. ● Glue Languages. ● Typeless. ● Interchangeable Code and Data. ● Interpreted vs. Compiled.
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 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
JavaScript, Sixth Edition
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.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
BLOCK  is enclosed by curly brackets {}.  Keep in mind that blocks are always bounded by curly brackets, even if your block has only one code line.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Learning Javascript From Mr Saem
Methods. The Structure of a Method Essentially, a method is a block of code with a name. You can execute the code by using the method’s name. You can.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Branching Constructs Review
Chapter 8: Control Structures
Flow of Control.
Conditinoal Constructs Review
Flow of Control.
Control statements Simple statements Basic structured statements
Conditinoal Constructs Review
Flow of Control.
Statement-Level Control Structures
Program Flow.
Presentation transcript:

Control Structures

if-elsif-else semantically the same as C/C++ syntactically, slightly different. if ($a > 0){ print “\$a is positive\n”; } elsif ($a == 0){ print “\$a equals 0\n”; } else { print “\$a is negative\n”; } brackets are *required*!

unless another way of writing if (! …) { } analogous to English meaning of “unless” unless (EXPR) BLOCK –“do BLOCK unless EXPR is true” –“do BLOCK if EXPR is false” can use elsif and else with unless as well

while/until loops while is similar to C/C++ while (EXPR) BLOCK –“While EXPR is true, do BLOCK” until (EXPR) BLOCK –“Until EXPR is true, do BLOCK” –“While EXPR is false, do BLOCK” –another way of saying while (! …) { } again, brackets are *required*

for loops Perl has 2 styles of for. First kind is virtually identical to C/C++ for (INIT; TEST; INCRMENT) { } for ($i = 0; $i < 10; $i++){ print “\$i = $i\n”; } yes, the brackets are required.

foreach loops Second kind of for loop in Perl –no equivalent in core C/C++ language foreach VAR (LIST) {} each member of LIST is assigned to VAR, and the loop executed $sum = 0; foreach $value $sum += $value; }

More About for/foreach for and foreach are actually synonyms –they can be used interchangeably. –code *usually* easier to read if conventions followed: for ($i = 0; $i<10; $i++) {} foreach $i {} list argument of foreach can be any list –foreach $i {} –foreach $i (1, 2, 3, 4, 5) {} –foreach $i (1.. 5) {} –foreach $field (split /:/, $data) {}

Two More Thing… (about for) foreach VAR (LIST) {} while iterating through list, VAR becomes an *alias* to each member of LIST –Changes within loop to VAR affect LIST if VAR omitted, $_ = (1, 2, 3, 4, 5) foreach { $_ *= 2; now  (2, 4, 6, 8, 10)

Loop Control – next, last, redo last  equivalent of C/C++ break –exit innermost loop next  (mostly) equivalent of C/C++ continue –begin next iteration of innermost loop redo  no real equivalent in C/C++ –restart the current loop, without evaluating conditional

continue block while, until, and foreach loops can have a continue block. placed after end of loop, executed at end of each iteration executed even if loop broken out of via next (but not if broken via last or redo ) foreach $i if ($i % 2 != 0){ next; } $sum += $i; } continue { $nums++; }

Breaking Out of More Loops next, last, redo operate on innermost loop Labels to break out of nesting loops TOP: while ($i < 10){ MIDDLE: while ($j > 20) { BOTTOM: foreach if ($j % 2 != 0){ next MIDDLE; } if ($i * 3 < 10){ last TOP; }

goto yes, it exists. No, don’t use it.