Conditions and Ifs BIS1523 – Lecture 8.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

LIS651 lecture 3 numbers, Boolean, control flow Thomas Krichel
Objectives In this chapter, you will learn about:
Conditional Statements Introduction to Computing Science and Programming I.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
JavaScript Form Validation
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
30/10/ Iteration Loops Do While (condition is true) … Loop.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
22/11/ Selection If selection construct.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
Summer SAS Workshop Lecture 3. Summer SAS Workshop Website
Unit 10 – JavaScript Validation Instructor: Brent Presley.
31/01/ Selection If selection construct.
Controlling Program Flow with Decision Structures.
Logical Operators.  Quiz  Let's look at the schedule  Logical Operators 2.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Computer Science Up Down Controls, Decisions and Random Numbers.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
JavaScript: Conditionals contd.
The Ohio State University
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Loops BIS1523 – Lecture 10.
Introduction To Repetition The for loop
Arrays: Checkboxes and Textareas
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Debugging and Random Numbers
IF statements.
Week 4 - Monday CS 121.
CHAPTER FIVE Decision Structures.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Lecture 07 More Repetition Richard Gesick.
Web Programming– UFCFB Lecture 17
Intro to PHP & Variables
Stack Data Structure, Reverse Polish Notation, Homework 7
Lecture 4B More Repetition Richard Gesick
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Cookies BIS1523 – Lecture 23.
CHAPTER FIVE Decision Structures.
And the text with form..
More Selections BIS1523 – Lecture 9.
Functions BIS1523 – Lecture 17.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Number and String Operations
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
In Class Programming BIS1523 – Lecture 11.
If selection construct
Do While (condition is true) … Loop
Introduction to TouchDevelop
Java Programming Control Structures Part 1
If selection construct
Do … Loop Until (condition is true)
Selection Statements.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Selection Statements Chapter 3.
Unit 3: Variables in Java
More on If statements (Calculate, Calculate1, Calculate2)
Presentation transcript:

Conditions and Ifs BIS1523 – Lecture 8

The if statement The if statement let’s us conditionally execute code. The condition above is something that evaluates to either TRUE or FALSE. There can be any number of commands inside the {} If the condition is true, the statements within {} are executed. If the condition is false, the statements are skipped, and the program continues immediately following the closing }

Boolean Variables An additional type of variable is a “Boolean” variable. Instead of being a number, or a string, it has the value of TRUE or FALSE. To set the value of a boolean variable, just use an assignment statement. Notice the word TRUE does not have double quotes around it (if it did, it would be considered a string.) Boolean variables can be used as a condition in an if. So if the value of the variable $okay was TRUE, it would print out the message.

The empty function There are several boolean functions available for use in PHP. One such function is the empty function. the empty() function, which checks to see if a given variable has an “empty” value. A variable is considered to have an empty value if the variable has no value, has a value of 0, or has a value of FALSE. In any of these cases, the function returns TRUE; otherwise, it returns FALSE The isset() function is nearly the opposite of empty. It returns TRUE if a variable has any value (including 0, FALSE, or an empty string)

is_numeric function Another boolean function is is_numeric(). It tests a variable to see if it has a numeric value, if so it returns TRUE. A variable could incorrectly have a non-numeric value if we allowed the user to enter it. For example, the user might do something like:

Reversing a Condition The previous example was good at telling if a variable was a number, but what if we want to do something if it is NOT a number? The ! Operator reverses a condition (read aloud as the word “not”). So the condition above is the logical opposite of the previous example. If the variable $balance is not numeric, an error will be printed out. We can use the empty, is_numeric, and boolean variables to do data validation of our forms

Data Validation Whenever we ask the user to enter in information, we want to make sure as best we can that they entered in correct, or valid values. Common mistakes include leaving inputs blank, or typing in incorrect values (where we want numbers) We can’t check every possible case (who could tell if the word Frothi was a valid name or not?), but we can test to make sure at least something was entered, and we can test to see that numbers were actually numbers. We will accomplish this with a series of if statements to test our inputs, and report an error if any of them were incorrect.

Which function to use? As a general rule: We will use is_numeric to test numeric values We will use the isset() function to validate non-text form elements like checkboxes, radio buttons, and select menus We will use the empty() function to validate text fields

Example Program As an example, lets look at the payment calculator program we did in Lecture 7. We have 3 different inputs, balance, charges, and credits. We want to make sure the user entered in values for all three of these.

Checking numeric We use a separate if statement for each input we want to test, printing out an appropriate error message for incorrect data

Preventing Calculations on Error If the user then leaves data blank, we get one (or more) error messages stating what went wrong Notice, however, that we also get a printout for new balance and minimum payment. These are incorrect, since we got incorrect data to start with. To prevent these from printing out, we will use a FLAG variable

Flag Variables A flag variable, in its simplest form, is a variable you define to have one value until some condition is true, in which case you change the variable's value. For data validation, we will use it to tell if any of the data entered was invalid. The process is: Set the variable equal to TRUE before testing your data If it fails any of the tests, set it to FALSE After you have tested all your data, if the variable is still TRUE, then you know the data passed all the tests.

Flag Variable Example After we have checked every input, we can check the flag. We only want to do our calculations and output if the flag is TRUE (everything is OKAY). Remember, if statements can contain any number of lines, so we can put ALL our calculations and output within that last if

Flag variable example So, in the example below, we won’t do any calculations or output if the data was not OK.

Relational Expressions In addition to functions, and boolean variables, a condition can also be a relational expression. Relational expressions compare 2 numbers, and return TRUE or FALSE The expressions are made up of one of the following relational operators: Relational expressions will have a number on each side of an operator.

Relational Expressions Relational expressions do numeric comparisons. So in the above example, if the value of the variable $new_balance was numerically less than the value 0, the condition would return true. Using parenthesis, relational expressions can be negated with the ! Operator, but since each relational operator has a logical opposite, that is usually not necessary. The condition below is logically exactly the same as the one at the top of this slide

Opposite Relations The following shows each relational operators opposite: Be sure and don’t confuse the assignment operator (=) with the equality operator (==). They are not interchangeable. String comparisons are done with functions, so only use these relational operators for numeric expressions. Operator Counterpart == != > <= < >=

The else clause Often times we want to do one section of code if a condition is true, and another if a condition is false. Rather than write 2 if statements in a row, you can use an else clause. Only one of the two groups of statements can be executed. If the condition is TRUE it will be the first group, if the condition is false it will be the second group. One of the 2 groups of statements will be executed. It might be a different group each time you run the program, but one of them has to be executed.

Else example In our previous data validation example, we printed out the calculations if the data was okay. We could add an ‘else’ condition for further error reporting.

Another Else Example In the previous credit card example, the minimum payment was set to 2% of the new balance. However, most credit cards won’t have a minimum payment of only a few cents. So, we could check to see if the balance was less than $20, and if so, the minimum payment is equal to the entire balance.