ITM 352 Flow-Control: if and switch

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Branching Loops exit(n) method Boolean data type and expressions
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
true (any other value but zero) false (zero) expression Statement 2
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
CIS162AD - C# Decision Statements 04_decisions.ppt.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
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.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Lecture 4 – PHP Flow Control SFDV3011 – Advanced Web Development 1.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
Flow of Control Chapter 3 Flow of control Branching Loops
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Chapter 3 1 l Branching l Loops l exit(n) method l Boolean data type and logic expressions Flow of Control – Part 1.
Lecturer: Dr. AJ Bieszczad Chapter 3 COMP 150: Introduction to Object-Oriented Programming 3-1 l Branching l Loops l exit(n) method l Boolean data type.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Branching statements.
Chapter 3 Selection Statements
Review 1.
CS0007: Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
Selection (also known as Branching) Jumail Bin Taliba by
The switch Statement, and Introduction to Looping
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
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
Chapter 3 Branching Statements
Operator Precedence Operators Precedence Parentheses () unary
ITM 352 Expressions, Precedence, Working with Strings Class #5
Topics The if Statement The if-else Statement Comparing Strings
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Branching Loops exit(n) method Boolean data type and expressions
Bools & Ifs.
Control Structures – Selection
Bools & Ifs.
Java Programming Control Structures Part 1
Summary Two basic concepts: variables and assignments Basic types:
Statement-Level Control Structures
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 4: Control Structures I (Selection)
Web Programming– UFCFB Lecture 20
Control Structure Chapter 3.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Relational and Logical Operators
CprE 185: Intro to Problem Solving (using C)
Using C++ Arithmetic Operators and Control Structures
Announcements/Reminders
Relational and Logical Operators
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Control Structure.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Selection Statements August 22, 2019 ICS102: The course.
Control Structures.
Presentation transcript:

ITM 352 Flow-Control: if and switch

What is "Flow of Control"? Flow of Control is the execution order of instructions in a program All programs can be written with three control flow elements: 1. Sequence - just go to the next instruction 2. Selection - a choice of at least two either go to the next instruction or jump to some other instruction 3. Repetition - a loop (repeat a block of code) at the end of the loop either go back and repeat the block of code or continue with the next instruction after the block Selection and Repetition are called Branching since these are branch points in the flow of control

The Type boolean A primitive type Can have expressions, values, constants, and variables just as with any other primitive type Only two values: true and false Every expression is boolean in some way 0, 0.0, '0', '' are all false Anything other than the above is true Comparison operators always return boolean $is_desired_grade = ($grade == 'A'); $is_drinking_age = ($age >= 21); $not_graduating = ($year != 'senior');

Boolean Expressions Boolean expressions can be thought of as test conditions (questions) that are either true or false Often two values (numbers, strings) are compared, return value is a boolean (i.e. true or false) For example: Is A greater than B?, Is A equal to B?, Is A less than or equal to B? Comparison operators are used for boolean expressions (<, >, <=, >=, ==, ===, !=. !==, …) A and B can be any data type (or class), but they generally are a "compatible" data type (or class) Comparisons are either numeric or lexicographic but can be user-defined via objects and functions. Comparing non-compatible types is legal but may have unexpected results.

Basic PHP Comparison Operators

A Note on Printing Boolean Values The echo (and print) command will convert values to strings for printing true is converted to '1' false is converted to “” (the empty string) echo true 1 echo false <no output>

"Identical" Comparison Operators Sometimes you really want to be sure two values are exactly the same value and type Is 0.0 equal to 0? Use the '===' and '!==' to test equality and non-equality for both value and type 0.0 == 0 returns true 0.0 === 0 returns false Do lab exercise #1

Compound Boolean Expressions Use && to AND (intersect) two or more conditions Use || to OR (union) two or more conditions For example, write a test to see if B is either 0 or between the values of A and C : (B == 0) || (A <= B && B < C) In this example the parentheses are not required but are added for clarity Subject to Precedence rules

Truth Tables for boolean Operators && (and) || (or) Value of A Value of B A && B true true true true false false false true false false false false Value of A Value of B A || B true true true true false true false true true false false false Value of A !A true false false true ! (not)

Precedence Rules for Common Operators Highest Precedence the unary operators: ++, --, and ! the binary arithmetic operators: *, /, % the binary arithmetic operators: +, - the boolean operators: <, >, =<, >= the boolean operators: ==, != the boolean operator & the boolean operator | the boolean operator && the boolean operator || Lowest Precedence Do lab exercise #2

PHP Flow Control Statements Branching: Selection if if-else if-else if-else if- … - else switch Branching: Repetition while do-while for foreach Sequential the default PHP automatically executes the next instruction unless you use a branching statement

PHP if statement Simple decisions Do the next statement if test is true or skip it if false Syntax: if (Boolean_Expression) Action if true; //execute if true next action; //always executed Note the indentation for readability (not compiler or execution correctness)

if Example The body of the if statement is conditionally executed if ($eggsPerBasket < 12) { //begin body of the if statement echo "Less than a dozen eggs per basket"; //end body of the if statement } $totalEggs = $numberOfEggs * $eggsPerBasket; echo "You have a total of $totalEggs eggs."; The body of the if statement is conditionally executed

PHP Statement Blocks: Compound Statements Action if(true) can be either a single statement or a set of statements enclosed in curly brackets (a compound statement, or block). For example: All statements between braces are controlled by if if ($eggsPerBasket < 12) { //begin body of the if statement echo "Less than a dozen ..."; $costPerBasket = 1.1 * $costPerBasket; } //end body of the if statement $totalEggs = $numberOfEggs * $eggsPerBasket; echo "You have a total of $totalEggs eggs.");

PHP Statement Blocks: Compound Statements Alternatively, "if(<exp>): … endif; " also works: All statements between : and endif; are controlled by if if ($eggsPerBasket < 12) : //begin body of the if statement echo "Less than a dozen ..."; $costPerBasket = 1.1 * $costPerBasket; //end body of the if statement endif; $totalEggs = $numberOfEggs * $eggsPerBasket; echo "You have a total of $totalEggs eggs."); This style is useful when using PHP in large blocks of HTML (see example in textbook) Do lab exercise #3

Gotchas - 1 Statements after the body of the if statement always execute (not conditional unless grouped inside {}'s) Let's say you want to give free shipping and a 10% discount if the order is $50: Wrong: if ($total > 50) $shipping = 0; $total = $total *0.9; Right: if ($total > 50) { $shipping = 0; $total = $total *0.9; }

Gotchas - 2 Never put a ; after the if statement! What will be the values of $total and $shipping after the if test below? Why? $total = 40; if ($total > 50); { $shipping = 0; $total = $total *0.9; }

Two-way Selection: if-else Select either one of two options Either do Action1 or Action2, depending on test value Syntax: if (Boolean_Expression) { Action1 //execute only if Boolean_Expression true } else Action2 //execute only if Boolean_Expression false Action3 //anything here always executed

if-else Examples Example with single-statement blocks: if ($time < $limit) echo "You made it."; else echo "You missed the deadline."; Example with compound statements: { $bonus = 100; } $bonus = 0; Do lab exercise #4

Multibranch selection: if-else if-elseif-…-else One way to handle situations with more than two possibilities Syntax: if(Boolean_Expression_1) { Action_1 } elseif(Boolean_Expression_2) { Action_2 } . elseif(Boolean_Expression_n) { Action_n } else { Default_Action }

if-elseif-elseif-…-else Example if($score >= 90 && $score <= 100) $grade= 'A'; elseif ($score >= 80) $grade= 'B'; elseif ($score >= 70) $grade= 'C'; elseif ($score >= 60) $grade= 'D'; else $grade= 'E'; Note how the sequence is important here and must use elseif rather than just if (why?)

if-elseif-elseif-…-else Non-Example?? if ($profRel == "colleague" ) $greeting = "Thomas"; elseif ($profRel == "friend" ) $greeting = "Tom"; elseif ($profRel == "grad student" ) $greeting = "TC"; elseif ($profRel == "undergrad student" ) $greeting = "professor"; else $greeting = "Dr. Collins";

Use switch for single-variable if switch($profRel) { case "colleague" : $greeting = "Thomas"; break; case "friend" : $greeting = "Tom"; case "grad student" : $greeting = "TC"; case "undergrad student" : $greeting = "professor"; default : $greeting = "Dr. Collins"; } Do lab exercise #5