Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

CS0007: Introduction to Computer Programming
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
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.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 7.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Chapter 3- Flow Control. Overview n Why flow control n Branches vs. Loops n Branch statements n Loop Statements n Complex loops n Boolean variables n.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Java Prepared by Gary Langner Types of Loops u for loop (entrance controlled) –do an action for a definite number of times u while loop (entrance controlled.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Control statements Mostafa Abdallah
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
5 Copyright © 2004, Oracle. All rights reserved. Controlling Program Flow.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Chapter 4: Control Structures I
The switch Statement, and Introduction to Looping
Chapter 5: Control Structures II
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 4: Control Structures I
Outline Altering flow of control Boolean expressions
Selection (if-then-else)
Lab5 PROGRAMMING 1 Loop chapter4.
CprE 185: Intro to Problem Solving (using C)
CprE 185: Intro to Problem Solving (using C)
Problem 1 Given n, calculate 2n
Presentation transcript:

Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days = 31; } System.out.println ( “Month” + month + “ has” + days + “ days”); A way to eliminate confusing if/else statements Note: You can only switch on Integer type variables: char, byte, short, int, and long

Iterations (Three kinds) Body Again ? Pre-test Again? Post-test Initialize Adjust Counter Body Again ? Counter controlled The Java syntax for each kind Is somewhat different

Pre Test Loop Sum numbers until a user enters a negative int x = 0, sum = 0; while (x >= 0) { x = IO.readInt (“Enter an integer to sum”); if (x >=0) sum +=x; } System.out.println(“The sum is ” + sum); while ( ) { }

Post-test loop (another way) Sum numbers until a user enters a negative int x = 0, sum = 0; do { sum += x; x = IO.readInt (“Enter an integer to sum”); } while (x >= 0); System.out.println(“The sum is ” + sum); do { } while ( );

Sum number another way int x = 0, sum = 0; while (true) { x = IO.readInt (“Enter an integer to sum”); if (x <0) break; sum +=x; } System.out.println(“The sum is ” + sum); Note: The break statement jumps out of the loop

Counter controlled loop Sum numbers from one to one-hundred int sum; for (int i=1; i<=100; i++) { sum += i; } System.out.println (“The sum is ” + sum); for ( ; ; ) { } Question: How to sum numbers from -100 to +100? Question: How to sum just the even numbers? i = 1 i++ sum+=i i <= 100

Pre-test loop: sum 1 through 100 Sum 1 through 100 int sum = 0, i = 1; while (i<=100) { sum += i++; } Question: What happens if the ++ was left off? Sum even numbers 2 through 100 (one way) int sum = 0, i = 1; while (i<=0) { if (++i%2!=0) continue; sum += i; } Sum even numbers 2 through 100 (another way) int sum = 0; i = 2; while (i<=100) { sum += i; i += 2; } Note: The continue statement iterates to the next value

Post-test loop to sum numbers Sum 1 through 100 int sum = 0, i = 1; do { sum += i++; } while (i<=100); Question: What happens if the ++ was left off? Sum even numbers 2 through 100 (one way) int sum = 0, i = 1; do { if (++i%2!=0) continue; sum += i; } while (i<=100); Sum even numbers 2 through 100 (another way) int sum = 0; i = 2; do { sum += i; i += 2; } while (i<=100);

A more difficult problem int sum = 0; for (int num=1; num < 1000); num++) { for (int i=2; i<num; i++) { if (num%i == 0) { sum += num; break; } Sum all the non-primes up to 1000 Note: A prime is a number that is divisible by only itself and one

Primitive verses Object variables Primitive variable types: long, int, short, byte, float, double, boolean Object variable types: String and others that we will be creating. How do we compare to primitive variables for equality? – Answer: We use == – Example: if (x == y) How do we compare object variables for equality? – Answer: We use the object’s.equals method – Example: if (str.equals(“quit”)); Question: Why the difference? Answer: Because == will compare where in memory the object is and not its contents. Note: There is more to this, but for now, this explanation will suffice

Structure an input loop Loop until the user enters a negative while ((value = IO.readInt("Enter value: "))>=0) { // Do something with the value variable } Loop until the user types quit do { // Do some kind of processing more = IO.readString(); } while (more!=null && !more.equals(“quit”));

Review 1.When would you use a pre-test, post-test, or counter controlled loop? 2.What is an infinite loop? Give an example of when it can happen. 3.What is the difference between continue and break? 4.Which loop allows you to initialize a variable as part of its initialization? 5.What is the loop condition for? What is the loop initialization for? 6.When would you use a switch statement? 7.What is a limitation of using a switch statement? 8.What is a nested loop, condition, or a switch? 9.Why is a loop a good thing to apply to the lab projects we did so far? 10.What is a prime number? 11.Which loop requires a semicolon as part of its syntax? 12.When are the braces needed in connection with the loop body? 13.Define the terms: iteration and body