Control Statements: Part Pearson Education, Inc. All rights reserved.

Similar documents
Control Structures (Deitel chapter 4,5)

Chapter 5 Control Structures: Part 2

Control Statements. Musa M. Ameen Computer Engineering Dept.

Chapter 4 Control Structures: Part 2

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

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

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

Control Structures of C++ Programming (2)

Control Statements: Part Pearson Education, Inc. All rights reserved.

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

TWO-DIMENSIONAL FIGURES

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Course overview: Introduction to programming concepts

Structured Program Development in C

Introduction. C provides two styles of flow control:

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

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

Object- Oriented Programming: Inheritance

Chapter 4 C Program Control

AP CS Unit 12: Drawing and Mouse Events

Lecture 7 A First Graphic Program And Data Structures & Drawing

Unified Modeling Language (UML)

LECTURE 5 Control Structures Part 2

CS313D: ADVANCED PROGRAMMING LANGUAGE

Introduction to the Java Basics: Control Flow Statements

Designing Classes Part 2

Methods: A Deeper Look Pearson Education, Inc. All rights reserved.

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Appendix F: Java Graphics

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Control Statements: Part Pearson Education, Inc. All rights reserved.

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Appendix F: Java Graphics

CS 106 Introduction to Computer Science I

CS 61B Data Structures and Programming Methodology. June David Sun

Chapter 3 Structured Program Development

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM

AplusBug dude = new AplusBug(); A+ Computer Science -

CSC Java Programming, Fall Java Data Types and Control Constructs

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?

In this chapter you will learn:

Repetition, Looping. While Loop

Chapter 4 Introduction to Control Statements

Programming: You will have 6 files all need to be located in the dir. named PA4:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

PROGRAMMING FUNDAMENTALS

Introduction to Classes and Objects

What is a Class Diagram? A diagram that shows a set of classes, interfaces, and collaborations and their relationships

What is a Class Diagram? Class Diagram. Why do we need Class Diagram? Class - Notation. Class - Semantic 04/11/51

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

The Java language has a wide variety of modifiers, including the following:

Assignment 2. Application Development

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

Dr. Hikmat A. M. AbdelJaber

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

DELHI PUBLIC SCHOOL TAPI

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.


Chapter 4 C Program Control

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Introduction to Programming

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Chapter 3: Inheritance and Polymorphism

Course PJL. Arithmetic Operations

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Why Is Repetition Needed?

Final Examination Semester 2 / Year 2012

COP 2000 Note Framework Chapter 5 - Repetition Page 1

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Visual Basic 2010 How to Program by Pearson Education, Inc. All Rights Reserved.

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 3 - Introduction to Java Applets

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

Object Oriented Design

Chapter 2 - Control Structures

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

public static void main(string[] args) { GTest mywindow = new GTest(); // Title This program creates the following window and makes it visible:

Chapter 2 - Control Structures

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

CT 229 Java Syntax Continued

Prepared by: Shraddha Modi

Computers Programming Course 6. Iulian Năstac

MET 107 Drawing Tool (Shapes) Notes Day 3

204111: Computer and Programming

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved.

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

9. APPLETS AND APPLICATIONS

Transcription:

1 5 Control Statements: Part 2

5.2 Essentials of Counter-Controlled Repetition 2 Counter-controlled repetition requires: Control variable (loop counter) Initial value of the control variable Increment/decrement of control variable through each loop Loop-continuation condition that tests for the final value of the control variable

3 5.3 for Repetition Statement (Cont.) for ( initialization; loopcontinuationcondition; increment ) statement; can usually be rewritten as: initialization; while ( loopcontinuationcondition ) { statement; increment; }

5.4 Examples Using the for Statement 4 Varying control variable in for statement Vary control variable from 1 to 100 in increments of 1 for ( int i = 1; i <= 100; i++ ) Vary co ntrol variable from 100 to 1 in increments of 1 for ( int i = 100; i >= 1; i-- ) Vary control variable from 7 to 77 in increments of 7 for ( int i = 7; i <= 77; i += 7 ) Vary control variable from 20 to 2 in decrements of 2 for ( int i = 20; i >= 2; i -= 2 ) Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 for ( int i = 2; i <= 20; i += 3 ) Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 for ( int i = 99; i >= 0; i -= 11 )

5 5.5 do while Repetition Statement do while structure Similar to while structure Tests loop-continuation after performing body of loop i.e., loop body always executes at least once

6 Fig. 5.8 do...while repetition statement UML activity diagram.

5.6 switch Multiple-Selection Statement 7 switch statement Used for multiple selections I recommend not using it

5.6 switch Multiple-Selection Statement (Cont.) 8 Expression in each case Constant integral expression Combination of integer constants that evaluates to a constant integer value Character constant E.g., A, 7 or $ Constant variable Declared with keyword final

9 5.7 break and continue Statements break/continue Alter flow of control break statement Causes immediate exit from control structure Used in while, for, do while or switch statements continue statement Skips remaining statements in loop body Proceeds to next iteration Used in while, for or do while statements

10 Software Engineering Observation 5.3 Some programmers feel that break and continue violate structured programming. Since the same effects are achievable with structured programming techniques, these programmers do not use break or continue.

11 5.8 Logical Operators Logical operators Allows for forming more complex conditions Combines simple conditions Java logical operators && (conditional AND) (conditional OR) & (boolean logical AND) (boolean logical inclusive OR) ^ (boolean logical exclusive OR)! (logical NOT)

12 5.8 Logical Operators (Cont.) Boolean Logical AND (&) Operator Works identically to && Except & always evaluate both operands Boolean Logical OR ( ) Operator Works identically to Except always evaluate both operands

13 5.9 Structured Programming Summary Sequence structure built-in to Java Selection structure if, if else and switch Repetition structure while, do while and for

14 Fig. 5.20 Java s single-entry/single-exit sequence, selection and repetition statements.

15 Rules for Forming Structured Programs 1 Begin with the simplest activity diagram (Fig. 5.22). 2 Any action state can be replaced by two action states in sequence. 3 Any action state can be replaced by any control statement (sequence of action states, if, if else, switch, while, do while or for). 4 Rules 2 and 3 can be applied as often as you like and in any order. Fig. 5.21 Rules for forming structured programs.

5.10 (Optional) GUI and Graphics Case Study: Drawing Rectangles and Ovals 16 Draw rectangles Method drawrect of Graphics Draw ovals Method drawoval of Graphics

1 // Fig. 5.26: Shapes.java 2 // Demonstrates drawing different shapes. 3 import java.awt.graphics; 4 import javax.swing.jpanel; 5 6 public class Shapes extends JPanel 7 { 8 private int choice; // user's choice of which shape to draw 9 10 // constructor sets the user's choice 11 public Shapes( int userchoice ) 12 { 13 choice = userchoice; 14 } // end Shapes constructor 15 16 // draws a cascade of shapes starting from the top left corner 17 public void paintcomponent( Graphics g ) 18 { 19 super.paintcomponent( g ); 20 Outline Shapes.java (1 of 2) 17 2005 Pearson Education, Inc. All rights reserved.

21 for ( int i = 0; i < 10; i++ ) 22 { 23 // pick the shape based on the user's choice 24 switch ( choice ) 25 { 26 case 1: // draw rectangles 27 g.drawrect( 10 + i * 10, 10 + i * 10, 28 50 + i * 10, 50 + i * 10 ); 29 break; 30 case 2: // draw ovals 31 g.drawoval( 10 + i * 10, 10 + i * 10, 32 50 + i * 10, 50 + i * 10 ); 33 break; 34 } // end switch 35 } // end for 36 } // end method paintcomponent 37 } // end class Shapes Draw rectangles Draw ovals Outline Shapes.java (2 of 2) Lines 27-28 Lines 31-32 18 2005 Pearson Education, Inc. All rights reserved.

1 // Fig. 5.27: ShapesTest.java 2 // Test application that displays class Shapes. 3 import javax.swing.jframe; 4 import javax.swing.joptionpane; 5 6 public class ShapesTest 7 { 8 public static void main( String args[] ) 9 { 10 // obtain user's choice 11 String input = JOptionPane.showInputDialog( 12 "Enter 1 to draw rectangles\n" + 13 "Enter 2 to draw ovals" ); 14 15 int choice = Integer.parseInt( input ); // convert input to int 16 17 // create the panel with the user's input 18 Shapes panel = new Shapes( choice ); 19 20 JFrame application = new JFrame(); // creates a new JFrame 21 22 application.setdefaultcloseoperation( JFrame.EXIT_ON_CLOSE ); 23 application.add( panel ); // add the panel to the frame 24 application.setsize( 300, 300 ); // set the desired size 25 application.setvisible( true ); // show the frame 26 } // end main 27 } // end class ShapesTest Outline ShapesTest.java (1 of 2) 19 2005 Pearson Education, Inc. All rights reserved.

Outline 20 ShapesTest.java (2 of 2) Program output 2005 Pearson Education, Inc. All rights reserved.

5.11 (Optional) Software Engineering Case Study: Identifying Object s State and Activities 21 State Machine Diagrams Commonly called state diagram Model several states of an object Show under what circumstances the object changes state Focus on system behavior UML representation State Rounded rectangle Initial state Solid circle Transitions Arrows with stick arrowheads

22 Fig. 5.29 State diagram for the ATM object.

23 Software Engineering Observation 5.5 Software designers do not generally create state diagrams showing every possible state and state transition for all attributes there are simply too many of them. State diagrams typically show only key states and state transitions.

5.11 (Optional) Software Engineering Case Study (Cont.) 24 Activity Diagrams Focus on system behavior Model an object s workflow during program execution Model the actions the object will perform and in what order UML representation Action state ( rectangle with its left and right sides replaced by arcs curving outwards) Action order ( arrow with a stick arrowhead) Initial state (solid circle) Final state (solid circle enclosed in an open circle)