Flow of Control Recitation – 09/(11,12)/2008 CS 180 Department of Computer Science, Purdue University.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
ITEC113 Algorithms and Programming Techniques
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Selection Statements & Exam 1 Review Recitation – 2/13/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 41 Defining Classes and Methods Chapter 4.
1 CS 177 Week 4 Recitation Slides Conditionals. 2 Announcements Project 1 is due on Feb. 7th 9pm (extended) Project 2 will be posted today (Feb. 5th)
The Type boolean. Boolean Expressions and Boolean Variables  The type boolean is a primitive type  Variables of type boolean and Boolean expressions.
● Basic Computation Recitation – 09/(04,05)/2008 CS 180 Department of Computer Science, Purdue University.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Chapter 31 Flow of Control Chapter 3. 2 Reminders Project 1 was due last night Project 2 released: due Sept 10:30 pm - No Late Submissions Follow.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Flow of Control Recitation – 09/(18,19)/2008 CS 180 Department of Computer Science, Purdue University.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
COMP Flow of Control: Branching 2 Yi Hong May 19, 2015.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
Chapter 3 Edited by JJ Shepherd
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
COMP 110: Spring Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
Flow of Control Chapter 3 Flow of control Branching Loops
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
JavaScript, Fourth Edition
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Chapter 3 1 l Branching l Loops l exit(n) method l Boolean data type and logic expressions Flow of Control – Part 1.
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter 31 Flow of Control Chapter 3. 2 Objectives learn about Java branching statements learn about loops learn about the type boolean (optional) learn.
Chapter 31 Announcements Project 2 has been posted –Due Feb 1st at 10:00pm –Work ALONE! Help hours –Monday – Thursday, 7-10pm –LWSN B146 Quiz solutions.
JAVA PROGRAMMING Control Flow. Jeroo: Finish Activities 1-7 Finish InputTest program w/changes.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Flow of Control Chapter 3. JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
COS 260 DAY 5 Tony Gauvin.
Flow of Control.
Chapter 3 Branching Statements
Microsoft Visual Basic 2005 BASICS
Flow of Control Chapter 3 Chapter 3.
Truth tables: Ways to organize results of Boolean expressions.
Flow of Control Chapter 3.
Announcements/Reminders
Announcements Program 1 due noon Lab 1 due noon
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

Flow of Control Recitation – 09/(11,12)/2008 CS 180 Department of Computer Science, Purdue University

Now posted on the class webpage. Due Wed, Sept. 17 at 10 pm. Start early. All questions on the class newsgroup. Evening consulting hours. LWSN B146. MTW 7-10 pm. Project 2

If and else parts in braces are preferred for even a single statement. if (count < 3) { total = 0; } is better than if (count < 3) total = 0; Statements in Braces

Benefits to do this: Decreases the chance of introducing a bug when adding a new statement. Makes the program a little more “self- documenting” (more readable and easier to maintain).

== Is NOT Always Equal == should not be used to determine if two objects have the same value, including two strings Even though (s1 == s2) might appear to work, do not use this. Use (s1.equals(s2)) instead to compare two strings.

Multibranch if-else Statements if-else if-else is different from a series of if statements. if (score >= 60) { result = ‘P’; } else if (score > 0) { result = ‘F’; } else { result = ‘Z’; } if (score >= 60) { result = ‘P’; } if (score > 0) { result = ‘F’; } else { result = ‘Z’; }

Short-circuit Evaluation Can prevent problems. if ((number != 0) && (sum/number > 5)) Can produce problems! if ((x.evaluate()) && (y.reassign())) How to run y.reassign() every time? Use & and | to replace && and ||. if ((x.evaluate()) & (y.reassign())) But notice & and && are different!

Input & Output of Boolean Values Example: boolean booleanVar = false; System.out.println(booleanVar); System.out.println("Enter a boolean value:"); Scanner keyboard = new Scanner(System.in); booleanVar = keyboard.nextBoolean(); System.out.println("You entered " + booleanVar);

Input & Output of Boolean Values Dialog: false Enter a boolean value: true You entered true

The switch Statement Syntax: switch (Controlling_Expression) { case Case_Label: Statement(s); break; case Case_Label: … default: … }

The switch Statement View sample program Listing 3.4 class MultipleBirthssample program Sample screen output

The switch Statement The action for each case typically ends with the word break. The optional break statement prevents the consideration of other cases. The controlling expression can be anything that evaluates to an integral type.

The switch Statement Example without break : switch (grade) { case ‘P’: System.out.print(“You have”); case ‘F’: System.out.print(“…No, you have not”); default: } System.out.println(“passed.”);

Enumerations Consider a need to restrict contents of a variable to certain values. An enumeration lists the values a variable can have. Example enum MovieRating {E, A, B} MovieRating rating; rating = MovieRating.A;

Enumerations Now possible to use in a switch statement

Enumerations An even better choice of descriptive identifiers for the constants. enum MovieRating {EXCELLENT, AVERAGE, BAD} rating = MovieRating.AVERAGE; case EXCELLENT:...

Specifying a Drawing Color When drawing a shape inside an applet’s paint method, think of the drawing being done with a pen that can change colors. The method setColor changes the color of the "pen." canvas.setColor(Color.YELLOW); Drawings done later appear on top of drawings done earlier.

Specifying a Drawing Color View sample program, Listing 3.5 class YellowFacesample program Sample screen output

Specifying a Drawing Color Predefined Colors for the setColor Method:

Dialog Box for a Yes/No Question Used to present the user with a yes/no question. The window contains  The question text  Two buttons labeled yes and no.

Dialog Box for a Yes/No Question Example: int answer = JOptionPane.showConfirmDialog(null, "End program?", "Click Yes or No:", JOptionPane.YES_NO_OPTION); if (answer == JOptionPane.YES_OPTION) System.exit(0); else if (answer == JOptionPane.NO_OPTION) System.out.println("One more time");

Dialog Box for a Yes/No Question A Yes-or No-Dialog Box: