LESSON 2: CONTROL STRUCTURES JAVASCRIPT. CONTROL STRUCTURES – ALLOWS US TO CHANGE THE ORDERING OF HOW THE STATEMENTS IN OUR PROGRAMS ARE EXECUTED.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Control Structures Corresponds with Chapters 3 and 4.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4 Making Decisions
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
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.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Copyright 2003 Scott/Jones Publishing Making Decisions.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
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.
JavaScript, Fourth 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.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Decision Statements, Short- Circuit Evaluation, Errors.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Decision Making and Branching
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
 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.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
CHAPTER 4 DECISIONS & LOOPS
The if…else Selection Statement
Decision Making in C.
The switch Statement, and Introduction to Looping
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
Flow of Control.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 6: Conditional Statements and Loops
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Control Structures.
Control Structures: Selection Statement
3 Control Statements:.
Structured Program Development in C++
PROGRAM FLOWCHART Selection Statements.
Control Structures: Selection Statement
Presentation transcript:

LESSON 2: CONTROL STRUCTURES JAVASCRIPT

CONTROL STRUCTURES – ALLOWS US TO CHANGE THE ORDERING OF HOW THE STATEMENTS IN OUR PROGRAMS ARE EXECUTED

TWO TYPES OF CONTROL STRUCTURES – DECISION CONTROL STRUCTURES ● ALLOWS US TO SELECT SPECIFIC SECTIONS OF CODE TO BE EXECUTED – REPETITION CONTROL STRUCTURES ● ALLOWS US TO EXECUTE SPECIFIC SECTIONS OF THE CODE A NUMBER OF TIMES

DECISION CONTROL STRUCTURES – JAVASCRIPT STATEMENTS THAT ALLOWS US TO SELECT AND EXECUTE SPECIFIC BLOCKS OF CODE WHILE SKIPPING OTHER SECTIONS TYPES: – IF-STATEMENT – IF-ELSE-STATEMENT – IF-ELSE IF-STATEMENT

IF-STATEMENT – SPECIFIES THAT A STATEMENT (OR BLOCK OF CODE) WILL BE EXECUTED IF AND ONLY IF A CERTAIN BOOLEAN STATEMENT IS TRUE.

IF-STATEMENT HAS THE FORM: IF( BOOLEAN_EXPRESSION ) STATEMENT; OR IF( BOOLEAN_EXPRESSION ){ STATEMENT1; STATEMENT2; }

CONDITIONAL CODE IF ( CONDITION){ //CODE GOES HERE }

IF-STATEMENT FLOWCHART

SAMPLE PROGRAM2: VAR GRADE = 68; IF( GRADE > 60 ){ DOCUMENT.WRITE("CONGRATULATIONS!"); DOCUMENT.WRITE("YOU PASSED!"); }

CODING GUIDELINES 1. THE BOOLEAN_EXPRESSION PART OF A STATEMENT SHOULD EVALUATE TO A BOOLEAN VALUE. THAT MEANS THAT THE EXECUTION OF THE CONDITION SHOULD EITHER RESULT TO A VALUE OF TRUE OR A FALSE. 2. INDENT THE STATEMENTS INSIDE THE IF-BLOCK. FOR EXAMPLE, IF( BOOLEAN_EXPRESSION ){ //STATEMENT1; //STATEMENT2; }

IF-ELSE STATEMENT – USED WHEN WE WANT TO EXECUTE A CERTAIN STATEMENT IF A CONDITION IS TRUE, AND A DIFFERENT STATEMENT IF THE CONDITION IS FALSE.

IF-ELSE STATEMENT HAS THE FORM: IF( BOOLEAN_EXPRESSION ){ STATEMENT1; STATEMENT2;... } ELSE{ STATEMENT3; STATEMENT4;... }

SAMPLE PROGRAM: VAR GRADE = 68; IF( GRADE > 60 ) DOCUMENT.WRITE("CONGRATULATIONS!"); ELSE DOCUMENT.WRITE("SORRY YOU FAILED");

SAMPLE PROGRAM2: VAR GRADE = 68; IF( GRADE > 60 ){ DOCUMENT.WRITE("CONGRATULATIONS!"); DOCUMENT.WRITE("YOU PASSED!"); } ELSE{ DOCUMENT.WRITE("SORRY YOU FAILED"); }

CODING GUIDELINES: 1. TO AVOID CONFUSION, ALWAYS PLACE THE STATEMENT OR STATEMENTS OF AN IF OR IF-ELSE BLOCK INSIDE {}. 2. YOU CAN HAVE NESTED IF-ELSE BLOCKS. THIS MEANS THAT YOU CAN HAVE OTHER IF-ELSE BLOCKS INSIDE ANOTHER IF-ELSE BLOCK. FOR EXAMPLE, IF( BOOLEAN_EXPRESSION ){ //SOME STATEMENTS HERE } ELSE{ //SOME STATEMENTS HERE }

EQUALITY = ASSIGNMENT OPERATOR == COMPARING EQUALITY … IF THE VALUE IS THE SAME === STRICT EQUALITY,VALUE AND DATA TYPE MUST BE THE SAME

SAMPLE CODE: VAR AMOUNT =500; IF(AMOUNT<1000){ ALERT(“IT’S LESS THAN 1000"); }

SAMPLE CODE: VAR GRADE =75; IF(GRADE<75){ ALERT(“CONGRATULATIONS!"); } ELSE{ ALERT(“SORRY,YOU FAILED”); }

SAMPLE CODE: VAR A =5; VAR B ="5"; IF(A==B){ ALERT("YES THEY ARE EQUAL"); } ELSE{ ALERT("YES THEY ARE NOT EQUAL"); }

IF-ELSE-ELSE IF STATEMENT: ● THE STATEMENT IN THE ELSE-CLAUSE OF AN IF-ELSE BLOCK CAN BE ANOTHER IF-ELSE STRUCTURES. ● THIS CASCADING OF STRUCTURES ALLOWS US TO MAKE MORE COMPLEX SELECTIONS. ● THE STATEMENT HAS THE FORM: IF( BOOLEAN_EXPRESSION1 ) STATEMENT1; ELSE IF( BOOLEAN_EXPRESSION2 ) STATEMENT2; ELSE STATEMENT3;

SAMPLE PROGRAM: VAR GRADE = 68; IF( GRADE > 90 ){ ALERT("VERY GOOD!"); } ELSE IF( GRADE > 60 ){ ALERT(" GOOD!"); } ELSE{ ALERT("SORRY YOU FAILED"); }

EXERCISE /*CREATE A SCRIPT THAT WILL COMPUTE THE REAL ESTATE TAX, GIVEN THE FOLLOWING FORMULAS: A.)IF VALUE OF REAL ESTATE IS LESS THAN 250,001.00, TAX IS 5% OF REAL ESTATE VALUE. B.)IF VALUE OF REAL ESTATE IS BETWEEN 250, TO 500,000, TAX IS 10% OF THE REAL ESTATE VALUE. C.)IF MORE THAN 500,000 TAX IS 15% OF THE REAL ESTATE VALUE.*/

EXERCISE /*AN EMPLOYEE’S WEEKLY WORKING HOURS IS 40. IF AN EMPLOYEE EXCEEDS 40 HOURS, IT IS CONSIDERED OVERTIME. CREATE A SCRIPT THAT WILL INSPECT THE NUMBER OF HOURS WORKED BY EMPLOYEE AND HIS/HER HOURLY RATE AND PRINT THE GROSS PAY AND OVERTIME PAY RENDERED, IF THERE’S NO OT PAY TO PRINT, PRINT ONLY THE GROSS PAY. TO COMPUTE FOR THE GROSS PAY OF THE EMPLOYEE, MULTIPLY THE NUMBER OF HOURS WORKED BY HIS/HER HOURLY RATE PLUS HIS/HER OT PAY. OT HOURS ARE TIME RENDERED BY EMPLOYEE OVER 40 HOURS. THE OVERTIME HOURS RENDERED SHOULD BE COMPUTED BY USING 150% OF HIS HOURLY RATE*/

EXERCISE /*CREATE A SCRIPT THAT WILL CHECK THE NUMBER AND PRINT EITHER ODD OR EVEN*/

EXERCISE /*CREATE A SCRIPT THAT WILL ASSESS WHICH OF THE 3 NUMBERS INSTANTIATED IS THE HIGHEST*/

LOGICAL AND/OR IF(A==B && C==D) IF(A==B || C==D)

EXERCISE: GRADE: 90 – 100 = “EXCELLENT” 80 – 89 = “GOOD JOB” 60 – 79 = “STUDY HARDER” 59 – BELOW = “SORRY YOU FAILED”

MODULUS OPERATOR : % VAR NUM = 3; VAR MOD= NUM% 2; IF(MOD==0){ ALERT("EVEN"); } ELSE{ ALERT("ODD"); }

UNARY OPERATOR INCREMENT A++; ++A; DECREMENT A--; --A;

WHILE LOOP VAR A =1; WHILE(A<10){ DOCUMENT.WRITE("SHA"); A++; }

DO WHILE VAR A=1; DO{ DOCUMENT.WRITE(); A++; } WHILE(A<10);

FOR LOOP FOR(I=1; I<5; I++){ DOCUMENT.WRITE(“I LOVE JAVASCRIPT”); }

SWITCH VAR GENDER=“F"; SWITCH(A){ CASE "F": CONSOLE.LOG("YOU ARE A FEMALE"); BREAK; CASE "M": CONSOLE.LOG("YOU ARE A MALE"); BREAK; }

SWITCH-STATEMENT USE THE SWITCH STATEMENT TO SELECT ONE OF MANY BLOCKS OF CODE TO BE EXECUTED. SWITCH – ALLOWS BRANCHING ON MULTIPLE OUTCOMES. WHERE, – SWITCH_EXPRESSION ● IS AN INTEGER OR CHARACTER EXPRESSION – CASE_SELECTOR1, CASE_SELECTOR2 AND SO ON, ● ARE UNIQUE INTEGER OR CHARACTER CONSTANTS

SWITCH STATEMENT HAS THE FORM: SWITCH( SWITCH_EXPRESSION ){ CASE CASE_SELECTOR1: STATEMENT1;// STATEMENT2;//BLOCK 1 BREAK; CASE CASE_SELECTOR2: STATEMENT1;// STATEMENT2;//BLOCK 2 BREAK; : DEFAULT: STATEMENT1;// STATEMENT2;//BLOCK N }

SWITCH-STATEMENT WHEN A SWITCH IS ENCOUNTERED, – JAVASCRIPT FIRST EVALUATES THE SWITCH_EXPRESSION, AND JUMPS TO THE CASE WHOSE SELECTOR MATCHES THE VALUE OF THE EXPRESSION. – THE PROGRAM EXECUTES THE STATEMENTS IN ORDER FROM THAT POINT ON UNTIL A BREAK STATEMENT IS ENCOUNTERED, SKIPPING THEN TO THE FIRST STATEMENT AFTER THE END OF THE SWITCH STRUCTURE. – IF NONE OF THE CASES ARE SATISFIED, THE DEFAULT BLOCK IS EXECUTED. TAKE NOTE HOWEVER, THAT THE DEFAULT PART IS OPTIONAL.

SWITCH-STATEMENT NOTE: – UNLIKE WITH THE IF STATEMENT, THE MULTIPLE STATEMENTS ARE EXECUTED IN THE SWITCH STATEMENT WITHOUT NEEDING THE CURLY BRACES. – WHEN A CASE IN A SWITCH STATEMENT HAS BEEN MATCHED, ALL THE STATEMENTS ASSOCIATED WITH THAT CASE ARE EXECUTED. NOT ONLY THAT, THE STATEMENTS ASSOCIATED WITH THE SUCCEEDING CASES ARE ALSO EXECUTED. – TO PREVENT THE PROGRAM FROM EXECUTING STATEMENTS IN THE SUBSEQUENT CASES, WE USE A BREAK STATEMENT AS OUR LAST STATEMENT.

FLOW CHART

CODING GUIDELINES 1. DECIDING WHETHER TO USE AN IF STATEMENT OR A SWITCH STATEMENT IS A JUDGMENT CALL. YOU CAN DECIDE WHICH TO USE, BASED ON READABILITY AND OTHER FACTORS. 2. AN IF STATEMENT CAN BE USED TO MAKE DECISIONS BASED ON RANGES OF VALUES OR CONDITIONS, WHEREAS A SWITCH STATEMENT CAN MAKE DECISIONS BASED ONLY ON A SINGLE INTEGER OR CHARACTER VALUE. ALSO, THE VALUE PROVIDED TO EACH CASE STATEMENT MUST BE UNIQUE.

ACTIVITY: SWITCH ACTIVITY 1 SWITCH ACTIVITY2

WINDOW.ONLOAD = INITALL; FUNCTION INITALL() { SWITCH(NAVIGATOR.PLATFORM) { CASE "WIN32": ALERT("YOU'RE RUNNING WINDOWS"); BREAK; CASE "MACPPC": ALERT("YOU HAVE A POWERPC-BASED MAC"); BREAK; CASE "MACINTEL": CASE "X11": ALERT("YOU HAVE AN INTEL-BASED MAC"); BREAK; DEFAULT: ALERT("YOU HAVE " + NAVIGATOR.PLATFORM);} }

VARIABLE SCOPE VARIABLES DEFINED INSIDE A FUNCTION ARE ONLY KNOWN TO THAT FUNCTION, AND VARIABLES DEFINED OUTSIDE A FUNCTION ARE IN THE GLOBAL SCOPED AND ARE KNOWN EVERYWHERE. VAR MESSAGE = "HI"; FUNCTION SHOWMESSAGE() { VAR MESSAGE = "BYE"; ALERT(MESSAGE); } SHOWMESSAGE(); ALERT(MESSAGE); THE FIRST ALERT WILL SHOW THE MESSAGE VALUE LOCAL TO THE SHOWMESSAGE FUNCTION, THE SECOND ALERT WILL SHOW THE MESSAGE VALUE IN THE GLOBAL SCOPE.