Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.

Slides:



Advertisements
Similar presentations
Chapter 3: Control Flow S. M. Farhad. Statements and Blocks An expression becomes a statement when it is followed by a semicolon Braces { and } are used.
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
ITC 240: Web Application Programming
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
UNIT II Decision Making And Branching Decision Making And Looping
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
The foreach LooptMyn1 The foreach Loop The foreach loop gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error.
Decisions, Loops, and Arrays Achmad Arwan, S.Kom.
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
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.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Controlling Execution Dong Shao, Nanjing Unviersity.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
COMP Loop Statements Yi Hong May 21, 2015.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
 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.
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.
PHP using MySQL Database for Web Development (part II)
Branching statements.
Core Java Statements in Java.
Expressions and Control Flow in PHP
REPETITION CONTROL STRUCTURE
Arrays An array in PHP is an ordered map
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Control Structures.
JavaScript: Control Statements I
Chapter 5: Control Structures II
Chapter 2.2 Control Structures (Iteration)
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
ITM 352 Flow-Control: Loops
Arrays, For loop While loop Do while loop
By Hector M Lugo-Cordero September 3, 2008
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
Chap 7. Advanced Control Statements in Java
Controlling Program Flow
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Unit – 3 Control structures

Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens if you do not work hard? Well, you fail! This is an example of an if/else conditional statement.

<?php $number_three = 3; if ( $number_three == 3 ) { echo "The if statement evaluated to true"; } else { echo "The if statement evaluated to false"; } ?>

<?php $employee = “xyz”; if($employee == “abc") { echo "Hello Ma'am"; } elseif($employee == “xyz") { echo "Good Morning Sir!"; } else { echo "Morning"; } ?>

2. Switch Use the switch statement to select one of many blocks of code to be executed. switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; default: code to be executed if n is different from both label1 and label2; }

<?php $x=1; switch ($x) { case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3"; } ?>

3. The Ternary Operator It is called the ternary operator because it takes three operands - a condition, a result for true, and a result for false.

Loops 1.while:- The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE. Syn:- while (expr): statement... endwhile;

<?php $i = 1; while ($i <= 10) { echo $i++; /* the printed value would be $i before the increment (post-increment) */ }

2. Break:- break ends execution of the current for, for each, while, do-while or switch structure. break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.

<?php $i=0; while($i<11) { switch($i++) { case 5: echo "5th......&nbsp "; /* Exit only the switch. */ break 1; case 10: echo "10th....."; /* Exit the switch and the while. */ break 2; default: break; } ?>

3. Continue:- continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

<?php $x=0; echo 'List of odd numbers between 1 to 10 '; while ($x<10) { $x++; if ($x==3) { continue; } echo $x.' '; } ?>

4. Do…. While:- do-while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. '; do { echo 'i value is '. $i. ', so code block will run. '; ++$i; } while ($i

5.For loop:- The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop. In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends. for (expr1; expr2; expr3) statement E.X:-

6. ForThis simply gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes; the second is a minor but useful extension of the first: foreach (array_expression as $value) statement OR foreach (array_expression as $key => $value) statement

<?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } // $arr is now array(2, 4, 6, 8) unset($value); // break the reference with the last element ?>

<?php //$a=array(38,23); $a[1] = "32"; $a['Quagmire'] = "30"; $a['Joe'] = "34"; foreach($a as $key1=>$value1) echo "$key1:$value1 "; ?>

$value) echo "$key: $value\n"; var_dump(key($a)); /* Produces: 0: zero 1: one 2: two 3: three int(1) */ ?>

Exit() exit — Output a message and terminate the current script Description void exit ([string $status ] ) void exit ( int $status ) //exit program normally exit; exit(); exit(0); //exit with an error code exit(1);

<?php $x=0; echo 'List of odd numbers between 1 to 10 '; while ($x<10) { $x++; if ($x==3) { exit(); } echo $x.' '; }?>

Die() die — Equivalent to exit() Description:- This language construct is equivalent to exit().

Return() If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call.

<?php function square($num) { return $num * $num; } function a($num,$num1) { return $num+$num1; } echo square(4)." "; echo a(4,5) ?>

Arrays in PHP An array is a variable that holds a group of values. Arrays are usually meant to store a collection of related data elements, although this is not a necessity. You access the individual elements by referring to their index position within the array. The position is either specificied numerically or by name

$arrName=array( key => value,... ) // key may be an integer or string // value may be any value OR $arr[key] = value; $arr[] = value; // key may be an integer or string // value may be any value

<?php $a=array(‘xyz’=>38,2=>23); /*$a[1] = "32"; $a['Quagmire'] = "30"; $a['Joe'] = "34";*/ echo "Peter is ". $a[2]. " years old."; ?>

"bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12]; // 1 ?>

1, 12 => 2); $arr[] = 56; // This is the same as $arr[13] = 56; // at this point of the script $arr["x"] = 42; // This adds a new element to // the array with key "x" unset($arr[5]); // This removes the element from the array unset($arr); // This deletes the whole array ?>