Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.

Slides:



Advertisements
Similar presentations
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
true (any other value but zero) false (zero) expression Statement 2
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Logical Operators and Conditional statements
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Decision Structures and Boolean Logic
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Flow of Control Part 1: Selection
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
ITM 352 Flow-Control: if and switch. ITM © Port, KazmanFlow-Control - 2 What is "Flow of Control"? Flow of Control is the execution order of instructions.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Week 8: Decisions Bryan Burlingame 21 October 2015.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Conditional Statements © Copyright 2014, Fred McClurg All Rights Reserved.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Control statements Mostafa Abdallah
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Decision Statements, Short- Circuit Evaluation, Errors.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
 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.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Lecture 3- Decision Structures
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in JavaScript
Chapter 4 Control Statements: Part I
Topics The if Statement The if-else Statement Comparing Strings
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Controlling Program Flow
Presentation transcript:

Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1

Chapter Four Decisions, Decisions urses/php/slides/chapter04a.conditions.ppt 2

“What is truth?” -- Pontius Pilate AD 30 PHP Logicals 3

Description: Comparison operators determine the relationship between two operands and return either a TRUE or FALSE Boolean value. Comparison Operators OperatorDescription == Equal To != Not Equal To < Less Than > Greater Than <= Less Than or Equal To >= Greater Than or Equal To 4

Description: Relational operators determine equality between two operands and return either a TRUE or FALSE Boolean value. Relational Operators Example: 0; // FALSE 1; // TRUE -1; // TRUE $a = 0; // FALSE (assignment) $a = 1; // TRUE (assignment) 1 == 1; // TRUE (equality) "1" == 1; // TRUE (equality) "1" === 1; // FALSE (identity) '1' != 'A'; // TRUE (inequality) 1 <> 2; // TRUE (same as "!=") Relational Operators 5

Description: Logical operators work with relational operators to form more complex expressions. Logical Operators OperatorSymbolDescription AND&& True if both operands are true OR|| True if one or more operands are true XOR n/aTrue if only one operand is true n/a ! True if false. False if true. 6

AND TRUEFALSE TRUE FALSE Definition: True if both operands are true. Truth Tables (Logical AND) T F F F 7 TRUE TRUE FALSE FALSE

OR TRUEFALSE TRUE FALSE Definition: True if at least one operand is true. Truth Tables (Logical OR) T F T T 8 TRUE FALSE

XOR TRUEFALSE TRUE FALSE Definition: True if only one operand is true.. Truth Tables (Logical XOR) T F T F 9 TRUE FALSE

! Result TRUE FALSE Definition: Negation. True if false. False if true. Truth Tables (Logical NOT) T F 10 TRUE FALSE !

Description: The curly braces “{}” define the boundaries of a code block that contain zero or more statements. The braces also allow you to group code so that it corresponds with additional statements (e.g. if, else, etc.) Syntax: { // begin code block statement;... } // end code block “Good Grief! You block of code.” 11

Description: The “if” statement evaluates a condition to determine true or false. The “if” Flow Chart FALSE TRUE block of code When a condition is false, the block of code (or statement) skipped. When a condition is true, the block of code (or statement) executed. 12 Begin Execution Continue Execution if condition

Description: Execute a block of code if the condition is true. Syntax: if ( condition ) { statement; } The “if” Conditional Statement 13

Rules: 1. The condition can be any expression (even an assignment). 2. The curly braces “ {} ” are optional if there is only one statement. However, best practices or style guidelines may mandate the use of braces in all circumstances. The “if” Rules 14

Trace the following code: <?php $x = 5; if ( $x == 5 ) // true echo "true! "; if ( $x ) // true echo "true! "; if ( TRUE ) // true echo "true! "; // is number between 2 and 7? if ( ($x > 2) && ($x < 7) ) // true echo "true!"; ?> Example “if” Statements 15

Trace Output: <?php $x = 0; if ( $x == 0 ) // true echo "true!"; if ( $x ) // false echo "true!"; if ( ! $x ) // true echo "true!"; // is number outside range of -1 and 7? if ( ($x 7) ) // false echo "true!"; ?> More “if” Statement Examples 16

Trace Output: <?php $x = NULL; if ( $x ) // false echo "true!"; $str = ""; if ( $str ) // false echo "true!"; if ( TRUE || FALSE ) // true echo "true!"; if ( TRUE && FALSE ) // false echo "true!"; ?> Still More “if” Statement Examples 17

In order to speed up processing, PHP (and other languages) may “short circuit” some logical operations. This means that the second half of a statement may not get executed. Example: FALSE AND TRUE; // false FALSE AND FALSE; // false TRUE OR TRUE; // true TRUE OR FALSE; // true Short Circuit Operations 18

<?php if ( $x = 0 ) // assignment not equality! echo "Never reached "; if ( $x = 4 ) // assignment not equality! echo "Always reached "; // short-circuited "OR" statement if ( TRUE || ++$x ) // not incremented printf( "Always reached: %d ", $x ); // short-circuited "AND" statement if ( FALSE && ++$x ) // not incremented printf( "Never reached: %d ", $x ); printf( "\$x = %d ", $x ); ?> “if” Pitfalls to Avoid 19

Description: Avoid using bitwise operators in an “if” statement. However, when they are used, they function as a non short-circuited operators. Examples: <?php $a = 3; $b = 5; if ( TRUE | ++$a ) // not short-circuit { printf( "Always reached " ); printf( "\$a = %d ", $a ); // 4 } if ( FALSE & --$b ) // not short-circuit { printf( "Never reached " ); } printf( "\$b = %d ", $b ); // 4 ?> Bitwise operators (non short circuit-operators) 20

Logical Operator Examples: TRUE AND TRUE; // true TRUE AND FALSE; // false FALSE AND TRUE; // false FALSE AND FALSE; // false TRUE OR TRUE; // true TRUE OR FALSE; // true FALSE OR TRUE; // true FALSE OR FALSE; // false TRUE XOR TRUE; // false TRUE XOR FALSE; // true FALSE XOR TRUE; // true FALSE XOR FALSE; // false ! TRUE; // false ! FALSE; // true What is truth? (review) 21

When a condition is true, the “if code” (or statement) executed. When a condition is false, the “else code” (or statement) executed. Description: The “else” statement handles the “not if” condition. The “else” Flow Chart 22 if condition FALSETRUE “if” code “else” code else branch if branch Continue Execution Begin Execution

Description: Provides an alternate branch of execution when the “if” condition is false. Syntax: if ( condition ) statement1; else // if ( ! condition ) statement2; The “else” Statement Syntax 23

Example: <?php $isTrue = FALSE; if ( $isTrue ) // true printf("Nothing but the truth!"); else // false or fallback printf("That is a lie!"); ?> An “else” Statement Example 24

Description: Beware of the dangling “else”! An “else” is always associated with the nearest “if” regardless of paragraph indentation. Trace Output: <?php $day = 28; if ( $day <= 29 ) // true if ( $day == 29 ) // false echo "Today's a leap day "; else // note: this is dangling else! echo "$day greater than 29 "; ?> The “else” Pitfall 25

Description: A dangling “else” can be avoided by specifying brackets around the “if” that is associated with the “else”. Problem Resolved: <?php $day = 28; if ( $day <= 29 ) // true { if ( $day == 29 ) // false echo "Today's a leap day "; } else // dangling else fixed echo "$day greater than 29 "; ?> Note: To avoid the potential dangling “else” hazard, Best Practices or Style Guidelines may mandate the use of curly brackets regardless of the number of statements contained in the block of code. Avoiding the “else” Pitfall 26

Description: There are cases when you need to compare against a large number of alternatives. This may produce “marching” nested if/else code. <?php if ( $chr = 'a' ) $rot13 = 'n'; else if ( $chr = 'b' ) $rot13 = 'o'; else if ( $chr = 'c' ) $rot13 = 'p'; else if ( $chr = 'd' ) $rot13 = 'q'; else if ( $chr = 'e' ) $rot13 = 'r'; else if ( $chr = 'f' ) $rot13 = 's'; else... ?> Note: There are more elegant ways to code a ROT13 character lookup. Nested “if” Statements 27

Description: This statement provides a test for multiple conditions. It eliminates the need for a nested “if-else” statement. Syntax: if ( condition1 ) statement1; elseif ( condition2 ) // note: not spelled "elsif" statement2; elseif ( condition3 ) statement3;... else statementx; Note: The “elseif” statement may not have the same result as “else if” when using curly brackets. The “elseif” Syntax 28

Example: <?php $dollar = 0.50; if ( $dollar > ) echo "Category: Upper Class"; elseif ( $dollar > ) echo "Category: Middle Class"; elseif ( $dollar > ) echo "Category: Low Class"; else // fallback echo "Category: No Class"; ?> The “elseif” Example 29

Description: The ternary operator “?” and “:” is an abbreviated syntax for the “if” statement Syntax: condition ? trueExpr : falseExpr; Equivalent “if” Statement: if ( condition ) trueExpression; else falseExpression; Ternary (conditional) Operator 30

<?php $count = 4; $name = "cact"; // make singular/plural by appending $name.= ($count == 1) ? "us" : "i"; // make singular/plural by assignment $verb = ($count == 1) ? "is" : "are"; $plural = ($count != 1) ? "s" : ""; printf( "There %s %d %s plant%s.", $verb, $count, $name, $plural ); ?> Ternary Operator Example 31

Description: The “switch” provides an alternate construct for an if-elseif-else statement. Advantage: Fall-through capability. Disadvantage: Uses only constants. Can not use any conditional other than “==”. Conditionals “ ”, !=, etc. are not allowed. Pitfall: Don't forget the “break” statement! The “switch” Statement 32

<?php switch ( $variable ) { case constant1 : statement1; break; // optional case constant2 : statement2; break; // optional default : // optional statementx; } ?> The “switch” Syntax 33

<?php $year = 1960; $month = 'June'; printf( "%s %d has ", $month, $year ); switch ( $month ) { case 'September' : case 'April' : case 'June' : case 'November' : $len = 30; break; case 'February' : $len = isLeapYear($year) ? 29 : 28; break; default : $len = 31; } printf( "%d days", $len ); ?> A “switch” Example 34

to be continued... ourses/php/slides/chapter04b.looping.ppt