Control structures Chapter 3.

Slides:



Advertisements
Similar presentations
Java Control Statements
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
Control Flow Statements: Repetition/Looping
CS0007: Introduction to Computer Programming
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Discussion1 Quiz. Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) e) DEFAULT_VALUE f) bad-choice.
3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
Chapter 5: Structured Programming
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
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:
Conditional Statements Consider: if the mouse location is contained in the rectangle, display message “success” Some programming constructs can choose.
Java Decision Making and booleans (Java: An Eventful Approach, Ch 4), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture October 2012.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Control statements Mostafa Abdallah
CSI 3125, Preliminaries, page 1 Control Statements.
Lesson thirteen Conditional Statement "if- else" ©
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Test 2 Review Outline.
Exam 2 Review.
Compound Condition Break , Continue Switch Statements in Java
Decisions Chapter 4.
Condition Statements.
Control structures Chapter 3.
Object-Oriented Programming
Chapter 5 Repetition.
Selection (if-then-else)
Control Structures: Selection Statement
More programming with "Processing"
Control structures Chapter 3.
CS139 October 11, 2004.
Lecture 6: Conditionals AP Computer Science Principles
Lecture Notes – Week 2 Lecture-2
February , 2009 CSE 113 B.
PROGRAM FLOWCHART Selection Statements.
Control structures Chapter 3.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Control Structures: Selection Statement
Presentation transcript:

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 int i =0; while (i < 100) { do something; i = i + 1; } For loop for (int i =0; i < 100; i = i +1) {

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 different sizes and at different locations Draw 10 circles of different sizes and at different locations Parameter passing

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); 2/19/2019

Boolean variable 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; 2/19/2019

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 2/19/2019

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.. 2/19/2019

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 2/19/2019

Example for switch int ct=0; int count = 0; int counta=0; int countb=0; int countc=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);