Control structures CHAPTER 3 Topics Assignment statement Selection

  • Slides: 12
Download presentation
Control structures CHAPTER 3

Control structures CHAPTER 3

Topics �Assignment statement �Selection: if. . else �Multi-way selection: switch �Repetition �Functions: setup(), draw(),

Topics �Assignment statement �Selection: if. . else �Multi-way selection: switch �Repetition �Functions: setup(), draw(), mouse. Pressed()

Repetition �Three kinds of loops �For loop �While loop �Do while loop

Repetition �Three kinds of loops �For loop �While loop �Do while loop

Loops syntax �While loop int i =0; while (i < 100) { do something;

Loops syntax �While loop int i =0; while (i < 100) { do something; i = i + 1; } �For loop for (int i =0; i < 100; i = i +1) { do something; }

Loops syntax (contd. ) int i = 0; do { do something; i =

Loops syntax (contd. ) int i = 0; do { do something; i = i + 1; } while (i < 100);

Lets apply this to drawing some shapes �On to Processing: �Draw 10 rectangles of

Lets apply this to drawing some shapes �On to Processing: �Draw 10 rectangles of different sizes and at different locations �Draw 10 circles of different sizes and at different locations �Parameter passing

Local Variables and Scope 7 void setup() { int loc. Var 1 = 301;

Local Variables and Scope 7 void setup() { int loc. Var 1 = 301; println(“ lacvar 1 in setup = “, loc. Var 1); { int loc. Var 2 = 290; println(“ loc. Var 1 nested in setup = +“ loc. Var 1); println(“loc. Var 2 nexted in setup = +”loc. Var 2); } println(“ local loc. Var 2 out of scope = “+ loc. Var 2); } void func() { println(“ local loc. Var 1 out of scope = “+ loc. Var 1); println(“ local loc. Var 2 out of scope = “+ loc. Var 2); } 11/26/2020

Boolean variable 8 �What are the different types of variable you have learned so

Boolean variable 8 �What are the different types of variable you have learned so far? int, float, char �Here is one more: boolean �A boolean varaible can take “true” or “false” as values. �How to define it? �How to initialize it? �How to use it? boolean win; win = true; 11/26/2020

Variable naming conventions 9 � Variable name begin with lower case and the first

Variable naming conventions 9 � Variable name begin with lower case and the first word is all lowercase. � If there are more than one word in the variable you begin the second with an upper case; do that same for more than two words of variable names too. � Examples: setup draw. Animal draw. African. Animal draw. Squares. Circles my. Salary my. Docs my. Name. Address. Grade 11/26/2020

Selection statement 10 �We already studied if. . else �See some examples in your

Selection statement 10 �We already studied if. . else �See some examples in your text �Lets look at out lab 1 and see how we use this if. . else if (condition) { statements to be executed } else { statements to be executed} We could use nested if. . else for multi-way selection. . 11/26/2020

Switch statement: multi-way selection 11 �Format or syntax: switch(val) { case 0: do this;

Switch statement: multi-way selection 11 �Format or syntax: switch(val) { case 0: do this; break; case 1: do this; break; default: do this; } // lets use this in an example 11/26/2020

Example for switch int ct=0; int count = 0; int counta=0; int countb=0; int

Example for switch int ct=0; int count = 0; int counta=0; int countb=0; int countc=0; void key. Pressed() { switch (key) { case 'a' : ct = counta++; break; case 'b' : ct = countb++; break; case 'c' : ct = countc++; break; default: ct = count++; } println(ct); }