DEMYSTIFYING PROGRAMMING: CHAPTER TEN REPETITION WITH WHILE-LOOP (TOC DETAILED)

Size: px
Start display at page:

Download "DEMYSTIFYING PROGRAMMING: CHAPTER TEN REPETITION WITH WHILE-LOOP (TOC DETAILED)"

Transcription

1 DEMYSTIFYING PROGRAMMING: CHAPTER TEN REPETITION WITH WHILE-LOOP (TOC DETAILED) Chapter Ten: Classes Revisited, Repetition with while... 1 Objectives Design... 1 Repetition process pseudocode Repetition... 2 while loop Patterns... 3 while loop Language... 3 GUI Font and Color class Reuse... 3 Font class... 3 Color class Review... 5 Designing control structures for repetition... 5 The while loop... 5 The use of Font and Color Challenge Exercise... 6 Design Exercises... 6 Code Analysis Exercises... 6 Complete the Code Exercises... 6 Code to Mend... 7 Code Exercises... 7 Index... 8

2 Chapter Ten: Classes Revisited, Repetition with while Objectives How do I repeat a process the easy way? How do I control repetitions (avoid the infinite loop)? How can I use font and color to improve the appearance of my JApplet? 10.1 Design Repetition process pseudocode We have worked with sequence and decision as control mechanisms in our programs. These two controls, however, just do not provide enough control. Consider the averaging of a series of numbers. As long as we are willing to wait for the user to add the new number we can repeat the sum and average process. This is a crude form of repetition. The functional analysis for the averaging process looks like this averagenums: get user input as newnum add newnum to store total increment counter divide total by counter as average As long as we are able and willing to wait for the user to start the process again from the top, and as long as we keep the total and counter as instance variables whose state remains between averaging events, this approach works. However, what if we wanted to determine the value of $100 deposited for ten years at 5% compounded interest? We don t want the user to input or press a button for each year (ten times). Waiting for the user to push the button when we already know that we want to run the calculation ten times is unnecessary. Instead let s ask the user how many years he wants to leave the money to earn this interest and repeat the calculation automatically based on a single input. The algorithm now looks like this: calculatebalance: set balance to 100 set counter to 0 get user input as number of years while counter is less than number of years loop: balance equals balance plus interest(balance times 0.05) increment counter end loop return balance Demystifying Programming Chapter Ten Page 1

3 This approach to solving the problem makes the calculation a controlled repetition. We setup the control with the counter compared to the user s input of number of years. The while includes the condition that counter must be less than the number of years. The number of years is the termination point. The lines within the while-loop keep the current balance and add to it an additional 5%. The counter must be incremented or the loop would never cease because it would never reach the terminal condition (number of years). Such an infinite loop cannot be allowed. These two lines (the process) repeat until the counter value reaches the value number of years. At that point the control of this algorithm jumps to the end of the loop and continues in sequential fashion on its merry way. Without this repetition control structure, we would have to type and re-type the calculation line code ten times to get the same result. This would be tedious typing and would only solve the problem when the number of years needed to be ten. If the number of years input was five, the wrong result would be calculated or we would have to revise the code before the solution would fit the new condition of five years. This repetition control tool not only saves a lot of typing it makes our solution general enough to handle any number of years input by the user without any additional coding work Repetition while loop The Java language includes a while-loop control. The calculatebalance algorithm illustrated in Design pseudocode above can easily be coded as follows. The full file is Interest.java: GUI component txtyears is a JTextField for user input of years Figure 1: Part of Interest.java Line 44 begins the while loop. Notice the condition that must be met for each cycle of the loop is that counter is less than yrs. It is essential that counter is incremented within the while-loop. Demystifying Programming Chapter Ten Page 2

4 Experiment by commenting line 46. What happens if there is no incrementing of the counter? Why? This method, calcbalance, is called (invoked) from line 39 with the numyears collected from txtyears. The return value of this method is consumed on the same line (39) Patterns while loop A counter for the condition is initialized before the loop begins while key word marks the beginning of the loop while is followed by the condition that must be true in order for a loop to occur. The counter is part of the condition. The body of the loop contains one or more lines of code as desired processing. The body of the loop also increments the counter to eventually terminate the loop. counter = 0; while (condition){ processes here 10.4 Language GUI Font and Color class Reuse Font class We have been working with the default Font on all of previous applets. It is now time to control this aspect of our products appearance too. Java already has a Font class. To use this class it is necessary to declare and instantiate an object of this class. Then use the setfont method of another object to apply the new font to that object. The code for these steps is as follows: Font myfont; //declaration myfont = new Font("Arial", Font.PLAIN, 16); display.setfont(myfont); //changing font for display Study the example InterestFonts.java provided for more detail. Experiment by changing Font.PLAIN on line 26 to Font.BOLD. You can probably guess what effect this has. Compile and test this change. Notice that PLAIN, BOLD, and ITALIC are all caps and control the appearance of the font. Variable names in all caps are called constants. You may choose a larger or smaller font size number Demystifying Programming Chapter Ten Page 3

5 than 16 to experiment with as well. If you want to take this experiment further declare and instantiate separate fonts for PLAIN called myfontplain, BOLD called myfontbold, and ITALIC called myfontitalic. Apply one to the JLabel, another to the JTextField, and the third to the JTextArea by calling a GUI object by name and dotting the setfont( ) method to it: display.setfont(myfontbold); Although the Font class is useful, don t over work this feature in your JApplets. Color class Another appearance feature that can be over used is color. The Color class has thirteen pre-established colors that can be called by name. black white red blue lightgray green yellow cyan gray pink orange darkgray magenta Standard 13 color names in Java Using the pre-established named Color classes found in the chart above is quite easy. Load, compile, and execute InterestColors.java to see color change in action. The relevant code uses the setforeground and setbackground methods. A full spectrum of colors is also available using a RGB decimal value combination. Creating a Color class object is simple. Declare, instantiate, and apply a Color class object to a GUI component using the constructor Color(RGB) where RGB are three decimal numbers each 255 or less, separated by commas. Figure 2: Use of Color Constructor The numbers inside the Color constructor on line 34 are standard decimal values for the amount of Red, Green, and Blue that make up this lovely new color. For our purposes this week, the standard thirteen named colors will be adequate. Challenge: Apply a color to a button or a label. Demystifying Programming Chapter Ten Page 4

6 10.5 Review Designing control structures for repetition In order to be effective a repetition control structure must loop a controlled number of times. Uncontrolled looping is a misuse of this structure. It is referred to as an infinite loop and must be avoided. Control is maintained by initializing a control variable before the loop, testing against this control variable at the head of each loop (the condition), and incrementing the counter inside the loop. The while loop A while-loop allows a repetition to begin and continue as long as a condition has been met. When the condition can no longer be met (is false), control jumps below the loop body (marked by a curly brace). The loop begins with a while statement followed by a condition in parentheses. But it is crucial to establish the counter before the loop begins. Care must be taken in designing the condition. The comparison operators > and < are preferred over ==. The counter must be incremented inside the loop. while (counter < 10){ do the processes found here; 1 initialize counter 2 use counter in condition 3 increment counter in loop The use of Font and Color Font and Color can enhance a GUI as long as they are not over used. Both classes exist in the Java libraries of awt. A Font object is declared and instantiated as other objects are. It is appled with the setfont command to an appropriate GUI object. Font smallfont = new Font( Arial, Font.PLAIN, 16); display.setfont(smallfont); Font largefont = new Font( Arial, Font.BOLD, 20); display.setfont(largefont); 10.6 Challenge Redesign one of the exercises from a previous chapter using two Font definitions and two Color choices. Demystifying Programming Chapter Ten Page 5

7 10.7 Exercise Design Exercises 1. A company needs an amortization table for various capital items. An amortization table presents the value of a depreciating item for each year of the depreciation. Using straight-line depreciation for a $10,000 vehicle over five years with no residual value would see the value of the truck diminish by 20% per year. This means that a truck will lose 20% of its value each year for five years until residual value reaches $0 and the truck can be junked. Design a while-loop that will calculate and display depreciation on this truck. Hint: What will be the control counter in this situation? Present your work in pseudocode only. 2. Produce models to describe a Cat class. What method will this non- JApplet class have? What additional attributes and methods do you wish to add to your model? 3. Provide a functional analysis with pseudocode for a method which totals the numbers from 1 to 100 using a while loop. Code Analysis Exercises Describe what each line of the following code accomplishes: 4. int total = 0; while (counter < 100){ total += total + 2; display.settext( Your total is + total); 5. int total = 0; while (counter < total){ total += counter; Complete the Code Exercises 6. Load Account1.java and add appropriate code at lines 18, 40 and 47 according to comments found in code. You will need to understand the code s purpose to complete this exercise. 7. Load Account2.java and supply the while loop according to the comment. Notice that the logic has changed. It may be useful to do a functional analysis of this solution before coding. Demystifying Programming Chapter Ten Page 6

8 Code to Mend Explain the problem with each of the following: 8. int total =0; while (counter < 100){ total += 2; 9. int total =0; while (counter > 0){ total += 2; 10. How should you respond the following compiler error? A:\CarLoan.java:57: cannot resolve symbol symbol : variable monthlyinterst location: class CarLoan display.append("\nyour total interest is "+ (monthlyinterst * 36)); ^ 1 error Tool completed with exit code 1 Code Exercises 11. Create a game called TestPenny which shows how much a person will have each day if he receives 1 penny the first day and the amount is doubled with each passing day. This means (2*1) two cents the second day, (2*2) four cents the third day, (4*2) eight cents the fourth day, etc. Show each day s additional income and the sum at the end of each day. Show what will happen for fifteen days. [hint: Use a variable for valuetoday and multiply that value times the counter of the day.] Complete a functional analysis before coding this solution. No event is required for this solution. The code is easier if counter is initialized to 1, but watch the termination point as fifteen days must be displayed. Demystifying Programming Chapter Ten Page 7

9 Index A J algorithm, 2, 3 append, 8 attributes, 7 JLabel, 5 JTextArea, 5 JTextField, 3, 5 C L Color, 1, 4, 5, 6 comment, 7 condition, 3, 4, 6 constants, 4 constructor, 5 counter, 2, 3, 4, 6, 7, 8 decision, 2 declare, 4, 5 equals, 2 event, 8 events, 2 Font, 1, 4, 5, 6 D E F loop, 1, 2, 3, 4, 6, 7 methods, 5, 7 new, 2, 3, 4, 5, 6 parentheses, 6 process, 1, 2, 3, 6 pseudocode, 1, 2, 3, 7 repeat, 2, 3 repetition, 1, 2, 3, 6, 7 return, 2, 4 M N P R GUI, 1, 2, 3, 4, 5, 6 G sequence, 2 S if, 2, 4, 8 instantiate, 4, 5 int, 6, 7, 8 invoked, 4 is equal to, 2 I variable, 6, 8 while, 1, 2, 3, 4, 6, 7, 8 while-loop, 3, 6, 7 V W Demystifying Programming Chapter Ten Page 8

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1 DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1 Objectives 1 6.1 Methods 1 void or return 1 Parameters 1 Invocation 1 Pass by value 1 6.2 GUI 2 JButton 2 6.3 Patterns

More information

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times } Rolling a Six-Sided Die face = 1 + randomnumbers.nextint( 6 ); The argument 6 called the scaling factor represents the number of unique values that nextint should produce (0 5) This is called scaling

More information

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR DEMYSTIFYING PROGRAMMING: CHAPTER FOUR Chapter Four: ACTION EVENT MODEL 1 Objectives 1 4.1 Additional GUI components 1 JLabel 1 JTextField 1 4.2 Inductive Pause 1 4.4 Events and Interaction 3 Establish

More information

Java Programming Lecture 6

Java Programming Lecture 6 Java Programming Lecture 6 Alice E. Fischer Feb 15, 2013 Java Programming - L6... 1/32 Dialog Boxes Class Derivation The First Swing Programs: Snow and Moving The Second Swing Program: Smile Swing Components

More information

DEMYSTIFYING PROGRAMMING: CHAPTER SIXTEEN ADD AND SEARCH AN ARRAY (TOC DETAILED)

DEMYSTIFYING PROGRAMMING: CHAPTER SIXTEEN ADD AND SEARCH AN ARRAY (TOC DETAILED) DEMYSTIFYING PROGRAMMING: CHAPTER SIXTEEN ADD AND SEARCH AN ARRAY (TOC DETAILED) Chapter Sixteen: Add and Search an Array... 1 Objectives... 1 16.1 Design and Implementation... 1 An Array-based List can

More information

1.00 Lecture 14. Lecture Preview

1.00 Lecture 14. Lecture Preview 1.00 Lecture 14 Introduction to the Swing Toolkit Lecture Preview Over the next 5 lectures, we will introduce you to the techniques necessary to build graphic user interfaces for your applications. Lecture

More information

Class 14: Introduction to the Swing Toolkit

Class 14: Introduction to the Swing Toolkit Introduction to Computation and Problem Solving Class 14: Introduction to the Swing Toolkit Prof. Steven R. Lerman and Dr. V. Judson Harward 1 Class Preview Over the next 5 lectures, we will introduce

More information

Chapter 12 GUI Basics

Chapter 12 GUI Basics Chapter 12 GUI Basics 1 Creating GUI Objects // Create a button with text OK JButton jbtok = new JButton("OK"); // Create a label with text "Enter your name: " JLabel jlblname = new JLabel("Enter your

More information

Part I: Learn Common Graphics Components

Part I: Learn Common Graphics Components OOP GUI Components and Event Handling Page 1 Objectives 1. Practice creating and using graphical components. 2. Practice adding Event Listeners to handle the events and do something. 3. Learn how to connect

More information

Add Subtract Multiply Divide

Add Subtract Multiply Divide ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient

More information

Critical Thinking Assignment #6: Java Program #6 of 6 (70 Points)

Critical Thinking Assignment #6: Java Program #6 of 6 (70 Points) Critical Thinking Assignment #6: Java Program #6 of 6 (70 Points) Java Interactive GUI Application for Number Guessing with Colored Hints (based on Module 7 material) 1) Develop a Java application that

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

3 The L oop Control Structure

3 The L oop Control Structure 3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement

More information

AWT COLOR CLASS. Introduction. Class declaration. Field

AWT COLOR CLASS. Introduction. Class declaration. Field http://www.tutorialspoint.com/awt/awt_color.htm AWT COLOR CLASS Copyright tutorialspoint.com Introduction The Color class states colors in the default srgb color space or colors in arbitrary color spaces

More information

CS Problem Solving and Object-Oriented Programming

CS Problem Solving and Object-Oriented Programming CS 101 - Problem Solving and Object-Oriented Programming Lab 5 - Draw a Penguin Due: October 28/29 Pre-lab Preparation Before coming to lab, you are expected to have: Read Bruce chapters 1-3 Introduction

More information

Lesson 9: Decimal Expansions of Fractions, Part 1

Lesson 9: Decimal Expansions of Fractions, Part 1 Classwork Opening Exercises 1 2 1. a. We know that the fraction can be written as a finite decimal because its denominator is a product of 2 s. Which power of 10 will allow us to easily write the fraction

More information

Loops. In Example 1, we have a Person class, that counts the number of Person objects constructed.

Loops. In Example 1, we have a Person class, that counts the number of Person objects constructed. Loops Introduction In this article from my free Java 8 course, I will discuss the use of loops in Java. Loops allow the program to execute repetitive tasks or iterate over vast amounts of data quickly.

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Repetition, Looping CS101

Repetition, Looping CS101 Repetition, Looping CS101 Last time we looked at how to use if-then statements to control the flow of a program. In this section we will look at different ways to repeat blocks of statements. Such repetitions

More information

Building Java Programs

Building Java Programs Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2 Objects (briefly) object: An entity that contains data and behavior. data: variables inside the object behavior: methods inside

More information

Programming for Engineers Iteration

Programming for Engineers Iteration Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers

More information

Garfield AP CS. Graphics

Garfield AP CS. Graphics Garfield AP CS Graphics Assignment 3 Working in pairs Conditions: I set pairs, you have to show me a design before you code You have until tomorrow morning to tell me if you want to work alone Cumulative

More information

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1 COMP 202 Recursion CONTENTS: Recursion COMP 202 - Recursion 1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself COMP 202 - Recursion

More information

Day 2: Review, ICS3U0, June 2017

Day 2: Review, ICS3U0, June 2017 Day 2: Review, ICS3U0, June 2017 Bubble in your answers as directed. Use a pencil. Put your name on the scantron. Multiple Choice. Select the best answer. Applets 1. After you click a button, e.getactioncommand()

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Building Java Programs

Building Java Programs Building Java Programs Supplement 3G: Graphics 1 drawing 2D graphics Chapter outline DrawingPanel and Graphics objects drawing and filling shapes coordinate system colors drawing with loops drawing with

More information

STUDENT LESSON A5 Designing and Using Classes

STUDENT LESSON A5 Designing and Using Classes STUDENT LESSON A5 Designing and Using Classes 1 STUDENT LESSON A5 Designing and Using Classes INTRODUCTION: This lesson discusses how to design your own classes. This can be the most challenging part of

More information

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers? Programming in C 1 Example 1 What if we want to process three different pairs of integers? 2 Example 2 One solution is to copy and paste the necessary lines of code. Consider the following modification:

More information

LOOPS. Repetition using the while statement

LOOPS. Repetition using the while statement 1 LOOPS Loops are an extremely useful feature in any programming language. They allow you to direct the computer to execute certain statements more than once. In Python, there are two kinds of loops: while

More information

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

More information

Control Statements: Part 1

Control Statements: Part 1 4 Let s all move one place on. Lewis Carroll Control Statements: Part 1 The wheel is come full circle. William Shakespeare How many apples fell on Newton s head before he took the hint! Robert Frost All

More information

CS Fall Homework 5 p. 1. CS Homework 5

CS Fall Homework 5 p. 1. CS Homework 5 CS 235 - Fall 2015 - Homework 5 p. 1 Deadline: CS 235 - Homework 5 Due by 11:59 pm on Wednesday, September 30, 2015. How to submit: Submit your files using ~st10/235submit on nrs-projects, with a homework

More information

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

[Page 177 (continued)] a. if ( age >= 65 ); cout << Age is greater than or equal to 65 << endl; else cout << Age is less than 65 << endl; Page 1 of 10 [Page 177 (continued)] Exercises 4.11 Identify and correct the error(s) in each of the following: a. if ( age >= 65 ); cout

More information

Problem Solving With Loops

Problem Solving With Loops To appreciate the value of loops, take a look at the following example. This program will calculate the average of 10 numbers input by the user. Without a loop, the three lines of code that prompt the

More information

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016 COMP-202: Foundations of Programming Lecture 6: Conditionals Jackie Cheung, Winter 2016 This Lecture Finish data types and order of operations Conditionals 2 Review Questions What is the difference between

More information

CS110D: PROGRAMMING LANGUAGE I

CS110D: PROGRAMMING LANGUAGE I CS110D: PROGRAMMING LANGUAGE I Computer Science department Lecture 5&6: Loops Lecture Contents Why loops?? While loops for loops do while loops Nested control structures Motivation Suppose that you need

More information

Building Java Programs

Building Java Programs Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2 Objects (briefly) object: An entity that contains data and behavior. data: Variables inside the object. behavior: Methods inside

More information

A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common.

A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common. Chapter 6 Class Action In Chapter 1, we explained that Java uses the word class to refer to A set, collection, group, or configuration containing members regarded as having certain attributes or traits

More information

Logic & program control part 2: Simple selection structures

Logic & program control part 2: Simple selection structures Logic & program control part 2: Simple selection structures Summary of logical expressions in Java boolean expression means an expression whose value is true or false An expression is any valid combination

More information

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

Part 1. Summary of For Loops and While Loops

Part 1. Summary of For Loops and While Loops NAME EET 2259 Lab 5 Loops OBJECTIVES -Understand when to use a For Loop and when to use a While Loop. -Write LabVIEW programs using each kind of loop. -Write LabVIEW programs with one loop inside another.

More information

Exercise NMCGJ: Animated Graphics

Exercise NMCGJ: Animated Graphics Exercise NMCGJ: Animated Graphics Animation is a rapid display of a sequence of graphics or images which creates an illusion of movement. Animation is an important feature in game programming. In this

More information

(Refer Slide Time: 00:26)

(Refer Slide Time: 00:26) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute Technology, Madras Module 07 Lecture 07 Contents Repetitive statements

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 02 / 15 / 2016 Instructor: Michael Eckmann Questions? Comments? Loops to repeat code while loops for loops do while loops Today s Topics Logical operators Example

More information

5. Introduction to Procedures

5. Introduction to Procedures 5. Introduction to Procedures Topics: The module SimpleGraphics Creating and Showing figures Drawing Rectangles, Disks, and Stars Optional arguments Application Scripts Procedures We continue our introduction

More information

ICS3C0 Day 2 Multiple Choice Exam Review Please answer on the scantron card.

ICS3C0 Day 2 Multiple Choice Exam Review Please answer on the scantron card. ICS3C0 Day 2 Multiple Choice Exam Review Please answer on the scantron card. 1. This screen flow diagram is used to design a checkers game. Which statement is true? (a) There are 6 screens. (b) The first

More information

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0 VISUAL GUIDE to RX Scripting for Roulette Xtreme - System Designer 2.0 L J Howell UX Software 2009 Ver. 1.0 TABLE OF CONTENTS INTRODUCTION...ii What is this book about?... iii How to use this book... iii

More information

IT150/IT152 Concepts Summary Sheet

IT150/IT152 Concepts Summary Sheet (Examples within this study guide/summary sheet are given in C#.) Variables All data in a computer program, whether calculated during runtime or entered by the user, must be stored somewhere in the memory

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:

More information

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..

More information

Initial Coding Guidelines

Initial Coding Guidelines Initial Coding Guidelines ITK 168 (Lim) This handout specifies coding guidelines for programs in ITK 168. You are expected to follow these guidelines precisely for all lecture programs, and for lab programs.

More information

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018. Week 2 Branching & Looping Gaddis: Chapters 4 & 5 CS 5301 Spring 2018 Jill Seaman 1 Relational Operators l relational operators (result is bool): == Equal to (do not use =)!= Not equal to > Greater than

More information

Graphics and Java 2D Introduction OBJECTIVES. One picture is worth ten thousand words.

Graphics and Java 2D Introduction OBJECTIVES. One picture is worth ten thousand words. 1 2 12 Graphics and Java 2D One picture is worth ten thousand words. Chinese proverb Treat nature in terms of the cylinder, the sphere, the cone, all in perspective. Paul Cézanne Colors, like features,

More information

Adding Buttons to StyleOptions.java

Adding Buttons to StyleOptions.java Adding Buttons to StyleOptions.java The files StyleOptions.java and StyleOptionsPanel.java are from Listings 5.14 and 5.15 of the text (with a couple of slight changes an instance variable fontsize is

More information

Introduction to the Java Basics: Control Flow Statements

Introduction to the Java Basics: Control Flow Statements Lesson 3: Introduction to the Java Basics: Control Flow Statements Repetition Structures THEORY Variable Assignment You can only assign a value to a variable that is consistent with the variable s declared

More information

Chapter 3 Structured Program Development

Chapter 3 Structured Program Development 1 Chapter 3 Structured Program Development Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 3 - Structured Program Development Outline 3.1 Introduction

More information

Flow Control. CSC215 Lecture

Flow Control. CSC215 Lecture Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)

More information

CSE 114 Computer Science I

CSE 114 Computer Science I CSE 114 Computer Science I Iteration Cape Breton, Nova Scotia What is Iteration? Repeating a set of instructions a specified number of times or until a specific result is achieved How do we repeat steps?

More information

Object Oriented Programming with Java

Object Oriented Programming with Java Object Oriented Programming with Java What is Object Oriented Programming? Object Oriented Programming consists of creating outline structures that are easily reused over and over again. There are four

More information

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester Programming Language Control Structures: Repetition (while) Eng. Anis Nazer Second Semester 2017-2018 Repetition statements Control statements change the order which statements are executed Selection :

More information

Word Processing Packet. Do NOT write in this packet!

Word Processing Packet. Do NOT write in this packet! Word Processing Packet Do NOT write in this packet! Word Processing Assignment #1 1. Open the FONT file (Remember that you have to go to Network Applications and click on CTE Intro Files) 2. Follow the

More information

Accelerating Information Technology Innovation

Accelerating Information Technology Innovation Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lección 03 Control Structures Agenda 1. Block Statements 2. Decision Statements 3. Loops 2 What are Control

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All for repetition statement do while repetition statement switch multiple-selection statement break statement continue statement Logical

More information

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos. Lecture 05: Methods AITI Nigeria Summer 2012 University of Lagos. Agenda What a method is Why we use methods How to declare a method The four parts of a method How to use (invoke) a method The purpose

More information

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Things to Review Review the Class Slides: Key Things to Take Away Do you understand

More information

Java Outline (Upto Exam 2)

Java Outline (Upto Exam 2) Java Outline (Upto Exam 2) Part 4 IF s (Branches) and Loops Chapter 12/13 (The if Statement) Hand in Program Assignment#1 (12 marks): Create a program called Ifs that will do the following: 1. Ask the

More information

Programming for Electrical and Computer Engineers. Loops

Programming for Electrical and Computer Engineers. Loops Programming for Electrical and Computer Engineers Loops Dr. D. J. Jackson Lecture 6-1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly

More information

Example 3-1. Password Validation

Example 3-1. Password Validation Java Swing Controls 3-33 Example 3-1 Password Validation Start a new empty project in JCreator. Name the project PasswordProject. Add a blank Java file named Password. The idea of this project is to ask

More information

Using Graphics. Building Java Programs Supplement 3G

Using Graphics. Building Java Programs Supplement 3G Using Graphics Building Java Programs Supplement 3G Introduction So far, you have learned how to: output to the console break classes/programs into static methods store and use data with variables write

More information

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M Control Structures Intro. Sequential execution Statements are normally executed one

More information

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I AL GHURAIR UNIVERSITY College of Computing CSC 209 JAVA I week 3- Control Statements: Part I Objectives: To use the if and if...else selection statements to choose among alternative actions. To use the

More information

Chapter 4 Lab. Loops and Files. Objectives. Introduction

Chapter 4 Lab. Loops and Files. Objectives. Introduction Chapter 4 Lab Loops and Files Objectives Be able to convert an algorithm using control structures into Java Be able to write a while loop Be able to write a do-while loop Be able to write a for loop Be

More information

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops. Chapter 6 Loops 1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C, every loop

More information

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when

More information

Topic 8 graphics. -mgrimes, Graphics problem report 134

Topic 8 graphics. -mgrimes, Graphics problem report 134 Topic 8 graphics "What makes the situation worse is that the highest level CS course I've ever taken is cs4, and quotes from the graphics group startup readme like 'these paths are abstracted as being

More information

Mr. Monroe s Guide to Mastering Java Syntax

Mr. Monroe s Guide to Mastering Java Syntax Mr. Monroe s Guide to Mastering Java Syntax Getting Started with Java 1. Download and install the official JDK (Java Development Kit). 2. Download an IDE (Integrated Development Environment), like BlueJ.

More information

Why Is Repetition Needed?

Why Is Repetition Needed? Why Is Repetition Needed? Repetition allows efficient use of variables. It lets you process many values using a small number of variables. For example, to add five numbers: Inefficient way: Declare a variable

More information

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition) C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures

More information

Java Coding 3. Over & over again!

Java Coding 3. Over & over again! Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java

More information

COMP 202 Java in one week

COMP 202 Java in one week CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language

More information

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. 9 Self-Test Exercises p. 11 History Note p. 12 Programming and

More information

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18 Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical

More information

Chapter 4 Introduction to Control Statements

Chapter 4 Introduction to Control Statements Introduction to Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives 2 How do you use the increment and decrement operators? What are the standard math methods?

More information

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 9/e Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Copyright 1992-2012 by Pearson Copyright 1992-2012 by Pearson Before writing a program to solve a problem, have

More information

IT 1033: Fundamentals of Programming Loops

IT 1033: Fundamentals of Programming Loops IT 1033: Fundamentals of Programming Loops Budditha Hettige Department of Computer Science Repetitions: Loops A loop is a sequence of instruction s that is continually repeated until a certain condition

More information

More About WHILE Loops

More About WHILE Loops More About WHILE Loops http://people.sc.fsu.edu/ jburkardt/isc/week04 lecture 07.pdf... ISC3313: Introduction to Scientific Computing with C++ Summer Semester 2011... John Burkardt Department of Scientific

More information

How to approach a computational problem

How to approach a computational problem How to approach a computational problem A lot of people find computer programming difficult, especially when they first get started with it. Sometimes the problems are problems specifically related to

More information

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6 Repetition Statements Animated Version required for reproduction or display. Chapter 6-1 Objectives After you have read and studied this chapter, you should be able to Implement repetition control

More information

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Graphics. Lecture 18 COP 3252 Summer June 6, 2017 Graphics Lecture 18 COP 3252 Summer 2017 June 6, 2017 Graphics classes In the original version of Java, graphics components were in the AWT library (Abstract Windows Toolkit) Was okay for developing simple

More information

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions. Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

More information

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is

More information

Loops. CSE 114, Computer Science 1 Stony Brook University

Loops. CSE 114, Computer Science 1 Stony Brook University Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?

More information

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014 Lesson 6A Loops By John B. Owen All rights reserved 2011, revised 2014 Topic List Objectives Loop structure 4 parts Three loop styles Example of a while loop Example of a do while loop Comparison while

More information

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name: CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : In mathematics,

More information

Admin. CS 112 Introduction to Programming. Counting Down: Code Puzzle. Counting Down: Code Puzzle

Admin. CS 112 Introduction to Programming. Counting Down: Code Puzzle. Counting Down: Code Puzzle Admin CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

Brand Guidelines 2012

Brand Guidelines 2012 Brand Guidelines 2012 Contents Introduction 3 General Guidelines 4 Proportions 5 Variations of the SendGrid logo 6 Protected Area 8 Minimum Size 9 Unacceptable Usage 10 Primary Corporate Colors 12 Secondary

More information