Chapter 4 Control Structures: Part 2
|
|
- Horace Cummings
- 5 years ago
- Views:
Transcription
1 Chapter 4 Control Structures: Part 2 1
2 2 Essentia ls of Counter-Controlled Repetition Counter-controlled repetition requires: Control variable (loop counter) Initial value of the control variable Increment/decrement of control variable through each loop Condition that tests for the final value of the control variable 2003 Prentice Hall, Inc. All rights reserved.
3 1 // Fig. 5.1: WhileCounter.java 2 // Counter-controlled repetition. 3 import java.awt.graphics; 4 5 import javax.swing.japplet; 6 7 public class WhileCounter extends JApplet { Control-variable Condition name is counter tests for counter s final value Control-variable initial value is // draw lines on applet s background 10 public void paint( Graphics g ) 11 { 12 super.paint( g ); // call paint method inherited from JApplet int counter = 1; // initialization while ( counter <= 10 ) { // repetition condition 17 g.drawline( 10, 10, 250, counter * 10 ); 18 ++counter; // increment } // end while } // end method paint } // end class WhileCounter Outline WhileCounter.ja va Line 14 Line 16 Increment Line for counter Prentice Hall, Inc. All rights reserved.
4 for Repetition Statement 4 import java.awt.graphics; import javax.swing.japplet; public class ForCounter extends JApplet { // draw lines on applet s background public void paint( Graphics g ) { super.paint( g ); // call paint method inherited from Japplet for ( int counter = 1; counter <= 10; counter++ ) g.drawline( 10, 10, 250, counter * 10 ); } }
5 5 for keyword Control variable Required semicolon separator Final value of control variable for which the condition is true Required semicolon separator for ( int counter = 1; counter <= 10; counter++ ) Initial value of control variable Loop-continuation condition Increment of control variable for statement header components Prentice Hall, Inc. All rights reserved.
6 for Repetition Structure (cont.) 6 for ( initialization; loopcontinuationcondition; increment ) statement; can usually be rewritten as: initialization; while ( loopcontinuationcondition ) { statement; increment; } 2003 Prentice Hall, Inc. All rights reserved.
7 7 Establish initial value of control variable int counter = 1 [counter > 10] [counter <= 10] Draw a line on the applet Increment the control variable Determine whether the final value of control variable has been reached g.drawline( 10, 10, 250, counter * 10 ); counter++ for statement activity diagram Prentice Hall, Inc. All rights reserved.
8 8 4.4 Exa mples Using the for Sta tement 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 control 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 ) 2003 Prentice Hall, Inc. All rights reserved.
9 1 // Fig. 5.5: Sum.java 2 // Summing integers with the for statement. 3 import javax.swing.joptionpane; 4 5 public class Sum { 6 7 public static void main( String args[] ) 8 { 9 int total = 0; // initialize sum // total even integers from 2 through for ( int number = 2; number <= 100; number += 2 ) 13 total += number; // display results 16 JOptionPane.showMessageDialog( null, "The sum is " + total, 17 "Total Even Integers from 2 to 100", 18 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end main } // end class Sum increment number by 2 each iteration Outline Sum.java Line Prentice Hall, Inc. All rights reserved.
10 10 do while Repetition Sta tement do while structure Similar to while structure Tests loop-continuation after performing body of loop i.e., loop body always executes at least once 2003 Prentice Hall, Inc. All rights reserved.
11 1 // Fig. 5.7: DoWhileTest.java 2 // Using the do...while statement. 3 import java.awt.graphics; 4 5 import javax.swing.japplet; 6 Oval is drawn before testing 7 public class DoWhileTest extends JApplet { 8 counter s final value 9 // draw lines on applet 10 public void paint( Graphics g ) 11 { 12 super.paint( g ); // call paint method inherited from JApplet int counter = 1; // initialize counter do { 17 g.drawoval( counter * 10, counter * 10, 18 counter * 20, counter * 20 ); 19 ++counter; 20 } while ( counter <= 10 ); // end do...while } // end method paint } // end class DoWhileTest Outline 11 DoWhileTest.jav a Lines Prentice Hall, Inc. All rights reserved.
12 12 action state condition [true] [false] Fig. 5.8 do while repetition sta tement a ctivity dia gra m Prentice Hall, Inc. All rights reserved.
13 13 switch Multiple-Selection Sta tement switch statement Used for multiple selections 2003 Prentice Hall, Inc. All rights reserved.
14 1 // Fig. 5.9: SwitchTest.java 2 // Drawing lines, rectangles or ovals based on user input. 3 import java.awt.graphics; 4 5 import javax.swing.*; 6 7 public class SwitchTest extends JApplet { 8 int choice; // user's choice of which shape to draw 9 10 // initialize applet by obtaining user's choice 11 public void init() 12 { 13 String input; // user's input Get user s input in JApplet // obtain user's choice 16 input = JOptionPane.showInputDialog( 17 "Enter 1 to draw lines\n" + 18 "Enter 2 to draw rectangles\n" + 19 "Enter 3 to draw ovals\n" ); choice = Integer.parseInt( input ); // convert input to int } // end method init // draw shapes on applet's background 26 public void paint( Graphics g ) 27 { 28 super.paint( g ); // call paint method inherited from JApplet for ( int i = 0; i < 10; i++ ) { // loop 10 times (0-9) 31 Outline 2003 Prentice Hall, Inc. All rights reserved. 14 SwitchTest.java Lines 16-21: Getting user s input
15 32 switch ( choice ) { // determine shape to draw case 1: // draw a line 35 g.drawline( 10, 10, 250, 10 + i * 10 ); 36 break; // done processing case case 2: // draw a rectangle 39 g.drawrect( 10 + i * 10, 10 + i * 10, i * 10, 50 + i * 10 ); 41 break; // done processing case case 3: // draw an oval 44 g.drawoval( 10 + i * 10, 10 + i * 10, i * 10, 50 + i * 10 ); 46 break; // done processing case default: // draw string indicating invalid value entered 49 g.drawstring( "Invalid value entered", 50 10, 20 + i * 15 ); } // end switch } // end for } // end method paint } // end class SwitchTest Outline user input (choice) is switch controlling statement expression determines which case label to execute, SwitchTest.java depending on controlling expression Line 32: controlling expression Line 32: switch statement Line 48 default case for invalid entries Prentice Hall, Inc. All rights reserved.
16 Outline 16 SwitchTest.java 2003 Prentice Hall, Inc. All rights reserved.
17 Outline 17 SwitchTest.java 2003 Prentice Hall, Inc. All rights reserved.
18 18 case a [fa lse] [true] case a ac tion(s) break case b [fa lse] [true] case b ac tion(s) break. [true] case z [fa lse] case z ac tion(s) break default ac tion(s) Fig switch multiple-selection sta tement a ctivity dia gra m with break sta tements Prentice Hall, Inc. All rights reserved.
19 19 break a nd continue Sta tements 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 2003 Prentice Hall, Inc. All rights reserved.
20 1 // Fig. 5.11: BreakTest.java 2 // Terminating a loop with break. 3 import javax.swing.joptionpane; 4 5 public class BreakTest { 6 7 public static void main( String args[] ) 8 { 9 String output = ""; 10 int count; for ( count = 1; count <= 10; count++ ) { // loop 10 times if ( count == 5 ) // if count is 5, 15 break; // terminate loop output += count + " "; } // end for output += "\nbroke out of loop at count = " + count; 22 JOptionPane.showMessageDialog( null, output ); System.exit( 0 ); // terminate application } // end main } // end class BreakTest Outline BreakTest.java exit for structure (break) Line 12 when count equals 5 Loop 10 times Lines Prentice Hall, Inc. All rights reserved.
21 1 // Fig. 5.12: ContinueTest.java 2 // Continuing with the next iteration of a loop. 3 import javax.swing.joptionpane; 4 5 public class ContinueTest { 6 7 public static void main( String args[] ) 8 { 9 String output = ""; for ( int count = 1; count <= 10; count++ ) { // loop 10 times if ( count == 5 ) // if count is 5, 14 continue; // skip remaining code in loop output += count + " "; } // end for output += "\nused continue to skip printing 5"; 21 JOptionPane.showMessageDialog( null, output ); System.exit( 0 ); // terminate application } // end main } // end class ContinueTest Outline ContinueTest.ja Skip line 16 and proceed va to line 11 when count equals 5 Loop 10 times Line 11 Lines Prentice Hall, Inc. All rights reserved.
22 22 Labeled break and continue Statements Labeled block Set of statements enclosed by {} Preceded by a label Labeled break statement Exit from nested control structures Proceeds to end of specified labeled block Labeled continue statement Skips remaining statements in nested-loop body Proceeds to beginning of specified labeled block 2003 Prentice Hall, Inc. All rights reserved.
23 1 // Fig. 5.13: BreakLabelTest.java 2 // Labeled break statement. 3 import javax.swing.joptionpane; 4 5 public class BreakLabelTest { 6 stop is the labeled block 7 public static void main( String args[] ) 8 { 9 String output = ""; stop: { // labeled block // count 10 rows 14 for ( int row = 1; row <= 10; row++ ) { // count 5 columns 17 for ( int column = 1; column <= 5 ; column++ ) { if ( row == 5 ) // if row is 5, 20 break stop; // jump to end of stop block output += "* "; } // end inner for output += "\n"; } // end outer for 29 Loop 10 times Nested loop 5 times Exit to line 35 (next slide) Outline 23 BreakLabelTest. java Line 11 Line 14 Line 17 Lines Prentice Hall, Inc. All rights reserved.
24 30 // following line is skipped 31 output += "\nloops terminated normally"; } // end labeled block JOptionPane.showMessageDialog( null, output, 36 "Testing break with a label", 37 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end main } // end class BreakLabelTest Outline 24 BreakLabelTest. java 2003 Prentice Hall, Inc. All rights reserved.
25 1 // Fig. 5.14: ContinueLabelTest.java 2 // Labeled continue statement. 3 import javax.swing.joptionpane; 4 5 public class ContinueLabelTest { 6 nextrow is the labeled block 7 public static void main( String args[] ) 8 { 9 String output = ""; Loop 5 times nextrow: // target label of continue statement // count 5 rows 14 for ( int row = 1; row <= 5; row++ ) { 15 output += "\n"; // count 10 columns per row 18 for ( int column = 1; column <= 10; column++ ) { // if column greater than row, start next row 21 if ( column > row ) 22 continue nextrow; // next iteration of labeled loop output += "* "; } // end inner for } // end outer for Nested loop 10 times Outline 25 ContinueLabelTe st.java Line 11 Line 14 Line 17 Lines continue to line 11 (nextrow) 2003 Prentice Hall, Inc. All rights reserved.
26 29 30 JOptionPane.showMessageDialog( null, output, 31 "Testing continue with a label", 32 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end main } // end class ContinueLabelTest Outline 26 ContinueLabelTe st.java 2003 Prentice Hall, Inc. All rights reserved.
27 27 Logica l Opera tors Logical operators Allows for forming more complex conditions Combines simple conditions Java logical operators && (conditional AND) & (boolean logical AND) (conditional OR) (boolean logical inclusive OR) ^ (boolean logical exclusive OR)! (logical NOT) 2003 Prentice Hall, Inc. All rights reserved.
28 28 expression1 expression2 expression1 && expression2 false false false false true false true false false true true true Fig && (conditional AND) operator truth table. expression1 expression2 expression1 expression2 false false false false true true true false true true true true Fig (conditional OR) operator truth table Prentice Hall, Inc. All rights reserved.
29 29 expression1 expression2 expression1 ^ expression2 false false false false true true true false true true true false Fig ^ (boolean logical exclusive OR) operator truth table. expression!expression false true true false Fig. 5.18! (logical negation, or logical NOT) operator truth table Prentice Hall, Inc. All rights reserved.
30 1 // Fig. 5.19: LogicalOperators.java 2 // Logical operators. 3 import javax.swing.*; 4 5 public class LogicalOperators 6 7 public static void main( String args[] ) 8 { 9 // create JTextArea to display results 10 JTextArea outputarea = new JTextArea( 17, 20 ); // attach JTextArea to a JScrollPane so user can scroll results 13 JScrollPane scroller = new JScrollPane( outputarea ); // create truth table for && (conditional AND) operator 16 String output = "Logical AND (&&)" + 17 "\nfalse && false: " + ( false && false ) + 18 "\nfalse && true: " + ( false && true ) + 19 "\ntrue && false: " + ( true && false ) + 20 "\ntrue && true: " + ( true && true ); // create truth table for (conditional OR) operator 23 output += "\n\nlogical OR ( )" + 24 "\nfalse false: " + ( false false ) + 25 "\nfalse true: " + ( false true ) + 26 "\ntrue false: " + ( true false ) + 27 "\ntrue true: " + ( true true ); 28 Conditional AND truth table Outline 30 LogicalOperator s.java Lines Lines Conditional OR truth table 2003 Prentice Hall, Inc. All rights reserved.
31 29 // create truth table for & (boolean logical AND) operator 30 output += "\n\nboolean logical AND (&)" + 31 "\nfalse & false: " + ( false & false ) + 32 "\nfalse & true: " + ( false & true ) + 33 "\ntrue & false: " + ( true & false ) + 34 "\ntrue & true: " + ( true & true ); 35 Boolean logical AND truth table 36 // create truth table for (boolean logical inclusive OR) operator 37 output += "\n\nboolean logical inclusive OR ( )" + 38 "\nfalse false: " + ( false false ) + 39 "\nfalse true: " + ( false true ) + 40 "\ntrue false: " + ( true false ) + 41 "\ntrue true: " + ( true true ); // create truth table for ^ (boolean logical exclusive OR) operator 44 output += "\n\nboolean logical exclusive OR (^)" + 45 "\nfalse ^ false: " + ( false ^ false ) + 46 "\nfalse ^ true: " + ( false ^ true ) + 47 "\ntrue ^ false: " + ( true ^ false ) + 48 "\ntrue ^ true: " + ( true ^ true ); // create truth table for! (logical negation) operator 51 output += "\n\nlogical NOT (!)" + 52 "\n!false: " + (!false ) + 53 "\n!true: " + (!true ); outputarea.settext( output ); // place results in JTextArea 56 Outline 31 LogicalOperator s.java Lines Lines Boolean logical inclusive Lines OR truth table Lines Boolean logical exclusive OR truth table Logical NOT truth table 2003 Prentice Hall, Inc. All rights reserved.
32 57 JOptionPane.showMessageDialog( null, scroller, 58 "Truth Tables", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end main } // end class LogicalOperators Outline 32 LogicalOperator s.java 2003 Prentice Hall, Inc. All rights reserved.
33 33 Operators Associativity Type right to left unary postfix ! (type) right to left unary * / % left to right multiplicative + - left to right additive < <= > >= left to right relational ==!= left to right equality & left to right boolean logical AND ^ left to right boolean logical exclusive OR left to right boolean logical inclusive OR && left to right conditional AND left to right conditional OR?: right to left conditional = += - = *= /= %= right t o left assignment Fig Precedence/associativity of the operators discussed so far Prentice Hall, Inc. All rights reserved.
Chapter 5 Control Structures: Part 2
Chapter 5 Control Structures: Part 2 Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: Name of control variable (loop counter) Initial value of control variable Increment/decrement
More informationControl Structures (Deitel chapter 4,5)
Control Structures (Deitel chapter 4,5) 1 2 Plan Control Structures ifsingle-selection Statement if else Selection Statement while Repetition Statement for Repetition Statement do while Repetition Statement
More informationControl Statements: Part Pearson Education, Inc. All rights reserved.
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
More informationControl Statements. Musa M. Ameen Computer Engineering Dept.
2 Control Statements Musa M. Ameen Computer Engineering Dept. 1 OBJECTIVES In this chapter you will learn: To use basic problem-solving techniques. To develop algorithms through the process of topdown,
More informationChapter 5 - Methods Prentice Hall, Inc. All rights reserved.
1 Chapter 5 - Methods 2003 Prentice Hall, Inc. All rights reserved. 2 Introduction Modules Small pieces of a problem e.g., divide and conquer Facilitate design, implementation, operation and maintenance
More informationJava Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application
Last Time v We created our first Java application v What are the components of a basic Java application? v What GUI component did we use in the examples? v How do we write to the standard output? v An
More informationArrays (Deitel chapter 7)
Arrays (Deitel chapter 7) Plan Arrays Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods Sorting Arrays Searching Arrays: Linear Search and
More informationChapter 3 - Introduction to Java Applets
1 Chapter 3 - Introduction to Java Applets 2 Introduction Applet Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language)
More informationChapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition
Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition 5.2 Q1: Counter-controlled repetition requires a. A control variable and initial value. b. A control variable
More informationArrays Introduction. Group of contiguous memory locations. Each memory location has same name Each memory location has same type
Array Arrays Introduction Group of contiguous memory locations Each memory location has same name Each memory location has same type Remain same size once created Static entries 1 Name of array (Note that
More informationChapter 4 C Program Control
1 Chapter 4 C Program Control Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 4 C Program Control Outline 4.1 Introduction 4.2 The Essentials of Repetition
More informationAssoc. 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 informationCourse PJL. Arithmetic Operations
Outline Oman College of Management and Technology Course 503200 PJL Handout 5 Arithmetic Operations CS/MIS Department 1 // Fig. 2.9: Addition.java 2 // Addition program that displays the sum of two numbers.
More informationChapter 6 - Methods Prentice Hall. All rights reserved.
Chapter 6 - Methods 1 Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math Class Methods 6.4 Methods 6.5 Method Definitions 6.6 Argument Promotion 6.7 Java API Packages 6.8 Random-Number Generation
More informationSequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;
Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The
More informationChapter 6 Arrays and Strings Prentice Hall, Inc. All rights reserved.
1 Chapter 6 Arrays and Strings Introduction 2 Arrays Data structures Related data items of same type Reference type Remain same size once created Fixed-length entries 3 Name of array (note that all elements
More informationCopyright 1999 by Deitel & Associates, Inc. All Rights Reserved.
CHAPTER 2 1 2 1 // Fig. 2.1: Welcome1.java 2 // A first program in Java 3 4 public class Welcome1 { 5 public static void main( String args[] ) 6 { 7 System.out.println( "Welcome to Java Programming!" );
More informationStructured Programming. Dr. Mohamed Khedr Lecture 9
Structured Programming Dr. Mohamed Khedr http://webmail.aast.edu/~khedr 1 Two Types of Loops count controlled loops repeat a specified number of times event-controlled loops some condition within the loop
More information2.11 Assignment Operators. Assignment expression abbreviations c = c + 3; can be abbreviated as c += 3; using the addition assignment operator
2.11 Assignment Operators 1 Assignment expression abbreviations c = c + 3; can be abbreviated as c += 3; using the addition assignment operator Statements of the form variable = variable operator expression;
More informationChapter 6 SINGLE-DIMENSIONAL ARRAYS
Chapter 6 SINGLE-DIMENSIONAL ARRAYS Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk What is an Array? A single array variable can reference
More informationC Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan
C Program Control Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline The for repetition statement switch multiple selection statement break
More informationINTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from
INTRODUCTION TO C++ PROGRAM CONTROL Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Repetition Statement for while do.. while break and continue
More informationLoops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while
More informationControl Statements: Part Pearson Education, Inc. All rights reserved.
1 5 Control Statements: Part 2 2 Not everything that can be counted counts, and not every thing that counts can be counted. Albert Einstein Who can control his fate? William Shakespeare The used key is
More informationReview. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char
Review Primitive Data Types & Variables int, long float, double boolean char String Mathematical operators: + - * / % Comparison: < > = == 1 1.3 Conditionals and Loops Introduction to Programming in
More informationLoops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017
Loops! Loops! Loops! Lecture 5 COP 3014 Fall 2017 September 25, 2017 Repetition Statements Repetition statements are called loops, and are used to repeat the same code mulitple times in succession. The
More informationTWO-DIMENSIONAL FIGURES
TWO-DIMENSIONAL FIGURES Two-dimensional (D) figures can be rendered by a graphics context. Here are the Graphics methods for drawing draw common figures: java.awt.graphics Methods to Draw Lines, Rectangles
More informationChapter 4 C Program Control
Chapter C Program Control 1 Introduction 2 he Essentials of Repetition 3 Counter-Controlled Repetition he for Repetition Statement 5 he for Statement: Notes and Observations 6 Examples Using the for Statement
More informationIntroduction. C provides two styles of flow control:
Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if
More informationMethods (Deitel chapter 6)
Methods (Deitel chapter 6) 1 Plan 2 Introduction Program Modules in Java Math-Class Methods Method Declarations Argument Promotion Java API Packages Random-Number Generation Scope of Declarations Methods
More informationCT 229 Arrays in Java
CT 229 Arrays in Java 27/10/2006 CT229 Next Weeks Lecture Cancelled Lectures on Friday 3 rd of Nov Cancelled Lab and Tutorials go ahead as normal Lectures will resume on Friday the 10 th of Nov 27/10/2006
More informationChapter 9 - JavaScript: Control Structures II
Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled Repetition 9.3 for Repetition Structure 9. Examples Using the for Structure 9.5 switch Multiple-Selection
More informationPart 8 Methods. scope of declarations method overriding method overloading recursions
Part 8 Methods scope of declarations method overriding method overloading recursions Modules Small pieces of a problem e.g., divide and conquer 6.1 Introduction Facilitate design, implementation, operation
More informationObject oriented programming. Instructor: Masoud Asghari Web page: Ch: 3
Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning
More informationREPETITION 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 informationIn this chapter you will learn:
1 In this chapter you will learn: Essentials of counter-controlled repetition. Use for, while and do while to execute statements in program repeatedly. Use nested control statements in your program. 2
More informationOutline. 7.1 Introduction. 7.2 Arrays. 7.2 Arrays
jhtp5_07.fm Page 279 Wednesday, November 20, 2002 12:44 PM 7 Arrays Objectives To introduce the array data structure. To understand the use of arrays to store, sort and search lists and tables of values.
More informationChapter 2 - Control Structures
Chapter 2 - Control Structures 1 2.11 Assignment Operators 2.12 Increment and Decrement Operators 2.13 Essentials of Counter-Controlled Repetition 2.1 for Repetition Structure 2.15 Examples Using the for
More information204111: Computer and Programming
204111: Computer and Programming Week 4: Control Structures t Monchai Sopitkamon, Ph.D. Overview Types of control structures Using selection structure Using repetition structure Types of control ol structures
More informationC++ Programming Lecture 7 Control Structure I (Repetition) Part I
C++ Programming Lecture 7 Control Structure I (Repetition) Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department while Repetition Structure I Repetition structure Programmer
More informationMethods (Deitel chapter 6)
1 Plan 2 Methods (Deitel chapter ) Introduction Program Modules in Java Math-Class Methods Method Declarations Argument Promotion Java API Packages Random-Number Generation Scope of Declarations Methods
More informationOperators in java Operator operands.
Operators in java Operator in java is a symbol that is used to perform operations and the objects of operation are referred as operands. There are many types of operators in java such as unary operator,
More informationThe first applet we shall build will ask the user how many times the die is to be tossed. The request is made by utilizing a JoptionPane input form:
Lecture 5 In this lecture we shall discuss the technique of constructing user-defined methods in a class. The discussion will be centered about an experiment of tossing a die a specified number of times.
More informationLECTURE 5 Control Structures Part 2
LECTURE 5 Control Structures Part 2 REPETITION STATEMENTS Repetition statements are called loops, and are used to repeat the same code multiple times in succession. The number of repetitions is based on
More informationChapter 10 JavaScript/JScript: Control Structures II 289
IW3HTP_10.fm Page 289 Thursday, April 13, 2000 12:32 PM Chapter 10 JavaScript/JScript: Control Structures II 289 10 JavaScript/JScript: Control Structures II 1
More informationCHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad Outline 1. C++ Iterative Constructs 2. The for Repetition Structure 3. Examples Using the for Structure 4. The while Repetition Structure
More informationThere 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*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN
PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN
More informationChapter 12 Advanced GUIs and Graphics
Chapter 12 Advanced GUIs and Graphics Chapter Objectives Learn about applets Explore the class Graphics Learn about the classfont Explore the classcolor Java Programming: From Problem Analysis to Program
More informationInformation Science 1
Topics covered Information Science 1 Fundamental Programming Constructs (1) Week 11 Terms and concepts from Week 10 Flow of control and conditional statements Selection structures if statement switch statement
More informationJava provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:
JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators
More informationJava Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.
Loop Control There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first,
More informationChapter 4 Loops. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk
Chapter 4 Loops Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk ١ while Loop Flow Chart while (loop-continuation-condition) { // loop-body;
More informationInformation Science 1
Information Science 1 Fundamental Programming Constructs (1) Week 11 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 10 l Flow of control
More informationLecture 7 Tao Wang 1
Lecture 7 Tao Wang 1 Objectives In this chapter, you will learn about: Interactive loop break and continue do-while for loop Common programming errors Scientists, Third Edition 2 while Loops while statement
More informationProgramming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029
Programming Basics and Practice GEDB029 Decision Making, Branching and Looping Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Decision Making and Branching C language possesses such decision-making capabilities
More informationAll 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 informationProgramming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures
Chapter 4: Control Structures I Java Programming: Program Design Including Data Structures Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form
More informationRepe$$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 informationCopyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1
Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1 Chapter 6 : (Control Structure- Repetition) Using Decrement or Increment While Loop Do-While Loop FOR Loop Nested Loop
More informationChapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1
Chapter 5: Loops and Files The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++; is the same as val = val + 1; ++ can be used before (prefix) or after (postfix)
More informationPart 1: Introduction. Course Contents. Goals. Books. Difference between conventional and objectoriented
1 Course Contents 2 Part 1: Introduction Difference between conventional and objectoriented programming Introduction to object-oriented programming with Java lots of Java details aimed at producing and
More informationLooping. Arizona State University 1
Looping CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 5 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University
More informationTheory of control structures
Theory of control structures Paper written by Bohm and Jacopini in 1966 proposed that all programs can be written using 3 types of control structures. Theory of control structures sequential structures
More informationChapter 4: Control structures. Repetition
Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition
More informationAn applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a
CBOP3203 An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page. When you use a Java technology-enabled
More informationECE 122. Engineering Problem Solving with Java
ECE 122 Engineering Problem Solving with Java Lecture 10 For Loops and Arrays Outline Problem: How can I perform the same operations a fixed number of times? Considering for loops Performs same operations
More informationB.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University
Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are
More informationProgramming Constructs Overview. Method Call System.out.print( hello ); Method Parameters
Programming Constructs Overview Method calls More selection statements More assignment operators Conditional operator Unary increment and decrement operators Iteration statements Defining methods 27 October
More informationStructured Program Development in C
1 3 Structured Program Development in C 3.2 Algorithms 2 Computing problems All can be solved by executing a series of actions in a specific order Algorithm: procedure in terms of Actions to be executed
More informationFlow 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 informationWhy 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 informationProblem Solving and 'C' Programming
Problem Solving and 'C' Programming Targeted at: Entry Level Trainees Session 05: Selection and Control Structures 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained herein
More informationControl Structures of C++ Programming (2)
Control Structures of C++ Programming (2) CISC1600/1610 Computer Science I/Lab Fall 2016 CISC 1600 Yanjun Li 1 Loops Purpose: Execute a block of code multiple times (repeat) Types: for, while, do/while
More informationOutline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors
Outline Overview history and advantage how to: program, compile and execute 8 data types 3 types of errors Control statements Selection and repetition statements Classes and methods methods... 2 Oak A
More informationLoops. 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 informationUnit 3 Decision making, Looping and Arrays
Unit 3 Decision making, Looping and Arrays Decision Making During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions.
More informationn Group of statements that are executed repeatedly while some condition remains true
Looping 1 Loops n Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop 2 Example counter 1,
More information5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.
Chapter 5: Looping 5.1 The Increment and Decrement Operators Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Addison-Wesley Pearson Education, Inc. Publishing as Pearson Addison-Wesley
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 5: Control Structures II (Repetition) Why Is Repetition Needed? Repetition allows you to efficiently use variables Can input,
More informationCOP 2000 Introduction to Computer Programming Mid-Term Exam Review
he exam format will be different from the online quizzes. It will be written on the test paper with questions similar to those shown on the following pages. he exam will be closed book, but students can
More informationChapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?
Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct and use countercontrolled, sentinel-controlled,
More informationChapter 4: Control structures
Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition
More informationWeek 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 informationChapter 3 Selection Statements
Chapter 3 Selection Statements 3.1 Introduction Java provides selection statements that let you choose actions with two or more alternative courses. Selection statements use conditions. Conditions are
More informationProgramming 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 informationChapter 7. Additional Control Structures
Chapter 7 Additional Control Structures 1 Chapter 7 Topics Switch Statement for Multi-Way Branching Do-While Statement for Looping For Statement for Looping Using break and continue Statements 2 Chapter
More information3chapter C ONTROL S TATEMENTS. Objectives
3chapter C ONTROL S TATEMENTS Objectives To understand the flow of control in selection and loop statements ( 3.2 3.7). To use Boolean expressions to control selection statements and loop statements (
More informationAdvanced Computer Programming
Hazırlayan Yard. Doç. Dr. Mehmet Fidan WHILE, DO-WHILE and FOR LOOPS Loops are used for executing code blocks repeatedly. Decision of continuing loop is given by boolean expression. If boolean expression
More informationC++ 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 informationModule 4: Decision-making and forming loops
1 Module 4: Decision-making and forming loops 1. Introduction 2. Decision making 2.1. Simple if statement 2.2. The if else Statement 2.3. Nested if Statement 3. The switch case 4. Forming loops 4.1. The
More informationIntroduction 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 informationChapter 5: Loops and Files
Chapter 5: Loops and Files 5.1 The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++; is the same as val = val + 1;
More informationExample: Monte Carlo Simulation 1
Example: Monte Carlo Simulation 1 Write a program which conducts a Monte Carlo simulation to estimate π. 1 See https://en.wikipedia.org/wiki/monte_carlo_method. Zheng-Liang Lu Java Programming 133 / 149
More informationDept. of CSE, IIT KGP
Control Flow: Looping CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of Repeated Execution Loop: Group of
More informationChapter 4. Flow of Control
Chapter 4. Flow of Control Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy http://bi.snu.ac.kr Sequential flow of control
More informationComputer Programming I - Unit 5 Lecture page 1 of 14
page 1 of 14 I. The while Statement while, for, do Loops Note: Loop - a control structure that causes a sequence of statement(s) to be executed repeatedly. The while statement is one of three looping statements
More informationRepe$$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 informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem.
More informationRepetition, Looping. While Loop
Repetition, Looping 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