PROGRAMMING. We create identity

Size: px
Start display at page:

Download "PROGRAMMING. We create identity"

Transcription

1 2 PROGRAMMING We create identity

2 TOPICS COVERED Getting Started Setup and draw Variables mousex, mousey other system variables variable declaration types Arithmetic Predefined methods abs, max, delay Topics Look for examples Look at manual Programming as communication Style Naming Comments Constants

3 EXAMPLES Tutorial 1.15 Executed top to bottom. Assignment is not equality Types matter

4 VARIABLE DECLARATION int numsegments; type name use a nice meaningful one

5 VARIABLE DECLARATION int numsegments = 5; intitialisation type name use a nice meaningful one

6 ARITHMETIC x = (3 + x)*(7 + y) + z; y = abs(x - z) % 50; z = (x y)/2; some operations behave differently for different types, e.g. integer division not all operations defined for all types computed top to bottom on left-hand side of = is the variable that gets a new value on the right-hand side of = the expression to compute the new value

7 SCOPE float topx; float topy; void setup(){ topx = width/2; topy = height/2; global variables for shared or persistent data void draw(){ float bottomx; float bottomy; bottomx = topx + width/10; bottomy = topy + height/10; curly brackets define scopes/blocks topx = (topy + 1)% 200; topy = (topy + 1) % 100; local variables for transient data or values

8 EXAMPLE Tutorial 1.16 Global vs local scope Use Global To store data between iterations (persistence) To share data between methods (Note: There are other ways to share) Use local To store values that are used within one method Modern/good programs try to minimize scope

9 COMMENTS Comments can be written in two styles: Single line: float canvolume = 0.355; // Liters in a 12-ounce can The compiler ignores everything after // to the end of line Multiline for longer comments: /* */ This program computes the volume (in liters) of a six-pack of soda cans.

10 CONSTANTS In Processing you can declare CONSTANTS. static final int ITEM_SIZE = 20; Needs initial value Common in Java. Rarely done in Processing. A good idea, also in Processing It tells that this value should not/cannot change It gives an explanatory NAME to a value. Makes code less obscure. Use ALL_CAPS for the name. rectmode CENTER is a constant

11 STYLE Programs must be written for people to read, and only incidentally for machines to execute. - H. Abelson and G. Sussman

12 STYLE Great software, likewise, requires a fanatical devotion to beauty. - Paul Graham

13 STYLE A good program Uses layout to convey the structure. Different components are easily recognized. The code adheres to a given coding guideline. All output is spelled correctly, with a proper layout A poor program The layout is coincidental, It is difficult to identify the structure. Names are cryptic. No discernible effort. Spelling mistakes, poorly formatting of output.

14 STYLE Make yourself familiar with professional style guides Using a good IDE will help to program in style. They will automatically fix many problems in your code.

15 EXAMPLES Check the scope

16 DESCISIONS

17 IFS Within a method all lines are essentially executed one after another. Often you want to make execution conditional. If the x-position is larger than 200 fill the shape blue. If the mouse it pressed, draw eyebrows.

18 IFS Within a method all lines are essentially executed one after another. Often you want to make execution conditional. Or you may want to offer two alternatives, choosing one or the other. If the x-position is larger than 200, fill the shape blue, otherwise, fill it red. If the mouse it pressed, draw eyebrows, otherwise, have the eyes look to the left.

19 IFS Within a method all lines are essentially executed one after another. Often you want to make execution conditional. Or you may want to offer two alternatives, choosing one or the other. Or you may want to offer multiple alternatives, choosing one. If the x-position is larger than 200, fill the shape blue. If y-position smaller the 200, fill it red. Otherwise, fill it green. If the mouse it pressed, draw eyebrows. If mouse pointer is on the left, look left. If mouse pointer is on right, look right. Otherwise, close the eyes.

20 IFS A boolean expression if(condition){ dothis... Executed conditionally

21 IFS A boolean expression if(condition){ dothis... Executed conditionally if(condition){ dothis... else{ dothat... Alternative #1 Alternative #2

22 IFS A boolean expression if(condition){ dothis... Executed conditionally if(conditiona){ dothis... Can be any block of code else if(conditionb){ if(condition){ dothis... else{ dothat... Alternative #1 Alternative #2 dothat... else if(conditionc){ domore... else{ A single line, many lines,... The first satisfied condition determines which branch to execute dosomething... Final else is fall back or default case, in case all conditions fail

23 EXAMPLE Conditional Quadrants conditionals-quadrants Combine ifs to multiple if If increase strokewidth if mousepressed, decrease until otherwise.

24 EXERCISE

25 EXERCISE

26 IFS: NESTED IFS Outer if decides on posx Use nested if to represent tables posy <300 posy>=300 posx <200 blue green 200 <= posx <400 red magenta 400 <= posx cyan yellow if(posx < 200){ if(posy < 300){ fill(0,0,255); else{ fill(0,255,0); else if(posx < 400){ if(posy < 300){ fill(255,0,0); else... else... Inner if decides on posy

27 IFS: UNNECESSARY DUPLICATION Unnecessary Duplication if(mousex>200){ fill(255,0,0); rect(mousex,mousey,10,10); nofill(); else{ fill(0,255,0); ellipse(mousex,mousey,10,10); nofill(); Improved if(mousex>200){ fill(255,0,0); rect(mousex,mousey,10,10); else{ fill(0,255,0); ellipse(mousex,mousey,10,10); nofill();

28 BOOLEAN VARIABLES AND OPERATORS

29 BOOLEAN VARIABLES AND OPERATORS Boolean variables are named after the mathematician George Boole. George Boole ( ), was a pioneer in the study of logic. He invented an algebra based on only two values: TRUE and FALSE.

30 BOOLEAN VARIABLES AND OPERATORS Type boolean The boolean data type represents the Boolean type. Variables of type boolean can hold exactly two values, denoted false and true. These values are not strings. There values are definitely not integers They are special values, just for Boolean variables.

31 BOOLEAN VARIABLES Example of defining a Boolean variable boolean islegalage = false; A Boolean variable named islegalage, initialized to false. It can be set by an intervening statement It can be used later to make a decision

32 BOOLEAN OPERATORS Complex Decisions When you make complex decisions, you often need to combine Boolean values. An operator that combines Boolean conditions is called a Boolean operator. Boolean operators take one or two Boolean values or expressions and combine them into a resultant Boolean value.

33 BOOLEAN OPERATORS Complex Decisions Boolean Algebra allows you to compute with Boolean variables like Algebra with normal variables You have Boolean operations similar to addition and multiplication in normal Algebra. Some common Boolean operators are AND, OR, and NOT.

34 THE BOOLEAN OPERATOR && (AND) Mathematicians use the symbol Boolean AND The Boolean expression a AND b Mathematicians write a b evaluates to true, only if a is true and b is true. Both have to be true. In Processing we use the && operator for AND.

35 THE BOOLEAN OPERATOR && (AND) Example if (sharkfree && sunny) { println( Go swimming! ); You can also combine conditions if ((temperature >=0) && (temperature <=100)) { println( Liquid Water );

36 THE BOOLEAN OPERATOR (OR) Mathematicians use the symbol Boolean or The Boolean expression At least one is true. a or b evaluates to true, if a is true or b is true. Both could be true. In Processing we use the operator for OR. Mathematicians write a b Either Or means exactly one is true. Do NOT confuse OR with EITHER OR!

37 THE BOOLEAN OPERATOR (OR) Example if (sharks rainy) { println( Don t swim! ); Don t swim if there are sharks or if it is rainy. Or both. You can also combine conditions if ((temperature <0) (temperature > 100)) { println( No Liquid Water );

38 THE BOOLEAN OPERATOR (OR) Do you want coffee or tea? Yes! Logic rules! Nerd! You can have too much logic with your coffee (or tea)

39 THE BOOLEAN OPERATOR NOT Mathematicians use the symbol Boolean NOT The Boolean expression NOT a Mathematicians write a evaluates to true, only if a is false. In Processing we use the! operator for NOT.

40 THE BOOLEAN OPERATOR NOT Example if (!sharkfree) { println( Don t swim! ); if (!(temperature >=0)) { println( Freezing ); if it is not shark free don t swim. This is the same as temperature<0

41 THE BOOLEAN OPERATORS Combining operators You can combine different operators. Use parentheses to define the order of evaluation. if (!sharks && (warm sunny)) { println( Go swimming! );

42 1 EXERCISE Given boolean manned = true; boolean aircraft=false; double speed =300; double altitude = 1000; Formula (manned && (speed <1000 aircraft)) (altitude >=1000) sub-formula true or false? (altitude >=1000) (speed <1000) (speed <1000 aircraft) (manned && (speed <1000 aircraft)) (manned && (speed <1000 aircraft)) (altitude >=1000)

43 2 EXERCISE Given boolean manned = false; boolean aircraft=true; double speed =1200; double altitude = 200; Formula (manned && (speed <1000 aircraft)) (altitude >=1000) sub-formula true or false? (altitude >=1000) (speed <1000) (speed <1000 aircraft) (manned && (speed <1000 aircraft)) (manned && (speed <1000 aircraft)) (altitude >=1000)

44 3 EXERCISE Given boolean manned = true; boolean aircraft=true; double speed =100; double altitude = 800; Formula (manned && (speed <1000 aircraft)) (altitude >=1000) sub-formula true or false? (altitude >=1000) (speed <1000) (speed <1000 aircraft) (manned && (speed <1000 aircraft)) (manned && (speed <1000 aircraft)) (altitude >=1000)

45 4 EXERCISE Given boolean green = true; boolean edible=false; double price =3.00; int age = 10; Formula (green && price <10.00) (edible && age >=10) sub-formula true or false? (age >=10) (price <10.00) (green && price <10.00) (edible && age >=10) (green && price <10.00) (edible && age >=10)

46 5 EXERCISE Given boolean green = false; boolean edible=true; double price =12.00; int age = 2; Formula (green && price <10.00) (edible && age >=10) sub-formula true or false? (age >=10) (price <10.00) (green && price <10.00) (edible && age >=10) (green && price <10.00) (edible && age >=10)

47 6 EXERCISE Given boolean green = true; boolean edible=true; double price =1.00; int age = 8; Formula (green && price <10.00) (edible && age >=10) sub-formula true or false? (age >=10) (price <10.00) (green && price <10.00) (edible && age >=10) (green && price <10.00) (edible && age >=10)

48 7 EXERCISE Given boolean hot = true; boolean bread=true; boolean sliced = true; int days = 3; Formula (!hot && bread) && (sliced day >=5) sub-formula true or false?!hot (!hot && bread) (day >=5) (sliced day >=5) (!hot && bread) && (sliced day >=5)

49 8 EXERCISE Given boolean hot = true; boolean bread =false; boolean sliced = true; int days = 5; Formula (!hot && bread) && (sliced day >=5) sub-formula true or false?!hot (!hot && bread) (day >=5) (sliced day >=5) (!hot && bread) && (sliced day >=5)

50 9 EXERCISE Given boolean hot = false; boolean bread=true; boolean sliced = false; int days = 7; Formula (!hot && bread) && (sliced day >=5) sub-formula true or false?!hot (!hot && bread) (day >=5) (sliced day >=5) (!hot && bread) && (sliced day >=5)

51 LOOPS

52 LOOPS: WHILE A while loop has this structure while (condition) { statements The condition is some kind of test (the same as in the if statement) The statements are repeatedly executed while the condition is true. The statements are also called the body of the while. It starts with the keyword while Followed by a condition Followed by one or more statements. The loop stops when the condition is false.

53 LOOPS: WHILE Example

54 LOOPS: INFINITE LOOPS Usually, you want to avoid infinite loops They will make you program crash or freeze. position = 0; while (position <= width) { fill(100); ellipse(position,100,40,40); The variable position is not updated. The condition never false.

55 LOOPS: FORS Often you will need to execute a sequence of statements a given number of times. You could use a while loop for this. counter = 1; // Initialize the counter while (counter <= 10) // Check the counter { println(counter); counter++; // Update the counter

56 LOOPS: FORS The for loop is better than while for doing certain things Things that matter for the loop are all over the place. counter = 1; // Initialize the counter while (counter <= 10) // Check the counter { println(counter); counter++; // Update the counter initialization condition statements update

57 LOOPS: FORS There is a custom made for this sort of processing: for loop The same now as a for loop. for (int counter = 1; counter <= 10; counter++) { println(counter); initialization condition statements update

58 LOOPS: FOR For-loops are used when you want repeat something a set number of times, at regular intervals. for(lines = 0; lines < 20; lines++)... for(xpos = 0; xpos < width; xpos += 10)... Repeat something 20 times. Increment by 10 as long a xpos is smaller than width. for(radius = 100; radius > 0; radius = radius - 20)... Decrement by 20 as long a radius is positive.

59 LOOPS: FOR Examples

60 LOOPS: INFINITE LOOPS You can also have accidentally infinite for-loops They will also make you program crash or freeze. for(int x = 100; x>=0; x +=10){ rect(x,200,5,5); Variable x increases without bounds. Condition never false.

61 PROBLEM SOLVING: HAND-TRACING Hand-tracing is a method of checking your work. To do a hand-trace, write your variables on a sheet of paper and mentally execute each step of your code writing down the values of the variables as they are changed in the code. Keep track where you are in the program, and what values the variables have that way you can also see the history of the values.

62 Line n sum digit comment PROBLEM SOLVING " 0 3 " " 4 " " condition true Consider this example: 1 int n = 738; 2 int sum = 0; 3 int digit; 4 while(n>0){ 5 digit=n % 10; 6 sum = sum + digit; 7 n = n/10; 8 9 println("sum: ",sum); What is the hand trace? What is computed?

63 LOOPS: DRAW Remember: The method draw will be repeated again and again. This is a loop too! Use the draw-loop for animation of different frames or to continuously process user input. Use the for or while-loop to perform multiple similar/same tasks within one frame, or one execution of a method like draw.

64 LOOPS:DRAW Examples drawisaloop Refactor to a for loop. Nice example

65 SEMANTICS

66 SEMANTICS Bad code isn't bad, it s just misunderstood. - Programmers aphorism

67 SEMANTICS Computers are good at following instructions, but not at reading your mind. - D. Knuth

68 SEMANTICS The computing scientist s main challenge is not to get confused by the complexities of his own making. - E. W. Dijkstra

69 SEMANTICS A good program Uses the programming construct correctly and as intended. A poor program It uses programming constructs with no regard for their semantics nor intended use. Every line has a clear purpose and effect. Some lines of code have no clear purpose, or misunderstood effect.

PROGRAMMING. We create identity

PROGRAMMING. We create identity 1 PROGRAMMING We create identity INTRODUCTION The course Lecture Tuesdays 10:45 to 12:30 Tutorial Thursday 8:45 to 12:30 or Fridays 12:45 to 17:30 Blackboard Textbook: Learning Processing, by Daniel Shiffman

More information

The way I feel about music is that there is no right and wrong. Only true and false. Fiona Apple. true false false

The way I feel about music is that there is no right and wrong. Only true and false. Fiona Apple. true false false 5 Conditionals Conditionals 59 That language is an instrument of human reason, and not merely a medium for the expression of thought, is a truth generally admitted. George Boole The way I feel about music

More information

CISC 1600 Lecture 3.1 Introduction to Processing

CISC 1600 Lecture 3.1 Introduction to Processing CISC 1600 Lecture 3.1 Introduction to Processing Topics: Example sketches Drawing functions in Processing Colors in Processing General Processing syntax Processing is for sketching Designed to allow artists

More information

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca COMP 1010- Summer 2015 (A01) Jim (James) Young young@cs.umanitoba.ca jimyoung.ca final float MAX_SPEED = 10; final float BALL_SIZE = 5; void setup() { size(500, 500); void draw() { stroke(255); fill(255);

More information

COMP 202 Java in one week

COMP 202 Java in one week COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

More information

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003 Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an

More information

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca COMP 1010- Summer 2015 (A01) Jim (James) Young young@cs.umanitoba.ca jimyoung.ca Don t use == for floats!! Floating point numbers are approximations. How they are stored inside the computer means that

More information

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Decisions Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To be able to implement decisions using if statements To learn

More information

5. Control Statements

5. Control Statements 5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements

More information

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2 Loops Variable Scope Remapping Nested Loops CS105 05 Loops 1 Donald Judd CS105 05 Loops 2 judd while (expression) { statements CS105 05 Loops 3 Four Loop Questions 1. What do I want to repeat? - a rect

More information

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford SSEA Computer Science: Track A Dr. Cynthia Lee Lecturer in Computer Science Stanford Topics for today Introduce Java programming language Assignment and type casting Expressions Operator precedence Code

More information

[ the academy_of_code] Senior Beginners

[ the academy_of_code] Senior Beginners [ the academy_of_code] Senior Beginners 1 Drawing Circles First step open Processing Open Processing by clicking on the Processing icon (that s the white P on the blue background your teacher will tell

More information

The for Loop. Lesson 11

The for Loop. Lesson 11 The for Loop Lesson 11 Have you ever played Tetris? You know that the game never truly ends. Blocks continue to fall one at a time, increasing in speed as you go up in levels, until the game breaks from

More information

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15 Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL

More information

Introduction to Java & Fundamental Data Types

Introduction to Java & Fundamental Data Types Introduction to Java & Fundamental Data Types LECTURER: ATHENA TOUMBOURI How to Create a New Java Project in Eclipse Eclipse is one of the most popular development environments for Java, as it contains

More information

Chapter 4: Conditionals and Loops

Chapter 4: Conditionals and Loops Chapter 4: Conditionals and Loops CS 121 Department of Computer Science College of Engineering Boise State University August 21, 2017 Chapter 4: Conditionals and Loops CS 121 1 / 69 Chapter 4 Topics Flow

More information

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements Topic 4 Variables Once a programmer has understood the use of variables, he has understood the essence of programming -Edsger Dijkstra What we will do today Explain and look at examples of primitive data

More information

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1 Conditionals Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary CS105 04 Conditionals 1 Pick a number CS105 04 Conditionals 2 Boolean Expressions An expression that

More information

1 Getting started with Processing

1 Getting started with Processing cisc3665, fall 2011, lab I.1 / prof sklar. 1 Getting started with Processing Processing is a sketch programming tool designed for use by non-technical people (e.g., artists, designers, musicians). For

More information

1007 Imperative Programming Part II

1007 Imperative Programming Part II Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be

More information

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work.

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work. MIDTERM REVIEW: THURSDAY I KNOW WHAT I WANT TO REVIEW. BUT ALSO I WOULD LIKE YOU TO TELL ME WHAT YOU MOST NEED TO GO OVER FOR MIDTERM. BY EMAIL AFTER TODAY S CLASS. What can we do with Processing? Let

More information

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

Unit 3. Operators. School of Science and Technology INTRODUCTION INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

More information

if / if else statements

if / if else statements if / if else statements December 1 2 3 4 5 Go over if notes and samples 8 9 10 11 12 Conditionals Quiz Conditionals TEST 15 16 17 18 19 1 7:30 8:21 2 8:27 9:18 3 9:24 10:14 1 CLASS 7:30 8:18 1 FINAL 8:24

More information

Variables. location where in memory is the information stored type what sort of information is stored in that memory

Variables. location where in memory is the information stored type what sort of information is stored in that memory Variables Processing, like many programming languages, uses variables to store information Variables are stored in computer memory with certain attributes location where in memory is the information stored

More information

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java Chapter 3 Syntax, Errors, and Debugging Objectives Construct and use numeric and string literals. Name and use variables and constants. Create arithmetic expressions. Understand the precedence of different

More information

Loops. CSE 114, Computer Science 1 Stony Brook University

Loops. CSE 114, Computer Science 1 Stony Brook University Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

Final Exam Winter 2013

Final Exam Winter 2013 Final Exam Winter 2013 1. Which modification to the following program makes it so that the display shows just a single circle at the location of the mouse. The circle should move to follow the mouse but

More information

Interaction Design A.A. 2017/2018

Interaction Design A.A. 2017/2018 Corso di Laurea Magistrale in Design, Comunicazione Visiva e Multimediale - Sapienza Università di Roma Interaction Design A.A. 2017/2018 8 Loops and Arrays in Processing Francesco Leotta, Andrea Marrella

More information

CST112 Looping Statements Page 1

CST112 Looping Statements Page 1 CST112 Looping Statements Page 1 1 2 3 4 5 Processing: Looping Statements CST112 Algorithms Procedure for solving problem: 1. Actions to be executed 2. Order in which actions are executed order of elements

More information

Chapter Goals. Contents LOOPS

Chapter Goals. Contents LOOPS CHAPTER 4 LOOPS Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30, 2011 Chapter Goals To implement while, for, and do loops To hand-trace the execution of a program To become familiar with common

More information

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program 1 By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program variables. Apply C++ syntax rules to declare variables, initialize

More information

CSCI 1061U Programming Workshop 2. C++ Basics

CSCI 1061U Programming Workshop 2. C++ Basics CSCI 1061U Programming Workshop 2 C++ Basics 1 Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment Statements Console Input/Output

More information

CSE 142 Su 04 Computer Programming 1 - Java. Objects

CSE 142 Su 04 Computer Programming 1 - Java. Objects Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request

More information

Technical Questions. Q 1) What are the key features in C programming language?

Technical Questions. Q 1) What are the key features in C programming language? Technical Questions Q 1) What are the key features in C programming language? Portability Platform independent language. Modularity Possibility to break down large programs into small modules. Flexibility

More information

Chapter 4 Introduction to Control Statements

Chapter 4 Introduction to Control Statements Introduction to Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives 2 How do you use the increment and decrement operators? What are the standard math methods?

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

Math Modeling in Java: An S-I Compartment Model

Math Modeling in Java: An S-I Compartment Model 1 Math Modeling in Java: An S-I Compartment Model Basic Concepts What is a compartment model? A compartment model is one in which a population is modeled by treating its members as if they are separated

More information

Ruby: Introduction, Basics

Ruby: Introduction, Basics Ruby: Introduction, Basics Computer Science and Engineering College of Engineering The Ohio State University Lecture 3 Ruby vs Java: Similarities Imperative and object-oriented Classes and instances (ie

More information

COMP 202 Java in one week

COMP 202 Java in one week CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language

More information

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created. + Inheritance + Inheritance Classes that we design in Java can be used to model some concept in our program. For example: Pokemon a = new Pokemon(); Pokemon b = new Pokemon() Sometimes we need to create

More information

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford SSEA Computer Science: Track A Dr. Cynthia Lee Lecturer in Computer Science Stanford Topics for today Java programming language: new tools Two new kinds of operators: RELATIONAL operators (>,

More information

5.1. Examples: Going beyond Sequence

5.1. Examples: Going beyond Sequence Chapter 5. Selection In Chapter 1 we saw that algorithms deploy sequence, selection and repetition statements in combination to specify computations. Since that time, however, the computations that we

More information

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA 1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

3 The L oop Control Structure

3 The L oop Control Structure 3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement

More information

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4) Loops! Flow of Control: Loops (Savitch, Chapter 4) TOPICS while Loops do while Loops for Loops break Statement continue Statement CS 160, Fall Semester 2014 2 An Example while Loop Step- by- step int count

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Chapter 17. Iteration The while Statement

Chapter 17. Iteration The while Statement 203 Chapter 17 Iteration Iteration repeats the execution of a sequence of code. Iteration is useful for solving many programming problems. Interation and conditional execution form the basis for algorithm

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java basics: Compilation vs Interpretation Program structure Statements Values Variables Types Operators and Expressions

More information

We will start our journey into Processing with creating static images using commands available in Processing:

We will start our journey into Processing with creating static images using commands available in Processing: Processing Notes Chapter 1: Starting Out We will start our journey into Processing with creating static images using commands available in Processing: rect( ) line ( ) ellipse() triangle() NOTE: to find

More information

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while

More information

Flow Control: Branches and loops

Flow Control: Branches and loops Flow Control: Branches and loops In this context flow control refers to controlling the flow of the execution of your program that is, which instructions will get carried out and in what order. In the

More information

McGill University School of Computer Science COMP-202A Introduction to Computing 1

McGill University School of Computer Science COMP-202A Introduction to Computing 1 McGill University School of Computer Science COMP-202A Introduction to Computing 1 Midterm Exam Thursday, October 26, 2006, 18:00-20:00 (6:00 8:00 PM) Instructors: Mathieu Petitpas, Shah Asaduzzaman, Sherif

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

Expressions and Casting

Expressions and Casting Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

More information

Topic 4 Expressions and variables

Topic 4 Expressions and variables Topic 4 Expressions and variables "Once a person has understood the way variables are used in programming, he has understood the quintessence of programming." -Professor Edsger W. Dijkstra Based on slides

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Repetition CSC 121 Fall 2014 Howard Rosenthal

Repetition CSC 121 Fall 2014 Howard Rosenthal Repetition CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

Repetition is the reality and the seriousness of life. Soren Kierkegaard

Repetition is the reality and the seriousness of life. Soren Kierkegaard 6 Loops Loops 81 Repetition is the reality and the seriousness of life. Soren Kierkegaard What s the key to comedy? Repetition. What s the key to comedy? Repetition. Anonymous In this chapter: The concept

More information

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop. 11-1 One of the most important structures in Java is the -loop. A loop is basically a block of code that is with certain rules about how to start and how to end the process. Suppose we want to sum up all

More information

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG 1 Notice Prepare the Weekly Quiz The weekly quiz is for the knowledge we learned in the previous week (both the

More information

Accelerating Information Technology Innovation

Accelerating Information Technology Innovation Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lección 03 Control Structures Agenda 1. Block Statements 2. Decision Statements 3. Loops 2 What are Control

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)

More information

COMP 202. Java in one week

COMP 202. Java in one week COMP 202 CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator Java in one week The Java Programming Language A programming language

More information

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide Second Year Edition Name: Tutorial Group: Groovy can be obtained freely by going to http://groovy-lang.org/download Page 1 of 8 Variables Variables

More information

Text Input and Conditionals

Text Input and Conditionals Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

More information

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++ Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure

More information

Flow Control. CSC215 Lecture

Flow Control. CSC215 Lecture Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements

More information

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

L o o p s. for(initializing expression; control expression; step expression) { one or more statements } L o o p s Objective #1: Explain the importance of loops in programs. In order to write a non trivial computer program, you almost always need to use one or more loops. Loops allow your program to repeat

More information

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh C++ PROGRAMMING For Industrial And Electrical Engineering Instructor: Ruba A. Salamh CHAPTER TWO: Fundamental Data Types Chapter Goals In this chapter, you will learn how to work with numbers and text,

More information

CS 177 Week 15 Recitation Slides. Review

CS 177 Week 15 Recitation Slides. Review CS 177 Week 15 Recitation Slides Review 1 Announcements Final Exam on Friday Dec. 18 th STEW 183 from 1 3 PM Complete your online review of your classes. Your opinion matters!!! Project 6 due Just kidding

More information

ME 461 C review Session Fall 2009 S. Keres

ME 461 C review Session Fall 2009 S. Keres ME 461 C review Session Fall 2009 S. Keres DISCLAIMER: These notes are in no way intended to be a complete reference for the C programming material you will need for the class. They are intended to help

More information

mith College Computer Science CSC103 How Computers Work Week 6 Fall 2017 Dominique Thiébaut

mith College Computer Science CSC103 How Computers Work Week 6 Fall 2017 Dominique Thiébaut mith College Computer Science CSC103 How Computers Work Week 6 Fall 2017 Dominique Thiébaut dthiebaut@smith.edu Ben Fry on Processing... http://www.youtube.com/watch?&v=z-g-cwdnudu An Example Mouse 2D

More information

cs1114 REVIEW of details test closed laptop period

cs1114 REVIEW of details test closed laptop period python details DOES NOT COVER FUNCTIONS!!! This is a sample of some of the things that you are responsible for do not believe that if you know only the things on this test that they will get an A on any

More information

(Refer Slide Time: 00:26)

(Refer Slide Time: 00:26) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute Technology, Madras Module 07 Lecture 07 Contents Repetitive statements

More information

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca COMP 1010- Summer 2015 (A01) Jim (James) Young young@cs.umanitoba.ca jimyoung.ca Hello! James (Jim) Young young@cs.umanitoba.ca jimyoung.ca office hours T / Th: 17:00 18:00 EITC-E2-582 (or by appointment,

More information

Advanced Computer Programming

Advanced Computer Programming Programming in the Small I: Names and Things (Part II) 188230 Advanced Computer Programming Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Department of Computer Engineering Khon Kaen University

More information

Object-Oriented Programming

Object-Oriented Programming Object-Oriented Programming Java Syntax Program Structure Variables and basic data types. Industry standard naming conventions. Java syntax and coding conventions If Then Else Case statements Looping (for,

More information

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage: Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;

More information

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00

More information

SFPL Reference Manual

SFPL Reference Manual 1 SFPL Reference Manual By: Huang-Hsu Chen (hc2237) Xiao Song Lu(xl2144) Natasha Nezhdanova(nin2001) Ling Zhu(lz2153) 2 1. Lexical Conventions 1.1 Tokens There are six classes of tokes: identifiers, keywords,

More information

Scope of this lecture. Repetition For loops While loops

Scope of this lecture. Repetition For loops While loops REPETITION CITS1001 2 Scope of this lecture Repetition For loops While loops Repetition Computers are good at repetition We have already seen the for each loop The for loop is a more general loop form

More information

Add Subtract Multiply Divide

Add Subtract Multiply Divide ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient

More information

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. 9 Self-Test Exercises p. 11 History Note p. 12 Programming and

More information

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3 Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3 Ziad Matni Dept. of Computer Science, UCSB A Word About Registration for CS16 FOR THOSE OF YOU NOT YET REGISTERED:

More information

CS 251 Intermediate Programming Java Basics

CS 251 Intermediate Programming Java Basics CS 251 Intermediate Programming Java Basics Brooke Chenoweth University of New Mexico Spring 2018 Prerequisites These are the topics that I assume that you have already seen: Variables Boolean expressions

More information

Chapter 4: Conditionals and Loops

Chapter 4: Conditionals and Loops Chapter 4: Conditionals and Loops CS 121 Department of Computer Science College of Engineering Boise State University March 2, 2016 Chapter 4: Conditionals and Loops CS 121 1 / 76 Chapter 4 Topics Flow

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College October 24, 2018 Outline Outline 1 Chapter 8: A C++ Introduction For Python Programmers Expressions and Operator Precedence

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

Computational Physics - Fortran February 1997

Computational Physics - Fortran February 1997 Fortran 90 Decision Structures IF commands 3 main possibilities IF (logical expression) IF (logical expression) THEN IF (logical expression) THEN IF (logical expression) THEN expression TRUE expression

More information