Faculty of Sciences and Social Sciences HOPE PHP Flow Control Website Development Stewart Blakeway FML 213 0151 291 3113.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

ALGORITHMS - PART 2 CONDITIONAL BRANCH CONTROL STRUCTURE
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 5: Steps in Problem Solving Stewart Blakeway FML 213
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 8: Java: Selection and Repetition Stewart Blakeway.
Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML
Faculty of Sciences and Social Sciences HOPE PHP – Working with Input Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213
ITC 240: Web Application Programming
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Faculty of Sciences and Social Sciences HOPE JavaScript Validation Regular Expression Stewart Blakeway FML
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Faculty of Sciences and Social Sciences HOPE JavaScript Advanced Stewart Blakeway FML
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.
CPS120 Introduction to Computer Science Iteration (Looping)
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Controlling Execution Dong Shao, Nanjing Unviersity.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
PHP Constructs Advance Database Management Systems Lab no.3.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
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.
CS1371 Introduction to Computing for Engineers
JavaScript: Control Statements.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
ITM 352 Flow-Control: Loops
Conditions and Ifs BIS1523 – Lecture 8.
During the last lecture we had a discussion on Data Types, Variables & Operators
Coding Concepts (Basics)
BIT116: Scripting Lecture 6 Part 1
CSC215 Lecture Control Flow.
Controlling Program Flow
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Faculty of Sciences and Social Sciences HOPE PHP Flow Control Website Development Stewart Blakeway FML

Faculty of Sciences and Social Sciences HOPE Aims How to use an IF statement How to execute alternative code if the IF condition is not true Nested IF The SWITCH statement The WHILE statement The FOR statement Nesting Loops

Faculty of Sciences and Social Sciences HOPE PHP Statements PHP statements follow the conventions of standard programming languages Each line is executed one by one. Starting from the top and working down Sometimes you may want to repeat a section of code or ignore a section completely

Faculty of Sciences and Social Sciences HOPE Switching Flow Sometimes depending on a result you will want to switch from executing (doing) one thing to another. We have seen this in JAVASCRIPT. If validation of the form fails, return false …. Otherwise return true

Faculty of Sciences and Social Sciences HOPE if We control the flow of execution (events) by checking ‘ something ’, if something equals the correct thing, we do this. If however, it does not equal what we expect we can do an alternative thing. This is called a conditional statement

Faculty of Sciences and Social Sciences HOPE if We use if everyday! When you get a bus: If you have the correct fare, pay, take bus. If you don ’ t have the correct fare, give what you have. If you have given less than the fare price … get off the bus! If you have given more than the fare price, then you are given change and you take the bus. IF is usually broken down. For example:

Faculty of Sciences and Social Sciences HOPE PHP if if ($money == $fare) { echo (“Fare ok”); echo (“sit down and enjoy the ride”); }

Faculty of Sciences and Social Sciences HOPE PHP if else else { echo (“Incorrect fare”); } You can not use an ELSE without an IF …. Otherwise how will the parser know what else you are talking about!

Faculty of Sciences and Social Sciences HOPE if else if ($money == $fare) { echo (“Fare ok”); echo (“sit down and enjoy the ride”); } else { echo (“Incorrect fare”); }

Faculty of Sciences and Social Sciences HOPE Not good enough! Did you give too much or too little money? Work in pairs to write the full PHP solution to resolve the initial question and the above question. Obviously if you gave too much money you should ride the bus. Otherwise you should get back what pittance you gave and get off the bus!

Faculty of Sciences and Social Sciences HOPE Solution if ($money == $fare) { echo (“Fare ok”); echo (“sit down and enjoy the ride”); } else { echo (“Incorrect fare”); if ($money > $fare) { $returnmoney = $money-$fare; echo (“Your change sir, enjoy the ride”); } else { $returnmoney = $money; echo (“get off my bus!”); } This is only one solution. There could be alternative solutions that are equally correct

Faculty of Sciences and Social Sciences HOPE IF and Multiple Expressions So far we have seen examples of IF testing one condition. What if you wanted to check two?

Faculty of Sciences and Social Sciences HOPE Multiple Expressions if (($money == $fare) && ($destination == True)) { echo (“Fare ok”); echo (“sit down and enjoy the ride”); } else { echo (“Incorrect fare”); } Question Although this will pretty much work. There is an obvious flaw. Can you spot it?

Faculty of Sciences and Social Sciences HOPE Problem! You work in a car park, the charge system is fairly simple. If the person leaving has been parked on the car park for less than an hour you charge a flat rate of £ If the person has been on the car park for over an hour you charge £ This is on the assumption that the person is not a regular (member). Members get a discount of 25%

Faculty of Sciences and Social Sciences HOPE Variables In my solution 3 variables have been identified – assume $current_time holds the current time – assume $timeonticket holds the time of arrival – assume $person[‘member’] holds the status of if the person is a member Next put the variables to use and write the PHP to solve the problem

Faculty of Sciences and Social Sciences HOPE else { if ($current_time < ($timeonticket + 1hour)) { echo (“£1.00 please sir”); } else { echo (“£2.00 please sir”); } Solution if ($person[‘member’] == “yes”) { if ($current_time < ($timeonticket + 1hour)) { echo (“75p please sir”); } else { echo (“£1.50 please sir”); } }

Faculty of Sciences and Social Sciences HOPE Alternatively if ($current_time < ($timeonticket + 1hour)) { if ($person == $member) { echo (“75p please sir”); } else { echo (“£1.00 please sir”); } else { if ($person == $member) { echo (“£1.50 please sir”); } else { echo (“£2.00 please sir”); }

Faculty of Sciences and Social Sciences HOPE Testing for NULL if (!empty($yourVariable)) { // do something! }

Faculty of Sciences and Social Sciences HOPE Summary of if More often than not if the statement is not true you will want to do something else. IF ELSE Sometimes, a simple IF will suffice, for example: – IF tea too strong, add more milk! Otherwise do nothing! if ($tea_strength > $desired_strength) { $tea=$tea + $milk; }

Faculty of Sciences and Social Sciences HOPE Question What would you do if you wanted to check more than a couple of responses from a single IF statement? For example: – If the person is under 5, let them in free. – If the person is between 6 – 16 charge child fare. – If the person is 16 – 60 charge adult fare. – If the person is 61+ charge OAP fare.

Faculty of Sciences and Social Sciences HOPE switch Switch is an ideal solution for more than true or false responses from a single question. It uses the case statement. – Case can be though of as: in this case do this.

Faculty of Sciences and Social Sciences HOPE SWITCH Syntax switch ($age) { case ($age<5): echo (“Go in free child”); break; case ($age>5 && $age<16): echo (“Please pay child fare”); break; case ($age>16 && $age<60): echo (“Please pay adult fare”); break; case ($age>60): echo (“Please pay OAP fare”); break; } Question Although this will pretty much work. There is an obvious flaw. Can you spot it? you can also set a default case. If none of the conditions are met the default will be executed default (condition):

Faculty of Sciences and Social Sciences HOPE What next? Program control can also be manipulated with the use of loops. There are Two basic loops. WHILE and FOR WHILE loops are usually used when the predetermined number of iterations is not known FOR loops are usually used when the number of iterations is known

Faculty of Sciences and Social Sciences HOPE while The basic structure of a while loop is similar to an IF while (condition) { // code goes here }

Faculty of Sciences and Social Sciences HOPE while $option = 9; while ($option != 0) { echo (“My simple menu”); echo (“1. Launch Excel ”); echo (“2. Launch Word ”); echo (“0. Exit Menu”); }

Faculty of Sciences and Social Sciences HOPE do…while do { echo (“1. Launch Excel ”); echo (“2. Launch Word ”); echo (“0. Exit Menu”); } while ($option != 0);

Faculty of Sciences and Social Sciences HOPE Multiple Conditions What if you want to check more than one expression? Remember the operators? – || – && – xor xor – true if either value evaluates true, but not both

Faculty of Sciences and Social Sciences HOPE Multiple Conditions $age = 19; $curtime = time(); $exptime = ($curtime + (60 * 60)); while (($age => 18) && ($exptime>$curtime)) { $curtime = time(); } What conditions must be met in order for the loop to execute? When will the loop stop executing? This loop will execute for 1 hour! Only if your age is 18 or higher

Faculty of Sciences and Social Sciences HOPE Multiple Conditions $age = 19; $curtime = time(); $exptime = ($curtime + (60 * 60)); do { echo (“This loop will execute at least once”); echo (“regardless of the age and the time”); $curtime = time(); } while (($age => 18) && ($exptime>$curtime));

Faculty of Sciences and Social Sciences HOPE for The FOR loop happens a predetermined amount of times. For loops can be nested

Faculty of Sciences and Social Sciences HOPE Simple for for ($counter = 1 ; $counter < 11 ; $counter ++) { echo (“Loop ”.$counter); }

Faculty of Sciences and Social Sciences HOPE foreach $myArray[] = “jim”; $myArray[] = “bo”; echo (“ ”); foreach ($myArray as $temp) { echo (" ".$temp." “); } echo (“ ”); For your assessment you will need create arrays and step through them in order to prepare the data for storage in the database

Faculty of Sciences and Social Sciences HOPE Nesting Loops, regardless of which loop you are using can be put inside a loop. This is called a nested loop.

Faculty of Sciences and Social Sciences HOPE Nested FOR for ($counter = 1 ; $counter < 11 ; $counter ++) { echo (“Loop ”.$counter. “ ”); for ($incounter = 1 ; $incounter<6 ; $incounter ++) { echo (“Inside Loop ”.$incounter. “ ” ); } Loop 1 Inside Loop 1 Inside Loop 2 Inside Loop 3 Inside Loop 4 Inside Loop 5 Loop 2 Inside Loop 1 Inside Loop 2 Inside Loop 3 Inside Loop 4 Inside Loop 5 Loop 3 Inside Loop 1 Inside Loop 2 Inside Loop 3 Inside Loop 4 Inside Loop 5 Loop 4 Inside Loop 1 Inside Loop 2 Inside Loop 3 Inside Loop 4 Inside Loop 5 and soforth

Faculty of Sciences and Social Sciences HOPE WARNING! Be very careful when using loops Especially when nesting loops!

Faculty of Sciences and Social Sciences HOPE What happens here? for ($counter = 1;$countre < 11;$counter ++) { echo (“Loop ”.$counter); }

Faculty of Sciences and Social Sciences HOPE What happens here? for ($counter=1;$counter<11;$counter++) { for ($counter=1;$counter <6 ; $counter ++) { echo (“Inside Loop ”.$counter. “ ” ); }

Faculty of Sciences and Social Sciences HOPE What have we covered? How to use an IF statement How to execute alternative code if the IF is not true Nesting IFs The SWITCH statement The WHILE statement The FOR statement Nesting Loops

Faculty of Sciences and Social Sciences HOPE Debugging Being able to spot syntax mistakes is vital for the exam. Over the next couple of slides see if you can spot the mistakes (3 per slide).

Faculty of Sciences and Social Sciences HOPE PHP IF if ($money = $fare) { echo (“Fare ok”) echo (“sit down and enjoy the ride”) }

Faculty of Sciences and Social Sciences HOPE if else if ($money = $fare) { echo (“Fare ok”); echo (“sit down and enjoy the ride”) } else { echo (Incorrect fare); }

Faculty of Sciences and Social Sciences HOPE WHILE $option = 9; while (option != 0) echo (“My simple menu”); echo (“1. Launch Excel ”); echo (“2. Launch Word ”); echo (“0. Exit Menu”) }

Faculty of Sciences and Social Sciences HOPE Testing for NULL if (!empty(yourVariable) { // do something! } otherwise { // do something else! }

Faculty of Sciences and Social Sciences HOPE Simple FOR for ($counter = 1, $counter < 11, $counter++) { echo (“Loop ”.$counter);

Faculty of Sciences and Social Sciences HOPE foreach $myArray[] = “jim”; $myArray[] = bo; for each ($myArray = $temp) { echo (" ".$temp." “); }

Faculty of Sciences and Social Sciences HOPE Any Questions? Make sure you keep up with the seminar exercises. They are designed to prepare you for your assessment You should complete the full set each week