CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.

Slides:



Advertisements
Similar presentations
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On..
Advertisements

1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
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.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
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.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
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
Computer Science Selection Structures.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
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.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Control statements Mostafa Abdallah
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
CSE202: Lecture 5The Ohio State University1 Selection 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.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
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)
 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.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Chapter 3 Selection Statements
Selections Java.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in JavaScript
More Selections BIS1523 – Lecture 9.
Topics The if Statement The if-else Statement Comparing Strings
Control Structures: Selection Statement
Pages:51-59 Section: Control1 : decisions
Logical Operations In Matlab.
Testing and Comparing Values
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
Pages:51-59 Section: Control1 : decisions
CprE 185: Intro to Problem Solving (using C)
PHP CONDITIONAL STATEMENTS
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

CONTROL STRUCTURE The if, elseif, and else & switch Statements 1

The if, elseif, and else Statements  The fundamentals of Boolean comparison  Conditional expressions and operators  How to combine expressions with logical operators  How to nest conditionals 2

if Conditionals  In the simplest sense, all decisions are made based on a condition: something that may or may not be true  The if statement follows this syntax: if (condition) { statements } 3

example if Conditionals <?php $count=10; // Condition 1 if ($count==10) { echo “Condition 1 outputs this.”; } ?> 4

If-else Conditionals  The if statement follows this syntax: if (condition) { Statements is true } else { Statements is false } 5

example if-else Conditionals <?php $count=5; // Condition is true if ($count==10) { echo “Condition 1 outputs this TRUE.”; } // Condition is false { echo “Condition 2 outputs this FALSE.”; } ?> 6

If-elseif-else Conditionals  The if statement follows this syntax: if (condition1) { Statements is true condition1 } elseif (condition2) { Statements is true condition2 } else { Statements is false } 7

example if-elseif-else Conditionals <?php $count=5; if ($count<=0)// Condition1 { echo “Condition 1 outputs this TRUE.”; } if ($count>5)// Condition2 { echo “Condition 1 outputs this TRUE.”; } else// Condition is false { echo “Other Condition outputs this FALSE.”; } ?> 8

Exercise – ifelse.php  For example, let’s assume you want to place someone into a four-category age-grouping system. The age groups are divided as follows:  Group A—Ages 20 and younger  Group B—Ages 21 to 40  Group C—Ages 41 to 60  Group D—Ages 61 and older  Give input value age from keyboard and display on monitor.  POST value on safe from 9

The Comparison Operators  The syntax for conditional operators is the same as other binary operators; that is, one value should be placed on each side of the operator, as shown here: Operator Name Example == Equal $var1 == $var2 === Identical $var1 === $var2 != Not equal $var1 != $var2 !== Not identical $var1 !== $var2 > Greater than $var1 > $var2 >= Greater than or equal to $var1 >= $var2 < Less than $var1 < $var2 <= Less than or equal to $var1 <= $var2 10

The == and === operators  The == and === operators (and their != and !== relatives) aren’t the same. The first, == which checks two values for equality, compares two values of the same data type. If they aren’t the same type, == typecasts them before they are compared. Therefore, the string “1” and the integer 1 are the same.  On the other hand, === checks each value’s type before comparing the actual value. If the types don’t match, the variables aren’t considered the same, and false is returned.  Only if two variables are identical (that is, their type and value both match) will this operator return true. 11

the Logical Operators  The && operator returns true if and only if both operands are true. Operator Name Example AND And $var1 and $var2 OR Or $var1 or $var2 XOR Exclusive or $var1 xor $var2 && And $var1 && $var2 || Or $var1 || !Not!$var1 12

AND operator in detail.  The AND (or &&) Operator Requires That Both of the Values Be True Left Value Right Value Return Value True True True True False False False True False False False False 13

OR operator in detail.  The OR (or ||) Operator Requires That at Least One of the Values Be True Left Value Right Value Return Value True True True True False True False True True False False False 14

XOR operator in detail.  The XOR Operator Requires That Only One of the Values Be True. Left Value Right Value Return Value True True False True False True False True True False False False 15

Not operator in detail.  The NOT (or !) Operator Reverses the Value of a Boolean Value Right Value Return Value True False False True 16

The switch Statement  The differences between switch and if-elseif-else  The syntax for the switch statement  How to break switch statement execution  How to specify a default case  How to create multifunction pages 17

switch Statement  A switch statement is used to compare a single variable with multiple possible values.  Here’s the syntax for a switch statement: switch (variable) { case value:code; break; case value:code; break; default:code; break; } 18

If-elseif-else program <?php /* ex01.php – demonstrates age group output using an if-elseif-else clause */ $chrAgeGroup = ‘B’; // Specify age group if ($chrAgeGroup == ‘A’) // Group A: 20 and younger { echo “Ages 20 and younger”; } elseif ($chrAgeGroup == ‘B’) // Group B: 21 – 40 { echo “Ages 21 to 40”; } elseif ($chrAgeGroup == ‘C’) // Group C: 41 – 60 { echo “Ages 41 to 60”; } else // Group D: 61 and older { echo “Ages 61 and older”; } ?> 19

<?php $chrAgeGroup = ‘B’; switch ($chrAgeGroup) { case ‘A’: // Group A: 20 and younger echo “Ages 20 and younger”; break; case ‘B’: // Group B: 21 – 40 echo “Ages 21 to 40”; break; case ‘C’: // Group C: 41 – 60 echo “Ages 41 to 60”; break; case ‘D’: // Group D: 61 and older echo “Ages 61 and older”; break; } ?> 20

<?php // Assign a number to be compared $intNumber = 7; switch ($intNumber > 5) { case true: echo “$intNumber is greater than 5”; break; case false: echo “$intNumber is less than or equal to 5”; break; } ?> 21

Multiple Cases for the Same Code  In some cases you may find that you have two cases in a switch statement that should prefix the same code. In fact, you could have any number of cases that should be followed by one block of code. 22

<?php /* color choices using if statement */ $strColor = ‘Blue’; // Decide if color is warm or cool if ($strColor == ‘Red’ || $strColor == ‘Orange’ || $strColor == ‘Yellow’) { echo “$strColor is a warm color”; } else { echo “$strColor is a cool color”; } ?> 23

<?php $strColor = ‘Blue’; switch ($strColor) { case ‘Red’: case ‘Orange’: case ‘Yellow’: echo “$strColor is a warm color”; break; default: echo “$strColor is a cool color”; break; } ?> 24

exercise  Program Grade: Input point form keyboard, and then decision to grade follow this:  80 – 100= A  70 – 79= B  60 – 69= C  50 – 59= D  0 – 49= F  Use if-elseif-else & switch to programming 25