Java I--Copyright © 2000-2007 Tom Hunter. Chapter 4 Control Structures: Part I.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
1 Outline Chapter 4 Introduction Control Structures if Single-Selection Statement if else Selection Statement while Repetition Statement Assignment Operators.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Introduction to Computers and Programming Lecture 7:
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Looping Yong Choi School of Business CSU, Bakersfield.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Copyright © Texas Education Agency, Computer Programming For Loops.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Chapter 5 Loops.
CPS120: Introduction to Computer Science Decision Making in Programs.
Controlling Execution Dong Shao, Nanjing Unviersity.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Repetition Statements while and do while loops
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Introduction to Computers and Programming Lecture 7:
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 4 Repetition Statements (loops)
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 4 – Control Structures Part 1
Looping and Repetition
Java I.
OPERATORS (2) CSC 111.
LRobot Game.
Chapter 4 - Control Structures: Part 1
Additional control structures
Loops CGS3416 Spring 2019 Lecture 7.
Looping and Repetition
Control Statements:.
Presentation transcript:

Java I--Copyright © Tom Hunter

Chapter 4 Control Structures: Part I

Java I--Copyright © Tom Hunter “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds of operations in a program: perform a sequence of actions, perform a selection between alternative actions, or perform a repetition or iteration of the same action. Sequence, Selection, Iteration Structured Programming

Java I--Copyright © Tom Hunter Sequence: one thing after another Structured Programming task1 task2 task3

Java I--Copyright © Tom Hunter Selection: making choices Structured Programming taskA taskB ? YES NO Structured programming, only one entrance, only one exit.

Java I--Copyright © Tom Hunter Repetition, Part I: doing the same thing again until there’s a reason to stop. Structured Programming taskA TRUE FALSE Do while: maybe won’t ever do taskA even once. “A while loop repeats as long as a condition is true.” expression ?

Java I--Copyright © Tom Hunter Repetition, Part II: doing the same thing again until there’s a reason to stop. Structured Programming taskA ? TRUE FALSE Do until: will always do taskA at least once. “ A Do Until loop repeats as long as a condition is false.”

Java I--Copyright © Tom Hunter Structured Programming Procedural Structured Programming Begin at the top, move to the bottom. Each program unit has only one entrance and only one exit.

Java I--Copyright © Tom Hunter Selection in Java Object Oriented Programming Within a method, procedural code. Simple ‘ if ’ with or without brackets. if( expression ) statement; if( expression ) { statement; }

Java I--Copyright © Tom Hunter Object Oriented Programming Simple ‘ if ’ with or without brackets. if( expression ) statement; if( expression ) { statement; } Within brackets, a “block.” Selection in Java

Java I--Copyright © Tom Hunter Object Oriented Programming Simple ‘ if ’ / ‘ else ’ without brackets. if( expression ) statement; else statement; Without brackets, limit of only one statement per branch. Selection in Java

Java I--Copyright © Tom Hunter w Object Oriented Programming Simple ‘ if ’ / ‘ else ’ with brackets. if( expression ) { statement; } else { statement; } Selection in Java

Java I--Copyright © Tom Hunter Compound ‘if’ / ‘else if’ / ‘else if’ / ‘else’. if( expression ) { statement; } else if( expression ) { statement; } else { statement; } Selection in Java

Java I--Copyright © Tom Hunter Special “Ternary” ? : Operator—shorthand ‘if’ / ‘else’ 1.) expression must evaluate to True or False. 2.) If expression is True, execute the command before the colon. 3.) If expression is False, execute the command after the colon. Do this if expression is True Do this if expression is False Selection in Java System.out.print ( expression ? “True” : “False” )

Java I--Copyright © Tom Hunter while —“the Do While” The Test is First Repetition: while Part I while( expression ) statement;

Java I--Copyright © Tom Hunter while —“the Do While” The Test is First Repetition: while Part I while( expression ) { statement; } The while { “Do While” } is used when you can’t predict exactly how many times your loop will be executed. The while may not be executed even once. It executes the loop while the expression is still true. w

Java I--Copyright © Tom Hunter // DoUntil.java // Even though "c" begins the loop false, // it still executes at least once. public class DoUntil { public static void main( String args[] ) { boolean c = false; do { System.out.println( ”Execute DoUntil at least once " ); } while( c ); System.exit( 0 ); }

Java I--Copyright © Tom Hunter while —“the Do Until” The Test is Last Repetition: while Part II do { statement; } while( expression ) ; w This do/while {“Do Until”} is also used when you can’t predict exactly how many times your loop will be executed. It executes at least once. It executes Until the expression becomes false.

Java I--Copyright © Tom Hunter // WhileTest.java // Since "c" is already false when it reaches the // test, the loop never executes. public class DoWhile { public static void main( String args[] ) { boolean c = false while( c ) { System.out.println( ”Execute DoWhile while c is true" ); } System.exit( 0 ); }

Java I--Copyright © Tom Hunter // SelectionTest.java import javax.swing.*; public class SelectionTest { public static void main( String args[] ) { int b, s; String big, small, out; big = JOptionPane.showInputDialog( "Big Number" ); small = JOptionPane.showInputDialog( "Small Number" ); b = Integer.parseInt( big ); s = Integer.parseInt( small ); out = ( b > s ? "Big was larger" : "Small was larger" ); JOptionPane.showMessageDialog( null, out, "Results", JOptionPane.INFORMATION_MESSAGE); System.exit( 0 ); } Test Ternary Operator

Java I--Copyright © Tom Hunter // DoWhileTest.java import javax.swing.*; public class DoWhileTest { public static void main( String args[] ) { int b = 2, s = 1; String big, small, out = “Big is still Bigger”; while( b > s ) { JOptionPane.showMessageDialog( null, out, "Results", JOptionPane.INFORMATION_MESSAGE); big = JOptionPane.showInputDialog( "Big Number" ); small = JOptionPane.showInputDialog( "Small Number" ); b = Integer.parseInt( big ); s = Integer.parseInt( small ); } System.exit( 0 ); } Test DoWhile

Java I--Copyright © Tom Hunter // DoUntilTest.java import javax.swing.*; public class DoUntilTest { public static void main( String args[] ) { int b = 2, s = 1; // preload variables. String big, small, out = “Big is still Bigger”; do { JOptionPane.showMessageDialog( null, out, "Results", JOptionPane.INFORMATION_MESSAGE); big = JOptionPane.showInputDialog( "Big Number" ); small = JOptionPane.showInputDialog( "Small Number" ); b = Integer.parseInt( big ); s = Integer.parseInt( small ); } while( b > s ) ; System.exit( 0 ); } Test DoUntil

Java I--Copyright © Tom Hunter Repetition: while Loops The majority of applications use the plain while loop. Choose either while loop when you can’t know in advance how many times the loop will be executed. The loop is repeated until it encounters a sentinel value, that announces that the loop has finished.

Java I--Copyright © Tom Hunter Assignment Operators We are already familiar with this statement: int x; x = 15; This means the value 15 is placed into the variable x. We say, “ 15 is assigned to x.” In Java, a single equals sign is the assignment operator.

Java I--Copyright © Tom Hunter Assignment Operators Another common bit of code is this: int x; Declares x as an int. x = 15; After this assignment, x contains 15. x = x + 5; After this assignment, x contains 20. First the addition on the right is done. Then, the result is assigned to the the variable on the left.

Java I--Copyright © Tom Hunter Assignment Operators Java offers a shortcut to the statement below: int x = 15; x = x + 5; After this assignment, x contains 20. x += 5; After this assignment, x contains 20. +=

Java I--Copyright © Tom Hunter Assignment Operators Java offers a shortcut to the statement below: int x = 15; x = x - 5; After this assignment, x contains 10. x -= 5; After this assignment, x contains 10. -=

Java I--Copyright © Tom Hunter Assignment Operators Java offers a shortcut to the statement below: int x = 15; x = x * 5; After this assignment, x contains 75. x *= 5; After this assignment, x contains 75. *=

Java I--Copyright © Tom Hunter Assignment Operators Java offers a shortcut to the statement below: int x = 15; x = x / 5; After this assignment, x contains 3. x /= 5; After this assignment, x contains 3. /=

Java I--Copyright © Tom Hunter Assignment Operators Java offers a shortcut to the statement below: int x = 15; x = x % 5; After this assignment, x contains 0. x % = 5; After this assignment, x contains 0. %=

Java I--Copyright © Tom Hunter Increment/Decrement Operators There is one addition statement in Java that is so common, it gets its own operator. It means, simply, “Add one to the variable.” x++x--

Java I--Copyright © Tom Hunter Increment/Decrement Operators Java offers a shortcut to the statement below: int x = 2; x = x + 1; After this assignment, x contains 3. x++; After this assignment, x contains 3. x++

Java I--Copyright © Tom Hunter Increment/Decrement Operators Java offers a shortcut to the statement below: int x = 2; x = x - 1; After this assignment, x contains 1. x--; After this assignment, x contains 1. x--

Java I--Copyright © Tom Hunter Increment/Decrement Operators You can do a pre-increment, or a post-increment. PrePost ++x;x++; --x;x--; If each of these statements is on a line by itself, there is no difference in the effect of doing a pre- or post- increment.

Java I--Copyright © Tom Hunter Increment/Decrement Operators If each of these statements is on a line by itself, there is no difference in the effect of doing a pre- or post- increment. However, if the variable—which is having the pre- or post-increment applied to it—is used within another statement, your choice of pre- or post-increment can alter your results.

Java I--Copyright © Tom Hunter // PrePostIncrement.java public class PrePostIncrement { public static void main( String args[] ) { int x=0; System.out.println( " Baseline, x = " + x ); System.out.println( "\n Pre-increment = ++x = " + ++x ); System.out.println( "\n After increment, x = " + x ); x = 0; System.out.println( "\n 2nd Baseline, x = " + x ); System.out.println( "\n Post-increment = x++ = " + x++ ); System.out.println( "\n After increment, x = " + x ); System.exit( 0 ); } Baseline, x = 0 Pre-increment = ++x = 1 After increment, x = 1 2nd Baseline, x = 0 Post-increment = x++ = 0 After increment, x = 1 w