Review Expressions and operators Iteration – while-loop – for-loop.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
Programming Languages and Paradigms The C Programming Language.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Computer Science 1620 Loops.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Loops – While, Do, For Repetition Statements Introduction to Arrays
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Chapter 6: Functions.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Procedural programming in Java Methods, parameters and return values.
Review Loops – Condition – Index Functions – Definition – Call – Parameters – Return value.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
JavaScript, Fourth Edition
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
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.
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
Words. Characters and Strings Character –A single character inside of single quotes char letter = 'A' ; char digit = '0' ; – Strings Zero or more character.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
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.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Java Programming: Guided Learning with Early Objects
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 - Functions Outline 5.1 Introduction
C Programming Getting started Variables Basic C operators Conditionals
Programming Languages and Paradigms
Presentation transcript:

Review Expressions and operators Iteration – while-loop – for-loop

Coding styles Headers Comments Indentation Parentheses Spacing

text() Strings can be drawn on a sketch using the text() function. Can set text position, font, size, alignment, … Font files are loaded from the data folder. // Set attributes textSize( sizeInPixels ); textAlign( {LEFT | CENTER | RIGHT} [,{TOP, BOTTOM, CENTER, BASELINE}] ); fill( color ); // Render text text( string, X, Y ); text( string, X, Y, width, height );

// text void setup() { size(500, 500); noLoop(); } void draw() { // bounding box stroke(0); fill(255); rect(50, 50, 400, 400); // text options fill(0); // black text text("Default", 50, 50, 400, 400); textAlign(CENTER); text("CENTER", 50, 50, 400, 400); textAlign(RIGHT); text("RIGHT", 50, 50, 400, 400); textAlign(CENTER, CENTER); text("CENTER-CENTER", 50, 50, 400, 400); textAlign(RIGHT, BOTTOM); text("RIGHT-BOTTOM", 50, 50, 400, 400); textAlign(LEFT, BOTTOM); text("LEFT-BOTTOM", 50, 50, 400, 400); }

Repetition of a program block Iterate when a block of code is to repeated multiple times. Options while-loop for-loop Iteration

Iteration: while-loop while ( boolean_expression ) { statements; // continue; // break; } Statements are repeatedly executed while the boolean expression remains true. To break out of a while loop, call break; To continue with next iteration, call continue; All iterations can be written as while-loops.

Iteration: for-loop for ( initialization; continuation_test; update) { statements; // continue;// Continues with next iteration // break;// Breaks out of loop } A kind of iteration construct initialization, continuation test and increment commands are part of statement To break out of a loop, call break; To continue with next iteration step, call continue; All for loops can be translated to equivalent while loops

void mousePressed() { for (int i = 0; i < 10; i++ ){ print( i ); } println(); } void draw() { } void mousePressed() { for (int i = 0; i < 10; i++ ) { if ( i % 2 == 1 ) continue; print( i ); } println(); } void draw() { }

Functions Informally A function A function is like a subprogram, a small program inside of a program. The basic idea – we write a sequence of statements and then give that sequence a name. We can then execute this sequence at any time by referring to the name. Function definition: this is where you create a function and define exactly what it does Function call: when a function is used in a program, we say the function is called. A function can only be defined once, but can be called many times.

Function Examples void setup() { … } void draw() { … } void line( float x1, float y1, float x2, float y2) { … } … and other graphic functions float float( … ) … and other type-conversion functions … etc.

Functions Modularity – Functions allow the programmer to break down larger programs into smaller parts. – Promotes organization and manageability. Reuse – Enables the reuse of code blocks from arbitrary locations in a program.

Function Parameters Parameters (arguments) can be “passed in” to function and used in body. Parameters are a comma-delimited set of variable declarations. Parameters act as input to a function. Passing parameters provides a mechanism to execute a function with many different sets of input We can call a function many times and get different results by changing its parameters.

What happens when we call a function? Execution of the main (calling) program is suspended. The argument expressions are evaluated. The resulting values are copied into the corresponding parameters. The statements in the function's body are executed in order. Execution of the main program is resumed when a function exits (finishes).

More Examples /************************************** ** This function squares a number ** Inputs: a value to be squared ** Outputs: returns the square of the number ** provided **************************************/ double square (double n) { return n*n; } /**************************************8 ** Function: FindMinimum () ** Finds the minimum of two integers ** Inputs: integers n1 and n2 to be compared ** Outputs: returns the smaller of n1 and n2. ****************************************/ int findMinimum (int n1, int n2) { int min; if(n1<n2) { min = n1; } else { min = n2; } return min; }

Functions that return values The return value of a function is the output of a function. A function evaluates to its return value. Function must return a value whose type matches the function declaration. return_type function_name( argument_decl_list ) { statements; return value; }

Variable Scope The part of the program from which a variable can be accessed. Rules : 1.Variables declared in a block are only accessible within the block. 2.Variables declared in an outer block are accessible from an inner block. 3.Variables declared outside of any function are considered global (available to all functions).

Variable Lifetime – Variables cannot be referenced before they are declared. – Variables can be declared in… the global scope the body of a function or constructor the arguments of a function or constructor a statement block (for, while, if, …). – A variable is created and initialized when a program enters the block in which it is declared. – A variable is destroyed when a program exists the block in which it was declared.

int v1 = 1; void setup() { int v2 = 2; for (int v3=3; v3 <= 3; v3++) { int v4 = 4; println(" "); println("v1=" + str(v1)); println("v2=" + str(v2)); println("v3=" + str(v3)); println("v4=" + str(v4)); //println("v5=" + str(v5)); } int v3 = 6; println("v3=" + str(v3)); aFunction(v2); } void aFunction(int v5) { println(" "); println("v1=" + str(v1)); //println("v2=" + str(v2)); //println("v3=" + str(v3)); //println("v4=" + str(v4)); println("v5=" + str(v5)); } void draw() { } What is printed? What happens if the second v3 declaration is removed? What would happen if the v5 print statement is executed? What would happen if commented statements in aFunction were called?