UFCFY5-30-1Multimedia Studio Scripting for Interactive Media Further Structures for Branching and Looping.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
IM 215 Operators, Conditions and Loops. Review Nature of Javascript How to include Javascript on a page Declaring and assigning variables Commenting code.
Computer Science 1620 Loops.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 61 Flags A flag is a variable that keeps track of whether a certain situation has occurred. The data type most suited to flags is Boolean.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
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.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Week 8: Decisions Bryan Burlingame 21 October 2015.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
COMP Loop Statements Yi Hong May 21, 2015.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Follow up from lab See Magic8Ball.java Issues that you ran into.
CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must.
Slides by Evan Gallagher
Slides by Evan Gallagher
CSC111 Quick Revision.
Java Language Basics.
Revision Lecture
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
LOOPS.
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
TIPS: How to Be Successful
T. Jumana Abu Shmais – AOU - Riyadh
Coding Concepts (Basics)
SELECTIONS STATEMENTS
More on conditional statements
Problem 1 Given n, calculate 2n
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

UFCFY5-30-1Multimedia Studio Scripting for Interactive Media Further Structures for Branching and Looping

UFCFY5-30-1Multimedia Studio Agenda Additional control flow structures and loops The ‘switch’ branching structure The while do loop The for loop Boolean data type Formatting decimal output

UFCFY5-30-1Multimedia Studio The switch statement int month = 3; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; default: monthString = "Invalid month"; break; } System.out.println(monthString); }

UFCFY5-30-1Multimedia Studio Looping: while // loop until some condition is met while(aCondition) { // do something here // update loop condition }

UFCFY5-30-1Multimedia Studio int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++;// shorthand for count = count + 1 } Looping: while d

UFCFY5-30-1Multimedia Studio ‘while’ verses ‘do while’ int count = 11; while (count < 11) { // loop will not be processed System.out.println("Count is: " + count); count++;// shorthand for count = count + 1 } program continues here ignoring the loop

UFCFY5-30-1Multimedia Studio ‘while do’ verses ‘do while’ int count = 11; do { // loop will be processed once System.out.println("Count is: " + count); count++;// shorthand for count = count + 1 } while (count < 11) program continues here after going through the loop

UFCFY5-30-1Multimedia Studio The while do loop is a pre-condition loop structure, if the initial condition is not met the loop will not be processed The do while loop is a post-condition loop structure, the loop will always be processed at least once ‘while do’ verses ‘do while’

UFCFY5-30-1Multimedia Studio The for loop for(start value; end value ; increment){ // do something here }

UFCFY5-30-1Multimedia Studio The for loop for(int aNumber =1; aNumber < 6; aNumber ++){ System.out.println("Count is: " + aNumber); }

UFCFY5-30-1Multimedia Studio The for loop The output of the program would be: Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5

UFCFY5-30-1Multimedia Studio The for loop // change the end value for the loop for(int aNumber =1; aNumber <= 6; aNumber ++){ System.out.println("Count is: " + aNumber); }

UFCFY5-30-1Multimedia Studio The for loop The output of the program now would be: Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6

UFCFY5-30-1Multimedia Studio The for loop The for loop is processed for a pre-determined number of times Typically used where a collection of data is being counted or totalled, or searching a collection of data for a given item or value

UFCFY5-30-1Multimedia Studio New Data Type: Boolean The boolean data type has only two possible values: true and false Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that is precisely defined.

UFCFY5-30-1Multimedia Studio Additional Boolean Condition boolean guessed = false; // declare Boolean variable – set to false if (userGuess == numberToGuess){ System.out.println("Well Done!" + userName + " You guesed the number in " + tries + " tries."); guessed = true;// set Boolean to true } while(tries < 10 && guessed == false);// && reads AND // or while(tries < 10 && !guessed);// reads AND NOT guessed, (! means NOT)

UFCFY5-30-1Multimedia Studio Formatting Decimal Output Numeric values output from variables declared from decimal types e.g. float types will usually require some formatting to make output more readable and user-friendly Java has several methods to format decimal output e.g. printf System.out.printf("%.2f", val)// prints to 2 decimal places // PI to nine decimal places System.out.printf("%1.2f", ); //outputs 3.14 There are other methods available to format output of decimal values e.g. rounding a decimal value

UFCFY5-30-1Multimedia Studio Summary The flow of a program can be modified by using loops and branching statements The switch statement may branch to many outputs depending on the case of each condition The while do loop is a pre-condition loop will not be processed if the initial loop condition is not met The do while loop is a post-condition loop that will always be processed at least once The for loop is processed for a pre-determined number of times Its typically used for adding or counting collections of data and searching collections of data for values or named items Boolean data type boolean has values of true or false Use printf to format decimal output e.g. System.out.printf("%.2f", val)// prints to 2 decimal places

UFCFY5-30-1Multimedia Studio Code Design Activity Design a program to emulate the National Lottery Draw. The user will be prompted to enter the required number of values via the keyboard. The system will then choose a random selection of its own values. The system will compare the system values with the user values A score will be calculated based on the following matches together with a user message: 3 matches (You have won £20) 4 matches (You have won £100) 5 matches (You have won the Jackpot!)