BIT116: Scripting Lecture 6 Part 1

Slides:



Advertisements
Similar presentations
Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
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.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
LOGO Conditional Statements & Loops in JavaScript CHAPTER 10 Eastern Mediterranean University School of Computing and Technology Department of Information.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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 cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
Learning Javascript From Mr Saem
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
 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.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
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 this.
JavaScript – If/Else contd
BIT116: Scripting Lecture 05
JavaScript Controlling the flow of your programs with ‘if’ statements
CHAPTER 4 DECISIONS & LOOPS
CHAPTER 10 JAVA SCRIPT.
Chapter 5: Structured Programming
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
JavaScript Loops.
JavaScript: Control Statements I
Chapter 6: Conditional Statements and Loops
JavaScript: Control Statements.
JavaScript: Control Statements I
ITM 352 Flow-Control: Loops
Arrays, For loop While loop Do while loop
During the last lecture we had a discussion on Data Types, Variables & Operators
Programming in JavaScript
Objectives In this chapter, you will:
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Conditional Statements & Loops in JavaScript
Control Structures Part 1
Programming in JavaScript
JavaScript: Control Statements II
Instructor: Craig Duckett
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CSE 206 Course Review.
Software Development Techniques
Presentation transcript:

BIT116: Scripting Lecture 6 Part 1 Instructor: Craig Duckett cduckett@cascadia.edu Thursday, July 23rd, 2015 Conditional Statements & Loops

ASSIGNMENT ANNOUNCEMENTS REMINDER: Submit via StudentTracker. If you haven't already done so, you will need to set up a StudentTracker account in order to upload your Assignment (ZIPPED up in a single file) Assignment 1 due on Lecture 7, NEXT Tuesday, July 28th , by midnight. Assignment 1 Revision due on Lecture 10, Thursday, August 6th , by midnight. Assignment 2 due on Lecture 11, Tuesday, August 11th , by midnight. Assignment 2 Revision due on Lecture 13, Tuesday, August 18th , by midnight. Assignment 3 due on Lecture 14, Thursday, August 20th , by midnight. Assignment 3 Revison due on Lecture 16, Thursday, August 27th , by midnight.

Conditional Statements

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

The if Statement (Example) if (hour < 18) {    show = "Good day"; } http://faculty.cascadia.edu/cduckett/bit116/Lecture_08/if.html

The if…else Statement (Example) if (hour < 18) { show = "Good day"; } else show = "Good evening"; http://faculty.cascadia.edu/cduckett/bit116/Lecture_08/ifelse.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!"; http://faculty.cascadia.edu/cduckett/bit116/Lecture_08/ifelseifelse.html

The switch Statement The switch statement is used to perform different action based on different conditions. Use the switch statement to select one of many blocks of code to be executed.

var day = new Date().getDay(); // Note that Sunday = 0, Monday = 1, Tuesday = 2, etc. switch (day) { case 0: currentDay = "Today is Sunday"; break; case 1: currentDay = "Today is Monday"; case 2: currentDay = "Today is Tuesday"; case 3: currentDay = "Today is Wednesday"; case 4: currentDay = "Today is Thursday"; case 5: currentDay = "TGIF!"; case 6: currentDay = "Today is Saturday"; } Example: http://faculty.cascadia.edu/cduckett/bit116/Lecture_08/switch1.html

var day = new Date().getDay(); switch (day) { case 0: currentDay = "Today is Sunday"; break; case 6: currentDay = "Today is Saturday"; default: // Use default to specify what to do if there's no match    currentDay = "Looking forward to a spectacular Weekend!"; } Example: http://faculty.cascadia.edu/cduckett/bit116/Lecture_08/switch2.html

Loops

What is a Loop? A loop is a block of code that allows you to repeat a section of code a certain number of times, perhaps changing certain variable values each time the code is executed. By doing this, you are often able to shorten certain tasks into a few lines of code, rather than writing the same line over and over again within the script and tiring your fingers. Loops are useful because they allow you to repeat lines of code without retyping them or using cut and paste in your text editor. This not only saves you the time and trouble of repeatedly typing the same lines of code, but also avoids typing errors in the repeated lines. You are also able to change one or more variable values each time the browser passes through the loop, which again saves you the time and trouble of typing a line that is only slightly different than the previous line.

The while Loop The while loop loops through a block of code as long as a specified condition is true. while(expression is true) {   Statement(s)to be executed } Example: http://faculty.cascadia.edu/cduckett/bit116/Lecture_08/while1.html

The do-while Loop The do...while loop is similar to the while loop except that the condition check happens at the end of the loop. This means that the loop will always be executed at least once, even if the condition is false. do{ Statement(s) to be executed; } while (expression is true); Example: http://faculty.cascadia.edu/cduckett/bit116/Lecture_08/dowhile.html

The for Loop The for loop is the most compact form of looping and includes the following three important parts: The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins. The test statement which will test if the given condition is true or false. If condition is true then code given inside the loop will be executed otherwise loop will come out. The iteration statement where you can increase (increment) or decrease (decrement) your counter. You can put all the three parts in a single line separated by a semicolon. for (initialization; test condition; iteration statement) { Statement(s) to be executed if test condition is true } http://faculty.cascadia.edu/cduckett/bit116/Lecture_08/for.html

The break Statement You have already seen the break statement used in an earlier slide of this lecture. It was used to "jump out" of a switch() statement. The break statement can also be used to jump out of a loop. The break statement breaks the loop and continues executing the code after the loop (if any): for (i = 0; i < 10; i++) { if (i == 3) break; } x += "The number is " + i + "<br>"; http://faculty.cascadia.edu/cduckett/bit116/Lecture_08/break.html

The continue Statement The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop. This example skips the value of 3: for (i = 0; i < 10; i++) { if (i == 3) continue; } x += "The number is " + i + "<br>"; http://faculty.cascadia.edu/cduckett/bit116/Lecture_08/continue.html

ICE 06 Part 1 Please begin working on the LECTURE 6 In-Class Exercises. When you have completed your ICE, call the Instructor over to verify your work. If you have questions about your work, please call over the Instructor to clarify. Once you have completed your ICEs, you are free to go for the day. If you don't finish your ICEs by the end of class, you will still get full credit for actively doing the work, but it is greatly recommended you finish any outstanding ICEs outside of class.