MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
true (any other value but zero) false (zero) expression Statement 2
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Visual C++ Programming: Concepts and Projects
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
The switch Statement, DecimalFormat, and Introduction to Looping
Fundamental Programming Fundamental Programming More on Selection.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
CONTROLLING PROGRAM FLOW
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.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
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.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
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.
Week 4 Program Control Structure
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditional Statements + Loops ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
CSI 3125, Preliminaries, page 1 Control Statements.
Chapter 6 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
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)
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
 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 6 Conditionals.
Selection—Making Decisions
CiS 260: App Dev I Chapter 4: Control Structures II.
Expressions and Control Flow in JavaScript
Control Structures.
JavaScript conditional
More Selections BIS1523 – Lecture 9.
IF if (condition) { Process… }
Control Structures: Selection Statement
Pages:51-59 Section: Control1 : decisions
Visual Basic – Decision Statements
Week 3 – Program Control Structure
CS2011 Introduction to Programming I Selections (I)
Program Flow.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
Pages:51-59 Section: Control1 : decisions
PHP CONDITIONAL STATEMENTS
Conditionals.
Chapter 6 Conditionals.
Presentation transcript:

MS3304: Week 6 Conditional statements

Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making statements in PHP Complex decision making statements in PHP Other uses of if statements Advanced topic introduction: the switch statement

Program flow of control Order of statement execution is called the flow of control Default order of execution of programming statements is linear Some statements (conditional) can modify the order of execution Conditional statements are based on Boolean logic

Conditional statements Conditional statements allow the script to decide which statement will be executed next Conditional statements in PHP are: –if –else –elseif

Overview of an the if statement The basic if statement has the following syntax if (condition){ statement; } if is a PHP reserved word The condition is a Boolean expression that can be evaluated as true or false If the condition is true, this statement is executed

Boolean logic Developed by George Boole in mid 1800s All Boolean logic is based on the evaluation of statements These statement can either be evaluated as true or false Conditional and logical operators are used to create these statements

Boolean logic example Assuming we have the following variables: $CW1 $CW2 $CWAverage = ($CW1+$CW2)/2 What logical statements, in plain English, can we make to tell students at UEL if they have passed or need to do a resit?

Comparative and logical operators Comparative ==equal to !=not equal to <less than >greater than <=less than or equal to >=greater than or equal to Logical !NOT &&AND ||OR

Comparative and logical operators example 1 if ($CW1=="NS"){ echo "CW1: 0 - Resit (not submitted). \n"; } if($CW1<30){ echo "CW1: $CW1 - Resit (below 30). \n"; } if(($CW1>29) && ($CW1<40)){ echo "CW1: $CW1 - not passed, but threshold met. \n"; } if ($CW1<40){ echo "CW1 passed. \n"; }

Comparative and logical operators example 2 if ( ($CW1<30 || $CW1 == "NS") || ($CW2<30 || $CW2 == "NS") || ($CWAverage <40) ){ echo "You have not passed the module and must resit at least one of the of the pieces of CW."; }

Comparative and logical operators example 3 if ( ($CW1>=30 && $CW1!="NS") && ($CW2>=30 && $CW2!="NS") && ($CWAverage>=40) ){ echo "You have passed the module with a mark of $CWAverage."; }

Overview of an the if/else statement The basic if/else statement has the following syntax if (condition){ statements; } else{ statements; } If the condition is true these statements are executed If the initial condition is false then the second block of statements is executed

if/else statements if ( ($CW1>=30 && $CW1!="NS") && ($CW2>=30 && $CW2!="NS") && ($CWAverage>=40) ){ echo "You have passed the module!" }else{ echo "You have not passed the module and must resit at least one of the of the pieces of CW."; }

Overview of an the if/elseif statement The basic if/else statement has the following syntax if (condition){ statements; } elseif (condition){ statements; } else{ statements; } If the condition is true these statements are executed If neither of the conditions are true, then these statements are executed If the first condition is false it tests the second condition, its true these statements are executed.

if/elseif statements if ($CW1=="NS"){ echo "CW1: 0 - Resit (not submitted). \n"; }elseif($CW1<30){ echo "CW1: $CW1 - Resit (below 30). \n"; }elseif(($CW1>29) && ($CW1<40)){ echo "CW1: $CW1 - not passed, but threshold met. \n"; }else{ echo "CW1: $CW1 - passed. \n"; }

Nested if statements if (condition){ statements } else{ statements } } else{ statements }

Nested if statements example The Marks Calculator

Other uses of if statements if ($catGender=="Male" ){ $catPronoun = "He"; }elseif ($catGender=="Female" ) { $catPronoun = "She"; } else { $catPronoun = "The cat"; } echo "I have a $catGender $catColour cat named $myCat. $catPronoun is $catAge years old."

If statements your turn Write the PHP statements to –Take both a cat’s age in years and a humans as input passed from a form –Calculates the age of the cat in human years and displays it –Displays a statement saying if the cat is older or younger than the person in cat equivalent years Cat age calculation formula details Year 1 = 15 human years Year 2 = 9 human years Years 3+ = 4 human years per year

Advanced topic: The switch statement switch ($catGender){ case "Male" : $catPronoun = "He"; break; case "Female" : $catPronoun = "She"; break; default : $catPronoun = "The cat"; }

PHP conditional statements summary Used to control the flow of execution of statements in a script Based on the evaluation of a Boolean statement through the use of comparative and logical operators Can use the if, elseif and else statements to compose more logically complex statements Can be nested within each other