Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIT116: Scripting Lecture 05

Similar presentations


Presentation on theme: "BIT116: Scripting Lecture 05"— Presentation transcript:

1 BIT116: Scripting Lecture 05
Conditional Statements

2 Today Quiz JavaScript: Conditional Statements
Today’s quiz is on debugging Next lecture we’re going to start with ‘fill in blank boxes’ quizzes’ I’ve got a ‘sample quiz’ up front: it doesn’t count for points, but it’ll be a good example of what to expect going forwards JavaScript: Conditional Statements

3 Due Today Assignment 1 If you’re having trouble:
You can submit a 1 page essay about what you tried to do, how it worked out, and what you can do differently in the future in order to get an extra revision Focus on constructive advice about stuff you control. Include the essay (.DOCX or .PDF) in your .ZIP

4 Conditional Statements – Basic Syntax

5 What is a Conditional Statement?
A conditional statement is a statement that you can use to execute a bit of code based on a condition or to do something else if that condition is not met. You can think of a conditional statement as being a little like cause and effect. Here's an example: "If a variable named myMoney is greater than 1000, send an alert that says my finances are OK. Otherwise, send an alert saying I need more money!"

6 The if , if/else , if/else if/else Statements
if statement: use this statement to execute some code once only if a specified condition is true if...else statement: use this statement to execute some code if the condition is true and another code if the condition is false if...else if....else statement: use this statement to select one of many blocks of code to be executed

7 FILE: Conditional_Demonstration_1.html
The if Statement (Example) if (hour < 18) {    show = "Good day"; } FILE: Conditional_Demonstration_1.html

8 FILE: Conditional_Demonstration_1.html
The if…else Statement (Example) if (hour < 18) { show = "Good day"; } else show = "Good evening"; FILE: Conditional_Demonstration_1.html

9 FILE: Conditional_Demonstration_1.html
The if…else if…else Statement (Example) if (hour < 12) { show = "Good morning"; } else if ( hour < 18 ) show = "Good afternoon"; else if ( hour < 22 ) show = "Good evening"; else show = "Goodnight!"; if (hour < 12) { show = "Good morning"; } else if ( hour < 18 ) show = "Good afternoon"; if ( hour < 22 ) show = "Good evening"; show = "Goodnight!"; FILE: Conditional_Demonstration_1.html

10 Do Exercise #1 The objective is to remind yourself what to type in order to get an if, if/else, or multiway if…else to work

11 Conditional Statements – Effective Usage

12 If:Optional Extra YES NO
Think of an if as a way of asking if the program should optionally do something extra, then resume the program var hour = $("#time"); if (hour < 18) {    $("#optionalOutput").html( "Good day!"); } $("#requiredOutput").html("Your time is " + hour); // Store time into variable var hour = $("#time"); // Is it before 6pm? if (hour < 18) YES // OPTIONALLY tell them 'good day' $("#optionalOutput").html("Good day!"); NO // ALWAYS tell the user what their choice is: $("#requiredOutput").html("Your time is " + hour);

13 IfElse: Either/Or NO YES If/Else: Either do A or else do B
One and only one will happen! var hour = $("#time"); if (hour < 18) {    $("#additionalOutput").html("Good day!"); } else {    $("#additionalOutput").html("Good evening!"); } $("#requiredOutput"). html("Your time is " + hour); // Store time into variable var hour = $("#time"); NO // Is it before 6pm? if (hour < 18) YES // THEN tell them 'good day' $("#additionalOutput").html("Good day!"); // OTHERWISE Tell them 'good evening' $("#additionalOutput").html("Good evening!"); // ALWAYS tell the user what their choice is: $("#requiredOutput").html("Your time is " + hour);

14 Multiway If..Else: Choose One
Think of a multiway if…else as a way of asking if the program should exactly one of a several possible choices if (hour < 12) { show = "Good morning"; } else if ( hour < 18 ) { show = "Good afternoon"; } else if ( hour < 22 ) { show = "Good evening"; } else { show = "Goodnight!"; } $("#requiredOutput").html("Your time is " + hour); // What is the hour? hour< 12 // Tell them 'good morning' Show = "Good morning"; 12 ≤ h< 18 // Tell them 'good day' show = "Good day!"; 18 ≤ h< 22 // Tell them 'good evening' show = "Good evening!"; hour≥ 22 // Tell them 'goodnight' show = "Good night!"; // ALWAYS tell the user what their choice is: $("#requiredOutput").html("Your time is " + hour);

15 How To Solve Coding Problems
List the steps that must be done in order to solve the problem Identify those steps that are OPTIONAL, EITHER-OR, or CHOOSE ONE (FROM MANY) These will be your if, if…else, and multiway if…else's Sketch out you’re your solution You can use flowcharts, like we did on the prior slides You can use pseudocode, which is easier to write into comments in your source code file Proofread your solution!!! Code it up Test it out Figure out the answer yourself, by hand, then compare to the program

16 Example: Fahrenheit  Celsius, with bonus commentary!
Problem specification: Create a page that will convert from Fahrenheit to Celsius. In addition, if the temperature is below freezing you should display a message formatted to look 'chilly'. Similarly, display a 'toasty' looking message if the temperature is above boiling.

17 Example: Fahrenheit  Celsius,
I'll assume that you can set up the HTML and basic jQuery stuff on your own. We're just focus on the problem-specific stuff List steps: First, get input Next, run through formula NOTE: You will probably need to look this up. Looking it up is something you need to do, not something that the program does. Display results on page Tell'em if it's freezing Tell'em if it's boiling Try writing this in a flowchart format Try writing this in pseudocode format

18 Example: Fahrenheit  Celsius,
Once you've thought the problem through (listing, flowchart/pseudocode), then code it up! Test Remember: Calculate the answer yourself, then compare it to your program Try several temperatures between freezing & boiling If you don't remember it off-hand then look up for yourself what these are in Fahrenheit Try freezing exactly, then one degree above and one degree below Does the 'chilly' message show up correctly? Try boiling exactly, then one degree above and one degree below Does the 'toasty' message show up correctly? Are there any other problems with this? FILE: F2C.html

19 Close, but not quite  If we put in 300 & click we get the 'hot' message. If we then change it to 3 & click we see BOTH messages We'll fix this with an if…else FILE: F2C_IfElse.html

20 Do Exercise #2

21 Example: Grade Feedback
Problem specification: Create a page that convert a numeric grade into an 'A', 'B', 'C', 'D', or 'F' grade, based on the table to the right. If a number greater than 100 or less than zero is used then display an error message on the page. Numeric Grade Letter Grade >100 Error! 90 – 100 A 80 – 90 B 70 – 80 C 60 – 70 D 0 – 60 F < 0

22 Example: Grade feedback
List steps: First, get input If the grade is negative, display an error and immediately exit/stop/halt/etc. If the grade is >100, display an error and immediately stop. (If we make it past this step then the input must be ok) (mostly – we're assuming that it's a number  ) Use a multiway if/else to figure out which letter grade they're getting Let's try writing this in a flowchart format Let's try writing this in pseudocode format (I'll do this in OneNote, so check the after-class videos to see these demonstrated) Useful idea: 'Peeling off' errors 'Peeling away' cases

23 Example: Grade feedback
Once you've thought the problem through (listing, flowchart/pseudocode), then code it up! Test Remember: Calculate the answer yourself, then compare it to your program Try it with 95, 90, 100 Try it with 85, 80, 90 Etc, etc Try it with 101, 110, 200 Try it with 0, -1, -10 Are there any other problems with this? FILE: GradeFeedback.html

24 Ideas / Interests for in-class exercises?
A research paper that I read said that it's good to give new programmers a wider choice of exercises So instead of "do #1, then #2, then #3"… Instead tell people "Here's 6, pick 3" This isn't always appropriate… "Let's make sure you can type in the syntax" Mechanical things like "ID different regions as CSS/JS" … and I'm not 100% confident I can brainstorm a zillion exercises for each class, but I'm gonna try it What ideas/interests do you have that might make for interesting problems involving if or if/else statements? I don't guarantee I'll use them, but it'll help Think about this during class, and we'll brainstorm a list towards the end (or else at the start of next class)

25 Do Exercise #3


Download ppt "BIT116: Scripting Lecture 05"

Similar presentations


Ads by Google