BIT116: Scripting Lecture 05

Slides:



Advertisements
Similar presentations
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Advertisements

The If/Else Statement, Boolean Flags, and Menus Page 180
Moodle (Course Management Systems). Assignments 1 Assignments are a refreshingly simple method for collecting student work. They are a simple and flexible.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
HOMEWORK REVIEW This is an if else statement layout if (condition) { code to be executed if condition is true; } else { code to be executed if condition.
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.
USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Conditional Statements.  Quiz  Hand in your jQuery exercises from last lecture  They don't have to be 100% perfect to get full credit  They do have.
Loops.  (No Quiz)  Hand in Assignment #1  Last chance for Q+A on the midterm  Loops 2.
Student Pages
Radio Buttons.  Quiz  Radio Buttons  Check boxes 2.
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
Logical Operators.  Quiz  Let's look at the schedule  Logical Operators 2.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Logical Operators.  Quizzes!  Let's look at the schedule  Logical Operators 2.
All materials copyright UMBC unless otherwise noted CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking.
JavaScript: Conditionals contd.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
JavaScript Controlling the flow of your programs with ‘if’ statements
Precedence Operators Error Types
Introduction to Computing Science and Programming I
CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking Prof. Katherine Gibson Based on slides by Shawn Lupoli and Max Morawski at UMBC.
CHAPTER 10 JAVA SCRIPT.
Whatcha doin'? Aims: To start using Python. To understand loops.
BIT116: Scripting Lecture 06
BIT116: Scripting Loops.
Introduction to Python
Loops BIS1523 – Lecture 10.
Programming Mehdi Bukhari.
IF statements.
CS1371 Introduction to Computing for Engineers
The Selection Structure
August 30 Intro to CS.
Programming 101 Programming for non-programmers.
Form Validation and AJAX
If, else, elif.
Intro to PHP & Variables
Temperature.
Programming – Touch Sensors
Cookies BIS1523 – Lecture 23.
MIS 3200 – Unit 4 Complex Conditional Statements else if switch.
Conditional Statements
Creativity in Algorithms
Selection CIS 40 – Introduction to Programming in Python
Learning to Program in Python
Conditions and Ifs BIS1523 – Lecture 8.
Number and String Operations
Programming in JavaScript
If selection construct
CSCE 489- Problem Solving Programming Strategies Spring 2018
“last minute” strategies
If selection construct
Due Next Monday Assignment 1 Start early
Algorithm and Ambiguity
Chapter 5: Control Structure
Python programming exercise
Programming in JavaScript
CST-115 Introduction to Computer Programming
Using Decision Structures
BIT116: Scripting Radio Buttons.
BIT116: Scripting Lecture 6 Part 1
Making decisions with code
Unit 3: Variables in Java
Chapter 2 - Algorithms and Design
creating a ecosystems model in net logo
CSCE 206 Lab Structured Programming in C
Asking Questions in BYOB Scratch.
Software Development Techniques
Retrieving information from forms
Presentation transcript:

BIT116: Scripting Lecture 05 Conditional Statements

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

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

Conditional Statements – Basic Syntax

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!"

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

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

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

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

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

Conditional Statements – Effective Usage

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);

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);

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);

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

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.

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

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

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

Do Exercise #2

Example: Grade Feedback Problem specification: Create a page that convert a 0- 100 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

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

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

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)

Do Exercise #3