Running JavaScript Chapter 18.

Slides:



Advertisements
Similar presentations
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Advertisements

Python Programming Language
1 BUILDING JAVA PROGRAMS CHAPTER 4 CONDITIONAL EXECUTION.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
The If/Else Statement, Boolean Flags, and Menus Page 180
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
CS305j Introduction to Computing Conditional Execution 1 Topic 12 Conditional Execution "We flew down weekly to meet with IBM, but they thought the way.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Flow of Control Part 1: Selection
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 11 Conditional.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.
Decision Making - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 10/27/20151.
Building Java Programs
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 if / else statements. 2 Conditionals “If you eat your vegetables, then you can have dessert.” “If you do your homework, then you may go outside to play,
Conditional Statements © Copyright 2014, Fred McClurg All Rights Reserved.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
Computer Science 1000 LOGO II. Boolean Expressions like Excel and Scratch, LOGO supports three Boolean operators less than (
Copyright 2008 by Pearson Education 1 The if statement Executes a block of statements only if a test is true if ( test ) { statement ;... statement ; }
Copyright 2010 by Pearson Education The if/else statement reading: 4.1, 4.6.
CSc 110, Autumn 2016 Lecture 9: input ; if/else Adapted from slides by Marty Stepp and Stuart Reges.
1 Building Java Programs Chapter 4: Conditional Execution These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted,
Building Java Programs
Building Java Programs
Building Java Programs Chapter 4
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
If, else, elif.
Lecture 4: Conditionals
Building Java Programs
CSc 110, Spring 2017 Lecture 8: input; if/else
Making Decisions in a Program
Building Java Programs
Executes a block of statements only if a test is true
CSc 110, Autumn 2016 Lecture 9: input; if/else
Building Java Programs
Building Java Programs
Selection Statements.
Summary Two basic concepts: variables and assignments Basic types:
Building Java Programs
Microsoft Visual Basic 2005: Reloaded Second Edition
Lecture 6: Conditionals AP Computer Science Principles
Building Java Programs
Building Java Programs
Building Java Programs
Programming In Lesson 4.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
Building Java Programs Chapter 4
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Running JavaScript Chapter 18

Concatenation: Operating On Strings string concatenation: using the + operator between a string and another value to make a longer string Examples: 'hello' + 42 is 'hello42' 1 + "abc" + 2 is "1abc2" 'abc' + 1 + 2 is 'abc12' 1 + 2 + "abc" is "3abc" "1" + 1 is "11" Expressions need not only manipulate numbers.

Popup Box Alert box syntax: alert(<expression>); Examples: alert("Hello, world!");

What Is In The Variable? Use alert boxes to reveal the value of variables. var x = 100; alert(x); Use string concatenation to make alert messages even more useful. alert("x = [" + x + "]"); better!

Linking JavaScript File To XHTML File <head> <title>...</title> <link rel="stylesheet" type="text/css" href="style.css" /> <script src="filename.js" type="text/javascript"></script> </head> Copy the type attribute and its corresponding value verbatim Use the src attribute to specify the location of a JavaScript file Path location may be absolute or relative

Conditionals

Conditionals "If button is clicked, then close the popup box." "If Mario touches the flag, then end the level." "If a correct password has been entered, then reveal the top secret documents, otherwise contact the FBI." "If the coin collected brings the total to one hundred, make 1-up sound, otherwise make regular coin collection sound."

The if Statement if statement: a control structure that executes a block of statements only if a certain condition is true General syntax: if (<test>) { <statement(s)> ; } Example: var gpa = 3.25; if (gpa >= 3.0) { alert("Good job! Have a cookie.");

if Statement Flow Chart

The if/else Statement if/else statement: A control structure that executes one block of statements if a certain condition is true, and a second block of statements if it is false. We refer to each block as a branch. General syntax: if (<test>) { <statement(s)> ; } else { } Example: var gpa = 3.25; if (gpa >= 3.0) { alert("Good job! Have a cookie."); alert("No cookie for you!");

if/else Statement Flow Chart

Relational Expressions The <test> used in an if or if/else statement must evaluate to a Boolean value (true or false). Relational expressions evaluate to Boolean values and use the following relational operators: Operator Meaning Example Value == equals 1 + 1 == 2 true != does not equal 3.2 != 2.5 < less than 10 < 5 false > greater than 10 > 5 <= less than or equal to 126 <= 100 >= greater than or equal to 5.0 >= 5.0

Evaluating Relational Expressions Relational operators have lower precedence than math operators. 5 * 7 >= 3 + 5 * (7 - 1) 5 * 7 >= 3 + 5 * 6 35 >= 3 + 30 35 >= 33 true Relational operators should not be "chained" as they can in algebra. WARNING! JavaScript will NOT complain if you do so and you may get unexpected results. 2 <= 1 <= 10 false <= 10 ???

Errors In Coding Many students new to if/else write code like this: var percent = 85; if (percent >= 90) { alert("You got an A!"); } if (percent >= 80) { alert("You got a B!"); if (percent >= 70) { alert("You got a C!"); if (percent >= 60) { alert("You got a D!"); } else { alert("You got an F!"); What will happen? What’s the problem? You may get too many popup boxes. Try it out!

Nested if/else Statements Nested if/else statement: A chain of if/else that can select between many different outcomes based on several tests. General syntax: if (<test>) { <statement(s)> ; } else if (<test>) { } else { } Example: if (number > 0) { alert("Positive"); } else if (number < 0) { alert("Negative"); alert("Zero");

Nested if/else Variations A nested if/else can end with an if or an else. If it ends with else, one of the branches must be taken. If it ends with if, the program might not execute any branch. if (<test>) { <statement(s)>; } else if (<test>) { } else { } if (<test>) { <statement(s)>; } else if (<test>) { } else if (<test>) { }

Nested if/else Flow Chart if (<test>) { <statement(s)>; } else if (<test>) { } else { }

Nested if/else if Flow Chart if (<test>) { <statement(s)>; } else if (<test>) { } else if (<test>) { }

Nested if/else Variations if (place == 1) { alert("You win the gold medal!"); } else if (place == 2) { alert("You win a silver medal!"); } else if (place == 3) { alert("You earned a bronze medal."); } Are there any cases where this code will not print a message? Yes, if the place variable is not 1, 2, or 3. How could we modify it to print a message to non-medalists? Add an else clause.

Sequential if Flow Chart if (<test>) { <statement(s)>; }

Summary: if/else Structures Choose exactly 1 set of statements if (<test>) { <statement(s)>; } else if (<test>) { } else { } Choose 0, 1, or more set of statements Choose 0 or 1 set of statements

Which if/else Construct To Use? Reading the user's GPA and printing whether the student is on the dean's list (3.8 to 4.0) or honor roll (3.5 to 3.7) Printing whether a number is even or odd Printing whether a user is lower-class, middle-class, or upper-class based on their income Determining whether a number is divisible by 2, 3, and/or 5 Printing a user's grade of A, B, C, D, or F based on their percentage in the course

Which if/else Construct To Use? Reading the user's GPA and printing whether the student is on the dean's list (3.8 to 4.0) or honor roll (3.5 to 3.7) if / else if Printing whether a number is even or odd if / else Printing whether a user is lower-class, middle-class, or upper-class based on their income if / else if / else Determining whether a number is divisible by 2, 3, and/or 5 if / if / if Printing a user's grade of A, B, C, D, or F based on their percentage in the course if / else if / else if / else if / else

That Thing Called Style As with HTML, you are required to indent your code properly. Indent code within opening and closing curly braces. You should spend time on thinking or coding. You should NOT be wasting time looking for that missing closing brace. So code with style!