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.

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

Review of Exercises from Chapter 17 Statistics, Spring
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.
MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Computer Science 1620 Programming & Problem Solving.
Making Decisions In Python
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
CIS101 Introduction to Computing Week 12 Spring 2004.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
THE BIG PICTURE. How does JavaScript interact with the browser?
General Programming Introduction to Computing Science and Programming I.
Chapter 2 - Algorithms and Design
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
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.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
A Simple Quiz: Ask User Functions. By Lana Dyck under the direction of Professor Susan Rodger Duke University June 2009, added Part 2 July 2011.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping with.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
22/11/ Selection If selection construct.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Student Pages
Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
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.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
GCSE Computing: Programming GCSE Programming Remembering Python.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Logical Operators.  Quiz  Let's look at the schedule  Logical Operators 2.
Check Boxes. 2 Check boxes  Image from:  HTML:  Each box gets it's own.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Learning Javascript From Mr Saem
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditionals.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Multi-Selection If-elsif Statement.  In fact, it’s everything in between  Yesterday we learned how to add a control statement that gave us two path.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
 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.
Logical Operators.  Quizzes!  Let's look at the schedule  Logical Operators 2.
BIT116: Scripting Lecture 05
IF statements.
The Selection Structure
Conditions and Ifs BIS1523 – Lecture 8.
Programming in JavaScript
Programming in JavaScript
BIT116: Scripting Radio Buttons.
BIT116: Scripting Lecture 6 Part 1
Making decisions with code
Software Development Techniques
How to allow the program to know when to stop a loop.
Presentation transcript:

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 to be "done except for maybe some small-ish errors"  JavaScript: Conditional Statements 2

3 Conditional Statements – Basic Syntax

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

5 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

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

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

8 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!"; } FILE: Conditional_Demonstration_1.html if (hour < 12) { show = "Good morning"; } else if ( hour < 18 ) { show = "Good afternoon"; } else if ( hour < 22 ) { show = "Good evening"; } else { show = "Goodnight!"; }

The objective is to remind yourself what to type in order to get an if, if/else, or multiway if…else to work 9

10 Conditional Statements – Effective Usage

 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); 11 // Store time into variable var hour = $("#time"); // ALWAYS tell the user what their choice is: $("#requiredOutput").html("Your time is " + hour); // OPTIONALLY tell them 'good day' $("#optionalOutput").html("Good day!"); // Is it before 6pm? if (hour < 18) YES NO

 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); 12 // Store time into variable var hour = $("#time"); // ALWAYS tell the user what their choice is: $("#requiredOutput").html("Your time is " + hour); // THEN tell them 'good day' $("#additionalOutput").html("Good day!"); // Is it before 6pm? if (hour < 18) YES NO // OTHERWISE Tell them 'good evening' $("#additionalOutput").html("Good evening!");

 Think of an if 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); 13 // ALWAYS tell the user what their choice is: $("#requiredOutput").html("Your time is " + hour); // Tell them 'good morning' Show = "Good morning"; // What is the hour? hour< 12 // Tell them 'good day' show = "Good day!"; 12 ≤ h< 18 // Tell them 'good evening' show = "Good evening!"; 18 ≤ h< 22 // Tell them 'goodnight' show = "Good night!"; hour≥ 22

1. List the steps that must be done in order to solve the problem 2. 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 3. 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 4. Proofread your solution!!! 5. Code it up 6. Test it out 1. Figure out the answer yourself, by hand, then compare to the program 14

 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. 15

 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: 1. First, get input 2. 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. 3. Display results on page 4. Tell'em if it's freezing 5. Tell'em if it's boiling  Try writing this in a flowchart format  Try writing this in pseudocode format  (I'll do this in OneNote, so check the after-class videos to see these demonstrated 16

 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? 17 FILE: F2C.html

 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 18 FILE: F2C_IfElse.html

19

 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. 20 Numeric GradeLetter Grade >100Error! 90 – 100A 80 – 90B 70 – 80C 60 – 70D 0 – 60F < 0Error!

 List steps: 1. First, get input 2. If the grade is negative, display an error and immediately exit/stop/halt/etc. 3. 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 ) 4. 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) 21 Useful idea: 'Peeling off' errors 'Peeling away' cases

 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? 22 FILE: GradeFeedback.html

 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) 23

24