Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith Solvith PHP Course-

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Lecture 2 Introduction to C Programming
Objectives Using functions to organize PHP code
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
True or false A variable of type char can hold the value 301. ( F )
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Introduction to C Programming
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
4. Python - Basic Operators
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
2440: 211 Interactive Web Programming Expressions & Operators.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Week 8: Decisions Bryan Burlingame 21 October 2015.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 2 Variables.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
ICT Introduction to Programming Chapter 4 – Control Structures I.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
FP512 WEB PROGRAMMING 1 PREPARED BY: PN. NUR SYUHADA BINTI MOHAMAD.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
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.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 4 – C Program Control
JavaScript: Control Statements.
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 7 Additional Control Structures
Chapter 4 - Program Control
PHP.
Chap 7. Advanced Control Statements in Java
Using C++ Arithmetic Operators and Control Structures
Introduction to C Programming
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith Solvith PHP Course- James Song ( )

Lesson Objectives Session 2 [13 Jul] (Changed*) 1. Continuation last week (2.5 hour) 2. Go through previous lab (30 mins) 3. PHP Modular programming. (1 hour) Functions *Students should come with team formation (5 members) *Issuing out of Projects Questions Solvith PHP Course- James Song ( )

Course Grading Criteria- Final 2 x Labs (25 May, 27 July)40% Final Test(24 Aug*)30% Project (24 Aug*)30% Solvith PHP Course- James Song ( )

PHP Syntax Tags  Always start your code with <?php  Ends of your code with ?>  PHP is Case-Sensitive- Captial and Small Capitals matter Instruction Seperation  Each PHP instruction must be ended with a semicolon  To add comments to code use the “//” Solvith PHP Course- James Song ( )

PHP Syntax Tags  Always start your code with <?php  Ends of your code with ?>  PHP is Case-Sensitive- Captial and Small Capitals matter Instruction Seperation  Each PHP instruction must be ended with a semicolon  PHP is line-blind. Ie: don’t need to end each code in a line  To add comments to code use the “//” Solvith PHP Course- James Song ( )

PHP Syntax  Which of this is a valid code ? <?php echo “hi brown cow”; //print hi echo “bye cow brown”; ?> Solvith PHP Course- James Song ( ) <?php echo “hi brown cow” echo “bye cow brown”; ?> <?php echo “hi brown cow”; echo “bye cow brown”; ?> <?php Echo “hi brown cow”; echo “bye cow brown”; ?>

Variable Types Solvith PHP Course- James Song ( ) Booleans  This is the simplest type. A boolean expresses a truth value. It can be either True or False. <?php $name=“John”; $gender_ismale = True; ?>

Variable Types Solvith PHP Course- James Song ( ) Integers  An integer is a number of the set ℤ = {..., -2, -1, 0, 1, 2,...}.  Largest Supported:  Minimum Supported:  Smallest Supported : <?php $name=“John”; $gender_ismale = True; ?>

Variable Types Solvith PHP Course- James Song ( ) Floating Point Numbers  Also known as floats, doubles or real numbers  Basically number with decimal places.  Maximum of 1.8 x 10^308, -1.8 x 10^308 <?php $money= ; ?>

Variable Types Solvith PHP Course- James Song ( ) String  A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256- character set, and hence does not offer native Unicode support.  You can use single quotes or double quote <?php $a=“There is chemistry tomorrow”; $b=‘There is Physics tomorrow”; ?>

Identify the Type Solvith PHP Course- James Song ( )  $abc=‘123’;  $abc=123;  $abc=123.01;  $abc=true;

Casting Between Types Solvith PHP Course- James Song ( )  Do type casting. String to float <?php $Abc=‘123.10’; $Number=floatval($Abc); ?> String to Integer <?php $Abc=‘123.10’; $Number=intval($Abc); ?> Why is there a difference in output ?? $number=123 $number=123.10

Casting Between Types Solvith PHP Course- James Song ( )  Do type casting. Float to Int <?php $Abc=123.10; $abc_int=(int)$Abc; ?> Int to Float <?php $Abc=123; $abc_int=(float)$Abc; ?>

Casting Between Types Solvith PHP Course- James Song ( )  Do type casting. Int to Boolean <?php $yes=1; $yesorno=(bool)$yes; ?> Any number except zero is considered true. Zero is considered false

Operator Solvith PHP Course- James Song ( )  Operator is something that takes in certain operand(Inputs) and return and output.  PHP supports:  Arithmetic Operators  Comparision Operators  Logical (or Relational) Operators  Assignment Operators  Conditional (or ternary) Operators

Operator Solvith PHP Course- James Song ( )  Operator is something that takes in certain operand(Inputs) and return and output or do something.  PHP supports:  Arithmetic Operators  Comparision Operators  Logical (or Relational) Operators  Assignment Operators  Conditional (or ternary) Operators

Operator -Arithmetic Solvith PHP Course- James Song ( ) Assume variable A holds 10 and variable B holds 20 then: OperatorDescriptionExample +Adds two operandsA + B will give 30 -Subtracts second operand from the first A - B will give -10 *Multiply both operandsA * B will give 200 /Divide numerator by denumerator B / A will give 2 %Modulus Operator and remainder of after an integer division B % A will give 0 ++Increment operator, increases integer value by one A++ will give 11 --Decrement operator, decreases integer value by one A-- will give 9

Operator - Comparison Solvith PHP Course- James Song ( ) Assume variable A holds 10 and variable B holds 20 then: Operat or DescriptionExample ==Checks if the value of two operands are equal or not, if yes then condition becomes true. (A == B) is not true. !=Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true. >Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true. <Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true. >=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true. <=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

Operator- Assignment Solvith PHP Course- James Song ( ) Assume variable A holds 10 and variable B holds 20 then: OperatorDescriptionExample =Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assigne value of A + B into C +=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A -=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A *=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A /=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A %=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C %

Expression Solvith PHP Course- James Song ( )  Do type casting. Int to Boolean <?php $yes=1; $yesorno=(bool)$yes; ?> Any number except zero is considered true. Zero is considered false

Control Structure Solvith PHP Course- James Song ( )  The if, elseif...else and switch statements are used to take decision based on the different condition.  You can use conditional statements in your code to make your decisions. PHP supports following three decision making statements: if...else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not true elseif statement - is used with the if...else statement to execute a set of code if one of several condition are true switch statement - is used if you want to select one of many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if..elseif..else code.

Control Structure – If Else Solvith PHP Course- James Song ( ) Syntax: if (condition) { code to be executed if condition is true; } Else { code to be executed if condition is false; } Else portion is optional

Control Structure – If Else Solvith PHP Course- James Song ( ) Example: $d=date(“D”); //get today dates If ($d==‘Fri’){ echo “Have a nice weekend”; }Else{ echo “Have a Nice Day”; } Else portion is optional

Control Structure – Switch Solvith PHP Course- James Song ( )  If you want to select one of many blocks of code to be executed, use the Switch statement.  The switch statement is used to avoid long blocks of if..elseif..else code. Syntax: switch (expression) { case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }

Control Structure – Switch Solvith PHP Course- James Song ( ) Example: <?php $d=date("D"); switch ($d) { case "Mon": echo "Today is Monday"; break; case "Tue": echo "Today is Tuesday"; break; case "Wed": echo "Today is Wednesday"; break; case "Thu": echo "Today is Thursday"; break; case "Fri": echo "Today is Friday"; break; case "Sat": echo "Today is Saturday"; break; case "Sun": echo "Today is Sunday"; break; default: echo "Wonder which day is this ?"; } ?>

Control Structure – For loop Solvith PHP Course- James Song ( )  Syntax for (initialization; condition; increment) { code to be executed; } Example: for ($i=0;$i<5;$i++) { echo $i; echo ‘,’; } Output: 1,2,3,4,

Control Structure – While Solvith PHP Course- James Song ( )  Syntax While (Condition) { Code to be excuted; } Example: $i=0; While ($i<5) { $i++; echo $i; } Output: 1,2,3,4,5

Control Structure – do while Solvith PHP Course- James Song ( )  Syntax do{ Code to be executed; }while(condition);  Remember the semicolon after the while Example: $i=0; do { echo $i; echo ‘,’; $i++; }while ($i<5); Output: 0,1,2,3,4,

Control Structure – Break and Continue Solvith PHP Course- James Song ( )  The PHP break keyword is used to terminate the execution of a loop prematurely.  The break statement is situated inside the statement block. If gives you full control and whenever you want to exit from the loop you can come out. After coming out of a loop immediate statement to the loop will be executed. for ($i=0;$i<5;$i++) { echo $i; If ($i==3) break; echo ‘,’; } Output: 0,1,2,3

Control Structure – Break and Continue Solvith PHP Course- James Song ( )  The PHP continue keyword is used to halt the current iteration of a loop but it does not terminate the loop.  Just like the break statement the continue statement is situated inside the statement block containing the code that the loop executes, preceded by a conditional test. For the pass encountering continue statement, rest of the loop code is skipped and next pass starts. for ($i=0;$i<5;$i++) { If ($i==3) continue; echo $i; echo ‘,’; } Output: 0,1,2,4,

Hands-on – Leap-Year Solvith PHP Course- James Song ( )  Generate the last 127 leap year dates. Leap-Year Criteria In the Gregorian calendar 3 criteria must be taken into account to identify leap years: The year is evenly divisible by 4; If the year can be evenly divided by 100, it is NOT a leap year, unless; The year is also evenly divisible by 400. Then it is a leap year.

Hands-on – Fortune Cookies Solvith PHP Course- James Song ( ) Write a program cookies.c to read in a positive integer and add up its digits repeatedly until the sum is a single digit. For example, if the integer is 12345, then adding its digits ( ) yields 15, and adding its digits again (1 + 5) yields 6. Hence the answer is 6. Using this single digit result, print out the corresponding Fortune Cookie message according to the table below:

Hands-on – Fortune Cookies Solvith PHP Course- James Song ( ) DigitFortune Cookie message 1You will have a fine capacity for the enjoyment of life. 2Now is the time to try something new. 3Don't let doubt and suspicion bar your progress. 4 Your principles mean more to you than any money or success. 5Accept the next proposition you hear. 6 A handful of patience is worth more than a bushel of brains. 7You have an active mind and a keen imagination. 8You are talented in many ways. 9Treat everyone as a friend.

Hands-on – Random NRIC Generator Solvith PHP Course- James Song ( ) Write a program that can generate random NRIC number of Singaporeans including the alphabetical checksum. Generate 100 of them. d= [ (d 1 d 2 d 3 d 4 d 5 d 6 d 7 ) ( ) ] mod 11 = ( 2d 1 + 7d 2 + 6d 3 + 5d 4 + 4d 5 + 3d 6 + 2d 7 ) mod 11

Go through last session lab Solvith PHP Course- James Song ( ) Go through last session lab

PHP Functions Solvith PHP Course- James Song ( )  PHP functions are similar to other programming languages. A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.  Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it. Following example creates a function called writeMessage() and then calls it just after creating it.  Note that while creating a function its name should start with keyword function and all the PHP code should be put inside { and } braces as shown in the following example below:

PHP Functions Solvith PHP Course- James Song ( ) Syntax: function functionname(inputs) { Code; return output; }

PHP Functions with parameters Solvith PHP Course- James Song ( ) PHP gives you option to pass your parameters inside a function. You can pass as many as parameters your like. These parameters work like variables inside your function. Following example takes two integer parameters and add them together and then print them. <?php function addFunction($num1, $num2) { $sum = $num1 + $num2; echo "Sum of the two numbers is : $sum"; } addFunction(10, 20); ?>

PHP Functions with Return Value Solvith PHP Course- James Song ( )  A function can return a value using the return statement in conjunction with a value or object. return stops the execution of the function and sends the value back to the calling code.  You can return more than one value from a function using return array(1,2,3,4).  Following example takes two integer parameters and add them together and then returns their sum to the calling program. Note that return keyword is used to return a value from a function. <?php function addFunction($num1, $num2) { $sum = $num1 + $num2; return $sum;} $return_value = addFunction(10, 20); echo "Returned value from the function : $return_value ?>

Solvith PHP Course- James Song ( )  Encapsulate your prime number function Hands-on – Prime number Function

Solvith PHP Course- James Song ( )  Reverse numbers Hands-on – Number reversal function