Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Changing Control.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

RAPTOR Syntax and Semantics By Lt Col Schorsch
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
True or false A variable of type char can hold the value 301. ( F )
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
IAT 800 Foundations of Computational Art and Design Lecture 2.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fundamental Concepts Expressed in JavaScript Get with the Program lawrence.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
110-D1 Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need.
Chapter 9 Interactive Multimedia Authoring with Flash - Introduction to Programming “Computers and Creativity” Richard D. Webster, COSC 109 Instructor.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
CPS120 Introduction to Computer Science Iteration (Looping)
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
CPS120: Introduction to Computer Science Decision Making in Programs.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Assignment 6.
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,
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Algorithms Writing instructions in the order they should execute.
Lesson Two: Everything You Need to Know
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
CPS120 Introduction to Computer Science Iteration (Looping)
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
LCC 6310 Computation as an Expressive Medium Lecture 2.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Drawing pictures … It’s not art, it’s fun.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 17 JavaScript.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
Controlling Program Flow with Decision Structures.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
1 CS100J September 27, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Control flow Ruth Anderson UW CSE 160 Winter
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
Expressions & Control Flow CSE 120 Spring 2017
Overview: Programming Concepts
Data Types, Identifiers, and Expressions
Boolean Expressions and If
Data Types, Identifiers, and Expressions
While Loops and If-Else Structures
Announcements Mid-Term Exam!! Quiz 5 Again + Quiz 6 Exercise 5
Testing and Repetition
Computing Fundamentals
Flow of Control.
Variables, Assignments Testing + Iteration
The structure of programming
Lawrence Snyder University of Washington, Seattle
Javascript Chapter 19 and 20 5/3/2019.
Announcements How to save your work? Quiz solution 5/5/2019
IAT 800 Foundations of Computational Art and Design
Announcements: Questions: Names on the board? Why?
Basic Processing … Drawing pictures … It’s not art, it’s fun
LCC 6310 Computation as an Expressive Medium
Expressions & Conditionals CSE 120 Winter 2019
Presentation transcript:

Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Changing Control

 We saw how to change the color of the ball and its direction with a mouse & key clicks  Recall … 5/13/2015© 2010 Larry Snyder, CSE2

 Rule: Assignment always moves information from right to left, as in incDec = - incDec;  Rule: Always evaluate (compute) the right side, then assign the result to the name on the left side … 5/13/2015© 2010 Larry Snyder, CSE3

 Facts about expressions  Expressions are formulas using: + - * / % || ! && == = > !=  Operators can only be used with certain data types and their result is a certain data type  Putting in parentheses is OK, and it’s smart  Rules about expressions  Expressions can usually go where variables can go 5/13/2015© 2010 Larry Snyder, CSE4

 Facts  Expressions are formulas: a+b points*wgt (year%4 == 0) 7 != 4 (age>12) && (age<20)  “Need & give data types” ▪ + - * / % > want numbers; ▪ && ! || want logical (Boolean) values ▪ == and != want arguments to be the same type  “Parentheses are good”: (a * b) + c is the same as a*b+c, but easier to read 5/13/2015© 2010 Larry Snyder, CSE5

 a%b (read, “a mod b”) is the amount left after “b divides into a evenly”  Examples:  0 % 3 is 0  1 % 3 is 1  2 % 3 is 2  3 % 3 is 0  4 % 3 is 1  5 % 3 is 2  6 % 3 is 0 5/13/2015© 2010 Larry Snyder, CSE6 Leap Year: year is a leap year if year%4 == 0 Even: a number n is even if n%2 == 0 Asian Zodiac: year1 and year2 are the same sign if year1%12 == year2%12

 As numbers get larger, mod will cause them to “drop to 0” … this is a Ninja move 5/13/2015© 2010 Larry Snyder, CSE7 … ra == 0 ra == 149 Just Do It

 Repeating commands is a powerful way to use a computer … we could repeat them, but all programming systems have a way to loop:  Lightbot 2.0 used recursion, a function calling itself  Symbolic Lightbot prefixed a number, 2:Step  Processing (and other modern languages) use a for loop: for (i = 0; i < 5; i = i + 1) { rect(10+20*i,10,10, 10); } 5/13/2015© 2010 Larry Snyder, CSE8

 A for loop has several parts, all required … for (int j = 0; j < 10; j = j + 1) { } 5/13/2015© 2010 Larry Snyder, CSE9 keyword next, open paren starting value continuation test increment next, close paren next, open brace last, close brace The result of this statement is 10 copies of the stuff to be repeated Just Do It

 The instructions of a program are executed sequentially, one after another … sometimes we want to skip some: Say “Hello” to the If  If also has a required form if (year%4 == 0) { ; } if (chosen_tint != color(0,0,255)) { //No TRUE blue! fill(chosen_tint); } 5/13/2015© 2010 Larry Snyder, CSE10

 An If-statement has a standard form if ( bmi >18.5 && bmi<=24.9 ) { fill(0, 255, 0); } 5/13/2015© 2010 Larry Snyder, CSE11 keyword next, open paren boolean expression next, close paren next, open brace last, close brace The result is that if bmi is in range the fill color is green (indicating OK)

 What happens if we want to do something else if the condition is false? What else? else!  The else statement must follow an if … if (year%4 == 0) { ; //Then Clause } else { ; //Else Clause } 5/13/2015© 2010 Larry Snyder, CSE12

 The standard form may now be obvious if (year%4 == 0) { feb_days = 29; } else { feb_days = 28; } 5/13/2015© 2010 Larry Snyder, CSE13 finally, close brace The result is sets the number of days in February based on leap year keyword open brace, immediately after “else” Else must follow if because it does the test

 Let’s go to processing for an example 5/13/2015© 2010 Larry Snyder, CSE14 Just Do It

 Naturally, programs are given sequentially, the declarations at the top  Braces { } are statement groupers … they make a sequence of statements into one thing, like the “true clause of an If-statement”  All statements must end with a semicolon EXCEPT the grouping braces … they don’t end with a semicolon (OK, it’s a rare inconsistency about computer languages!)  Generally white space doesn’t matter; be neat! 5/13/2015© 2010 Larry Snyder, CSE15