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

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Changing Control.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
True or false A variable of type char can hold the value 301. ( F )
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
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.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
Python Control of Flow.
Introduction to Python
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
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.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Assignment 6.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Code Grammar. Syntax A set of rules that defines the combination of symbols and expressions.
Algorithm Design.
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:
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
 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.
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.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.
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
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Expressions & Control Flow CSE 120 Winter 2018
Data Types, Identifiers, and Expressions
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Module 2: Understanding C# Language Fundamentals
Announcements Mid-Term Exam!! Quiz 5 Again + Quiz 6 Exercise 5
Testing and Repetition
Computer Science Core Concepts
Expressions and Assignment
Expressions.
Variables, Assignments Testing + Iteration
Relational Operators.
Lawrence Snyder University of Washington, Seattle
Javascript Chapter 19 and 20 5/3/2019.
IAT 800 Foundations of Computational Art and Design
Announcements: Questions: Names on the board? Why?
Announcements Survey feedback: summary Quiz: HW 7:
Expressions & Conditionals CSE 120 Winter 2019
Presentation transcript:

Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004

10/23/2015© 2010 Larry Snyder, CSE2 Just Do It

 Rule: Assignment always moves information from right to left, as we used in Lab 5 incDec = - incDec;  Rule: Always evaluate (compute) the right side of the equals, then assign the result to the name on the left side … 10/23/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 10/23/2015© 2010 Larry Snyder, CSE4

 Fact – they pretty much work like a calculator  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 10/23/2015© 2010 Larry Snyder, CSE5

10/23/2015© 2010 Larry Snyder, CSE6 Just Do It  Using the program, it’s possible to Illustrate the various operators

 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 10/23/2015© 2010 Larry Snyder, CSE7 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 10/23/2015© 2010 Larry Snyder, CSE8 … 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); } 10/23/2015© 2010 Larry Snyder, CSE9

 A for-loop has several parts, all required … for (int j = 0; j < 10; j = j + 1) { } 10/23/2015© 2010 Larry Snyder, CSE10 keyword next, open paren starting value continuation test increment next, close paren next, open brace last, close brace Result: 10 executions 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)) { fill(chosen_tint); } 10/23/2015© 2010 Larry Snyder, CSE11

 An If-statement has a standard form if ( bmi >18.5 && bmi<=24.9 ) { fill(0, 255, 0); } 10/23/2015© 2010 Larry Snyder, CSE12 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 } 10/23/2015© 2010 Larry Snyder, CSE13

 The standard form may now be obvious if (year%4 == 0) { feb_days = 29; } else { feb_days = 28; } 10/23/2015© 2010 Larry Snyder, CSE14 finally, close brace The result 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 10/23/2015© 2010 Larry Snyder, CSE15 Just Do It