1 / 12

Control structures

Control structures. Chapter 3. Topics. Assignment statement Selection: if..else Multi-way selection: switch Repetition Functions: setup(), draw(), mousePressed (). Repetition. Three kinds of loops For loop While loop Do while loop. Loops syntax. While loop i nt i =0;

bina
Download Presentation

Control structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control structures Chapter 3

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

  3. Repetition Three kinds of loops For loop While loop Do while loop

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

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

  6. 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

  7. Local Variables and Scope void setup() { int locVar1 = 301; println(“ lacvar1 in setup = “, locVar1); { int locVar2 = 290; println(“ locVar1 nested in setup = +“ locVar1); println(“locVar2 nexted in setup = +”locVar2); } println(“ local locVar2 out of scope = “+ locVar2); } void func() { println(“ local locVar1 out of scope = “+ locVar1); println(“ local locVar2 out of scope = “+ locVar2); }

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

  9. Variable naming conventions 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 drawAnimal drawAfricanAnimal drawSquaresCircles mySalary myDocs myNameAddressGrade

  10. Selection statement 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. Switch statement: multi-way selection Format or syntax: switch(val) { case 0: do this; break; case 1: do this; break; default: do this; } // lets use this in an example

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

More Related