Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.

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

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
IDK0040 Võrgurakendused I harjutus 07: PHP: Operators, Switch, Forms Deniss Kumlander.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Chapter 4 Selection Structures: Making Decisions.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Previously Repetition Structures While, Do-While, For.
If Statements If statements: allow the computer to make a decision Syntax: if (some condition) { then do this; } else { then do that; } no semicolons on.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Agenda Basic Logic Purpose if statement if / else statement
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
Exercise 1: IF/ELSE Step 1: Open NotePad++ and create “number.php” in your webspace Step 2: Write codes to do the following 1.Generate a random number.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Decision Statements, Short- Circuit Evaluation, Errors.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
JAVA PROGRAMMING Control Flow. Jeroo: Finish Activities 1-7 Finish InputTest program w/changes.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 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.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
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 this.
Selection (also known as Branching) Jumail Bin Taliba by
IF if (condition) { Process… }
Control Structures: Selection Statement
During the last lecture we had a discussion on Data Types, Variables & Operators
3 Control Statements:.
Conditionals.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Control Structures: Selection Statement
Presentation transcript:

Lecture 2 Conditional Statement

chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions are performed for different decision. PHP has the following conditional statements If statement: executes a code only if a condition is met. If – else statement: executes a code if a condition is true and if false, executes another code. If – else if – else statement: if the first condition is not true, another condition is tested, if all condition fails, then a code is executed. Switch Statement: selects a block of code out of several blocks to be executed.

chcslonline.org IF Statement If statement executes a code only if a condition is met. It has the syntax: If (condition){ do something } For example <?php $image = “car”; If ($image == “car”){ print ‘ ’; } ?>

chcslonline.org So here we have used both the if statement and the comparison operator = = (equal). It checks if the value assigned to the variable $image is car, if it is, the image is displayed and if not, nothing is displayed. Assuming we change the value of $image to be moon. Nothing will be displayed because the condition is false.

chcslonline.org If-else statement Assuming we want to do something else if a condition is false, the if-else statement is used. It has the syntax If (condition){ do something } else{ Do something else } For example, lets try using another comparison operator.

chcslonline.org practical 5.2 Given x = 5, write a program that will check if the value of x is an odd number or an even number. To do this, we will first declare a variable x and then store the value 5 in it. Then we will write the if – else statement. We also know that every even number is divisible by 2 without remainder and every odd number will have a remainder when divided by 2. to test this condition, we will use the modulus operator to check if dividing the value of x by 2 gives a remainder or not then a comparison operator to check.

chcslonline.org solution <?php $x = 5; if ($x%2 !=0){ print “X is an odd number”; } else{ print “X is an even number”; }

chcslonline.org If-else if -else Assuming we have more than two conditions to test. The if – else if –else statement is used. It has the syntax if (condition){ Do something; } else if (another condition){ Do something; } else{ Do something else; }

chcslonline.org Switch statement Assuming we have more than two conditions to test, multiple if statement will be more complex to handle this. The best way will be to use a switch statement. It chooses one block of code out of several blocks to execute if the condition for that code evaluates to true. It has the syntax. Switch (n){ case x: Do something; Break; Case x: Do something; Break; Default: Do something; } Where n is the variable name and x is the value of the variable we are testing. The break is used to prevent the code from automatically running into the next block of code The default is used to handle a situation when all case fails or evaluates to false.

chcslonline.org practical 5.3 Write a program to output the class of a grade depending on the Grade of the user. <?php $grade =“A” ; Switch ($grade){ Case “A”: echo “Excellent”; break; Case “B”: Echo “very good”; Break; Case “C”: Echo “Average”; Break; Default: Echo “Please Enter a valid grade”; }