More Selections BIS1523 – Lecture 9.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
JavaScript Form Validation
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
CIS162AD - C# Decision Statements 04_decisions.ppt.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
Using Client-Side Scripts to Enhance Web Applications 1.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
JavaScript, Fourth Edition
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Controlling Program Flow with Decision Structures.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
 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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Loops BIS1523 – Lecture 10.
Arrays: Checkboxes and Textareas
Arrays and files BIS1523 – Lecture 15.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Web Programming– UFCFB Lecture 17
Intro to PHP & Variables
MATLAB: Structures and File I/O
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Cookies BIS1523 – Lecture 23.
HTML Forms and User Input
Control Statement Examples
Functions BIS1523 – Lecture 17.
Conditions and Ifs BIS1523 – Lecture 8.
Number and String Operations
Selection CSCE 121 J. Michael Moore.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 3: Introduction to Problem Solving and Control Statements
In Class Programming BIS1523 – Lecture 11.
The switch statement: an alternative to writing a lot of conditionals
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Simple Branches and Loops
Selection Statements Chapter 3.
More on If statements (Calculate, Calculate1, Calculate2)
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

More Selections BIS1523 – Lecture 9

Logical Operators Sometimes we want to compare more than just 2 things together, or build more complex conditions based on many things. To combine relational expressions into more complex expressions, we use Logical Operators. Relational expressions can be combined with the AND or OR operators. Each returns true in a slightly different manner: (expr1) AND (expr2) Returns true only if both expr1 and expr2 are TRUE (expr1) OR (expr2) Returns true if either expr1 or expr2 are TRUE AND can also be written as && OR can also be written as ||

Examples For the following values of length and width, is the above expression true or false? $length = 10 $width = -10 TRUE $length = -10 $width = 0 FALSE

Examples $length = 10 $width = 10 FALSE $length = 15 $width = 5 $length = 20 $width = 15 TRUE If you include math the result of the math is used in the comparison

Examples Notice in the above examples we always use parenthesis when using logical operators, to ensure the relational operators are evaluated first In a series of if statements, it is possible for more than one to be executed. If length is -10, and width is 50, the first two lines will both be printed

Example In the previous lecture, we used a series of if statements to do data validation. You could combine these into a single statement using OR’s, though the error messages would be less precise. Could be written as:

Nesting IF statements The code within the {}’s of an if statement can be any number of lines long, it can also include additional “IF” statements. In the example to the right, the red line group is entirely within the “if” part of the first if statement. So they will only be executed if condition1 is TRUE. Group2 will only be executed if condition2 is TRUE, but it is also part of the “red line” group, so condition1 will also have to be TRUE. Group3 is in the else part of the first condition, so it is executed only if condition1 is FALSE.

Nested If Example Take the following example, where we want to assign a discount based on some parameters: Customer code can be “R” or C” Discount is calculated by: “R” class customers get a 10% discount if they spend more than 100$. “C” class customers get a 15% discount normally, or a 20% discount if they spend more than 200$. We could nest if statements to first see if a customer was an R or C, and then check the purchase amount to assign a discount

Discount Example

Alternative Form As with most if statements, this problem can be solved several different ways. The two examples below are equivalent: The one on the left is slightly more efficient.

Continued Example In this example, what happens if the customer code is something other than an R or C? We never checked to see if it was a C, so we could add a second check there.

Continued Example The logic in this example is: If its an R, check range Else if it is a C, check its range Else, its an error. This construct allows us to pick one of a series of options (R, or C, or something else). Using nested if/elses accomplishes this.

Elseif You can combine the Construct into a single “elseif” as shown to the right. It is functionally the same, but requires you to use one less set of {}

Example: No nesting, using Elseif One final way to code this example would be to use only elseif’s. It is more efficient than a series of Ifs, and not quite as complex to write as nesting as there are less {}’s to deal with: Using elseif’s allows only one of the possible discounts to be assigned.

switch Another selection construct that allows us to execute one selection of code out of several, based on a condition, is the switch The switch statement compares a variable (or expression) to a list of things, when it finds one it matches, it executes a section of code. The “default” value is a “none of the above” conditions. Switch does allow the use of strings in its comparison. The ‘break’ command should be used to end each section of code

Switch example Below the string variable $grade is compared to each value, ‘A’, then ‘B’, then ‘C’, and an appropriate section of code executed. Only one of the sections of code is executed, so this is functionally similar to a series of if/elseif’s.

Checkboxes Checkboxes are a user input method that allow the user to select any number of options from a list of things. Each checkbox should have a different name. The name is used in PHP to determine if the checkbox was checked using isset

checkboxes Since any number of checkboxes can be checked, we normally want to have an if statement for each one. For example, if we wanted to count the number of languages, we could do: Since more than one thing can be checked, switch, or elseif’s are not appropriate for checkboxes.

Checked property You can include the checked property in your HTML if you want a checkbox to be selected at the start.

Checkboxes vs Radio Buttons Allow any number of selections Only one can be selected per group Each has a unique name One name for each ‘group’ of radio buttons Value property not used (yet, we will use it later in the course) Value property is used to determine which one is selected Use isset($_POST[‘name’]) to test One IF for each checkbox If isset($_POST[‘name’]) $result = $_POST[‘name’] One IF statement for each possible value If ($result == value)

Multiple PHP Tags Can you have multiple sections of code in your HTML. In this case the code is executed from top to bottom. This is useful in a few cases. Since PHP outputting HTML is slower than just plain HTML, if you have a lot of HTML it is better to not have it enclosed in php print commands It can also be useful for organization (covered later).

Single Page Programs We can use if statements to compress our pages into a single PHP enabled page. If you go to one of our “results” pages without first going to the HTML page, the variables will be empty. It would be like the user hit “submit” without typing anything in. The “submit” variable will also be empty. This value is passed when we click the “submit” button of the form. To create a single-page program, we have the page check to see if it was submitted, if not, print out the “INPUT” html, if it was submitted, do the “output” stuff.

Example Outline Example is payment_simple.php on K: drive in the lecture 9 folder

Payment Example To test to see if the program was submitted, we can use this if: If it is not set, we want to print out the entire HTML for the input form. Since there are no variables, and no single quotes in it, we can cut & paste, and enclose it in single quotes…. Escaping single quotes might be necessary if your HTML included any.

Payment Example Then we add an ‘else’ to that, that needs to print out ALL the html for the result screen, as well as the PHP