Control Structures: if Conditional

Slides:



Advertisements
Similar presentations
IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
Advertisements

● Perl reference
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Perl Basics Software Tools. Slide 2 Control Flow l Perl has several control flow statements: n if n while n for n unless n until n do while n do until.
Flow of Control MINS298c Fall 1998 Chapter 9. Overview ABAP Programming Structures for: –Iteration –Decisions Control Flow –If … Then –Do & While loops.
Perl Basics Learning Objectives: 1. To understand the commands available for control flow in Perl 2. To learn some useful operators in Perl.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Lab 8 Shell Script Reference:
2440: 211 Interactive Web Programming Expressions & Operators.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 5 Server Side Scripting Perl.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Perl Practical(?)‏ Extraction and Report Language.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
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
CS 105 Perl: Data Types Nathan Clement 15 May 2014.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CS 105 Perl: Basic I/O, Context, Strings, Lists Nathan Clement January 22, 2014.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02CX - Perl Programming Language Design and Implementation.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Visual Basic 6 (VB6) Data Types, And Operators
CSC 352– Unix Programming, Fall 2012
Perl Programming Language Design and Implementation (4th Edition)
Chapter 3 Branching Statements
JavaScript: Control Statements.
Perl Variables: Array Web Programming.
Outline Altering flow of control Boolean expressions
Chapter 8 JavaScript: Control Statements, Part 2
CS320 Web and Internet Programming Expression Language (EL)
An Introduction to Perl
Control Structures: for & while Loops
Context.
PHP.
Basics.
PowerShell Flow of Control Copyright © 2016 – Curt Hill.
Presented by, Mr. Satish Pise
CSC 352– Unix Programming, Fall, 2011
The Selection Structure
Perl Control Flow Learning Objectives:
DATA TYPES AND OPERATIONS
INTRODUCTION to PERL PART 1.
REPETITION Why Repetition?
Chapter 8 JavaScript: Control Statements, Part 2
LOOP Basics.
Karan Thaker CS 265 Section 001
Control Structures.
Presentation transcript:

Control Structures: if Conditional Web Programming

Review Double quotes vs. Single-quotes Variables inside double quotes are replaced by their values. e.g. “Hello, $name” == Hello, Jim (when $name == “Jim”) ‘Hello, $name’ == Hello, $name Escape character (i.e. \) has special meaning inside double quotes e.g. “Hello, \Ujim” == Hello, JIM ‘Hello, \Ujim’ == Hello, \Ujim Interchangeability of Strings and Numeric Values String can be used as numbers when appropriate, and vice versa e.g. $a= “25”; $sum= $a+5; e.g. $number = <STDIN>; chop $number; $sum= $number+5; String used in integer context inappropriately will produce unexpected results. $b = “five” + 5; ($b == 5) $b = “15k50” + 5; ($b == 20) $b = “k1550” + 5; ($b == 5) Initial value of Scalar Variable Perl scalar variables have an initial value of the null string (“”) Null string is converted to 0 in integer context Web Programming

Control Structures: Overview What are Control Structures? Program statements that control the program flow i.e. controls which statement to execute next Perl Control Structures if conditional Execution of statements depend on current condition of the expression if (expression) { statements } e.g. if ($count<5) { print “count is $count\n”; } while loop Statements are executed while expression is true Syntax: while (expression) { statements } e.g. while ($i<5) { print “count is $i\n”; $i++; } for loop Statements repeats for a given number of times Syntax: for (expression) { statements } e.g. for ($i=0; $i<5; $i++) { print “count is $i\n”; } Web Programming

Conditional Expressions Conditional Expression is a key component of Control Structures The “value” of the conditional expression determines the program flow Syntax: KEYWORD (expression) { statements } e.g. if ($count<5) { print “count is $count\n”; } Conditional Expressions can be any Perl expressions Constant or Variable e.g. (1), (0), (“true”), ($a) Using Comparison Operators ==, <, >, eq, lt, gt, etc. test for single condition, e.g. ($a > $b) Using Logical Operators &&, ||, !, etc. test for multiple conditions, e.g. ($a<0 || $a>9) Evaluation of Conditional Expression True if the value of the expression is non-zero. e.g. (1), (“false”), (“00”), (“ ”) False if the value of the expression is zero (or null). e.g. (0), (“”), (“0”) Web Programming

Comparison Operators Comparison Operators (CO) are used in Conditional Expressions, which are used in Control Structures. Control Structure = KEWORD (conditional expression) { statements } Conditional Expression = (value1 CO value2) Numeric Comparison Operators ==, !=, >, >=, <, <= String Comparison Operators eq, ne, gt, ge, lt, le Strings are compared from left to right in the alphabetical order “aa” is less than “ab” Alphabetical order  null < blank < !”#$%&’()*+,-./ < numbers < :;<=>?@ < A-Z < [\]^-`< a-z < {|}~ Web Programming

Comparison Operators: Pitfalls String vs. Integer comparison When comparing numbers, comparison operator drives the evaluation of an expression numeric comparison for numeric comparison operator TRUE: (“123” > “45”), (123 > 45) alphabetical comparison for string comparison operator FALSE: (“123” gt “45”), (123 gt 45)  Example script Web Programming

Logical Operators Logical Operators, also used in Conditional Expressions, can test for multiple conditions e.g. ( ($a<0) || ($a>9) ) Logical AND && (and) (A && B) : TRUE when both A and B are true Logical OR || (or) (A || B) : TRUE when either A or B are true Logical NOT ! (not) (!A) : TRUE when A is false *Note: FALSE == 0 or null, TRUE == NOT(0 or null)  Example script Web Programming

if Conditional Syntax Single-Line Conditional Statements if (condexpr1) { statements1 } elsif (condexpr2) { statements2 } else { statements3 } statements are executed if condexpr is true Single-Line Conditional Statements When statement block consists of only one statement, Syntax: statement KEYWORD (expression) e.g. print “You cannot buy beer\n” if ($age<21);  Example script Web Programming