CSCI 116 Decisions & Validation. Overview Constants Boolean conditions Decision logic Form validation 2.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Manipulating Strings. 2 Topics Manipulate strings Manipulate strings Parse strings Parse strings Compare strings Compare strings Handle form submissions.
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
JavaScript, Third Edition
Chapter 4 Making Decisions
CSCI 116 Week 3 Functions & Control Structures. 2 Topics Using functions Using functions Variable scope and autoglobals Variable scope and autoglobals.
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
CIS162AD - C# Decision Statements 04_decisions.ppt.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
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.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Flow of Control Part 1: Selection
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Conditional Statements © Copyright 2014, Fred McClurg All Rights Reserved.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Control statements Mostafa Abdallah
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Random Functions Selection Structure Comparison Operators Logical Operator
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
Chapter 4: Making Decisions.
An Introduction to Programming with C++ Fifth Edition
More Selections BIS1523 – Lecture 9.
Conditions and Ifs BIS1523 – Lecture 8.
The Selection Structure
Presentation transcript:

CSCI 116 Decisions & Validation

Overview Constants Boolean conditions Decision logic Form validation 2

Defining Constants A constant contains information that does not change during program execution Constant names – Do not begin with a dollar sign – Do use all uppercase letters Use the define() function to create a constant define("CONSTANT_NAME", value); Examples – define("PI", ); – define("STATE_ABBR", "WA"); 3

Using Constants define("PI", ); $radius = 5.0; $circumference = 2 * PI * $radius; echo "PI is ". PI. " "; echo "The circumference is $circumference"; 4 Note: Unlike variables, you cannot include a constant name within the quotations that surround a text string.

Boolean Variables A Boolean variable is either true or false $is_registered = TRUE; print " Registered: $is_registered "; Often used in decision making if($is_registered)… 5 TRUE prints as 1 FALSE prints as 0

Boolean Conditions Boolean conditions evaluate to true or false Use relational and logical operators Used in decision logic 6 if($isSunny AND $dayOfWeek >= 6) { print "Off to the beach!"; }

Relational Operators 7

2 > 3 3 <= 2 2 == * 3 < != 3 3 == "3" 3 === "3" >= 2-- Evaluate the following boolean conditions:

Logical Operators Logical operators are used for comparing two boolean expressions Return TRUE or FALSE 9

Operator Precedence Pre-increment/decrement Arithmetic operators (*, /, %, +, -) Relational operators (<, <=, ==, !=) Logical operators (AND, OR) – AND has precedence over OR Assignment operators (=, +=, etc.) Post-increment/decrement 10

Logical Operators >= 3 AND 3 > * 2 > 6 OR 2 != 2 OR 5 < != 1 AND 2 * 3 >= == OR 3 % 2 <= Evaluate the following boolean conditions:

Practice Evaluate the following boolean conditions given that $x=1, $y=2, and $z=3 $x <= $y $x == $y OR $z > $y $y != $z AND $y >= $x $y + $x == $z $x - $y * $z < $z / $y $z % $y != $x && $y >= $z || $x <= $z $x $x * $y 12

if Statements Used to execute one or more statements if a boolean condition is true if(boolean condition) { statements; } If the boolean condition is false, the statements do not execute 13

if Statements $age = 21; $day = “Monday”; if($age == 21) { print “Let’s party!”; print “It’s dancing time”; } print “Smile”; 14

if Statements $age = 21; $day = “Monday”; if($age == 21 && $day == “Friday” || $day == “Saturday”) { print “Let’s party!”; } print “Smile”; 15

if Statements $age = 21; $day = “Monday”; if($age == 21 && ($day == “Friday” || $day == “Saturday”)) { print “Let’s party!”; } print “Smile”; 16

if...else Statements An else clause executes when the condition in an if...else statement evaluates to false Syntax: if (conditional expression) { statement(s); } else { statement(s); } 17

if...else Statements $day = “Monday”; if($day == “Saturday”) { print “Let’s party!”; } else { print “Good night!”; } 18 Curly braces are not required when there is only one statement after the if or else, however, it’s still a good idea to use them.

Nested if Statements One decision-making statement may be contained within another 19 if($day == “Friday” || $day == “Saturday”) { if($age >= 21) { print “Let’s party!”; } Notetheindentation

switch Statements Executes a specific set of statements depending on the value of an expression Compares the value of an expression to a value in a case label – case label can be followed by one or more statements – Each set of statements is usually followed by the keyword break – The default label contains statements that execute when the value of the expression does not match any other case label 20

switch Statements Syntax switch (expression) { case label: statement(s); break; case label: statement(s); break;... default: statement(s); } Example switch ($day) { case 1: print “Monday”; break; case 2: print “Tuesday”; break;... default: print “Invalid”; } 21

Decision Practice Write a statement that prints “even” if a variable num is even. (Hint: Use the modulus operator.) Write a statement that prints “even” if a variable num is even, and “odd” otherwise. Write a statement that prints “A” for a grade of , “B” for 80-89, “C” for 70-79, “D” for and “F” otherwise. Write a switch statement that takes a number and prints the corresponding month, e.g. “January” for 1. 22

Comparing Strings $cartoon1 = "Minnie Mouse"; $cartoon2 = "Mickey Mouse"; if ($cartoon1 == $cartoon2) echo " Same cartoon. "; else echo " Different cartoons. "; 23

Comparing Strings (continued) $cartoon1 = "Pluto"; $cartoon2 = "pluto"; if ($cartoon1 < $cartoon2) echo "$cartoon1 comes before $cartoon2"; else echo "$cartoon2 comes before $cartoon1"; 24

ASCII Numeric representations of English characters ASCII values range from 0 to 256 Lowercase letters are represented by the values 97 (“a”) to 122 (“z”) Uppercase letters are represented by the values 65 (“A”) to 90 (“Z”) Lowercase letters have higher values than uppercase letters, so they are evaluated as being “greater” than uppercase letters 25

String Comparison Functions The strcasecmp() function performs a case-insensitive comparison of strings – strcasecmp(“DAISY", “daisy")  true The strcmp() function performs a case-sensitive comparison of strings – strcmp(“DAISY", “daisy")  false 26

Conditional Operator The conditional operator executes one of two expressions, based on the results of a conditional expression Syntax: conditional expr ? expr1 : expr2; 27 evaluates if conditional expression is true evaluates if conditional expression is false

Conditional Operator $grade = 2.5; ($grade >= 2.0) ? $result = "You pass!" : $result = "You fail."; echo $result; 28

Handling Form Submissions Form data is submitted in name=value pairs GET method passes form data by appending a question mark (?) and name=value pairs to the URL 29

Handling Form Submissions A user can bypass JavaScript form validation on the client – If get is used, they can type in a URL – If post is used, they can construct a transmission using HTTP headers Always use PHP code to validate submitted data 30

Validating Submitted Data Use isset() or empty() functions to ensure that a variable contains a value – if (isset($_GET[‘amount’])) – if (!empty($_GET[‘amount’])) Use is_numeric() to test whether a variable contains a numeric string – if (is_numeric($_GET[‘amount’])) 31

isset() and empty() isset($var)empty($var) $var = 0;true $var = ‘hello’;truefalse $var = null;falsetrue $var = ‘’;true falsetrue 32 used to validate text fields; do not use if 0 (zero) is valid used to validate text fields; do not use if 0 (zero) is valid used to validate check boxes, radio buttons, and select lists used to validate check boxes, radio buttons, and select lists

Data Validation if(is_numeric($_POST[‘year’]) AND strlen($_POST[‘year’] == 4) AND $_POST[‘year’] >= 2010) { $year = $_POST[‘year’] ; } else { $valid = false; print “ Invalid year. ”; } 33

34 Enter your credit card number: <?php if(isset($_POST["ccnumber"])) { $num = $_POST["ccnumber"]; $num = str_replace("-", "", $num); $num = str_replace(" ", "", $num); if(is_numeric($num)) echo "Your credit card number is $num."; else echo "Your credit card number is not valid."; } ?> create the form check to see if a value was entered strip out dashes and spaces check to see if what’s left is numeric