COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
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.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Chapter 5: Control Structures II (Repetition)
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
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.
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
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
Chapter 5 Loops.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
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.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Chapter 5: Control Structures II (Repetition)
EKT120 COMPUTER PROGRAMMING
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Outline Altering flow of control Boolean expressions
3 Control Statements:.
Control Statements Loops.
Control Statements Loops.
Presentation transcript:

COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007

Announcements

One-Way Selection if (expression)if (expression) { statementstatement(s) }

Two-Way Selection if (expression) statement1 else statement2 if (expression) { statement(s)1 } else { statement(s)2 }

Two-Way Selection And White Space if (expression) { statement(s) } else { statement(s) } if (expression) { statement(s) } else { statement(s) } if (expression) { statement(s) } else { statement(s) }

The switch Statement switch (expression) { case value1: statements1 break; case value2: statements2 break; case value3: statements3 break; default: statements }

switch and nested if-else if (expression == value1) { statements1 } else if (expression == value 2) { statements2 } else if (expression == value 3) { statements3 } else { statements } switch (expression) { case value1: statements1 break; case value2: statements2 break; case value3: statements3 break; default: statements }

Writing Selection Statements What Type of Construct To Use? Only need to execute additional statements if condition is true Need to execute separate additional statements based on if the condition is true or false Need to execute different statements based on multiple conditions Need to execute different statements based on an expression that evaluates to a char or int if if-else nested if-else switch

Writing Selection Statements Write the outline of the selection statement –keywords –curly braces –case and break statements (for switch) Write the expression –boolean expression, or condition –expression or variable evaluating to char or int Write statements that execute based on the condition

Example Write a selection statement that sets grade to 'F' if score is less than 60 and sets grade to 'P' if score is greater than or equal to 60. 1) Choose construct if-else 3) Add condition if (score < 60) { } else { } 4) Add statements if (score < 60) { grade = 'F'; } else { grade = 'P'; } 2) Write outline if ( ) { } else { }

Questions What type of selection statement should be used? 1. Print "Male" if gender is 'M' and "Female" if gender is 'F'. 2. Print the state associated with areaCode, where areaCode is an int. 3. Print the maximum of three integers. if-else switch nested if-else

Question Assume that the lengths of 3 sides of a triangle are given in the integer variables side1, side2, and side3. Write code that will print: –"Equilateral" if all three sides are equal –"Isosceles" if only two sides are equal –"Scalene" if no sides are equal equilateral isosceles scalene First, what selection construct should be used? nested if-else

Answer // side1, side2, and side3 are lengths // of the sides of a triangle if (side1 != side2 && side2 != side3 && side1 != side3) { // no sides are equal System.out.println ("Scalene"); } else if (side1 == side2 && side2 == side3) { // all sides are equal System.out.println ("Equilateral"); } else { // two sides are equal System.out.println ("Isosceles"); }

Today in COMP 110 Repetition statements while loops Textbook Reference: Chapter 5 (pgs )

Why Is Repetition Needed? Want to add 5 integers to find their average? –declare a variable for each integer –initialize the sum –read in the user input one at a time –sum the numbers –take the average Want to add 1000 integers to find their average? repeat steps 3 and times 1. declare one variable for input 2. initialize the sum 3. read in one line of user input 4. add to the sum 5. take the average

Repetition Statements Allow us to execute a statement multiple times Often referred to as loops Controlled by boolean expressions –like selection, or conditional, statements Java has three kinds of repetition statements: –the while loop –the for loop –the do loop

Loops Must use a loop control variable –controls how many times to loop 4 Parts to Every Loop –initialization - set loop control variable before condition –condition - when to stop –update - change to the loop control variable –body - actions to repeat

Typical Uses of Loops Repeat a section of code a specified number of times - counter-controlled Repeat a section of code (reading input) until the a specific value is read - sentinel-controlled Repeat a section of code (reading input) until a valid value is entered - input validation Repeat a section of code (reading from a file) until the end of the file is reached - EOF-controlled Repeat a section of code until a boolean variable becomes false - flag-controlled

The while Loop Syntax while ( condition ) { loop body; } while is a reserved word If the condition is true, the loop body is executed. Then the condition is evaluated again. The loop body is executed repeatedly until the condition becomes false.

The while Loop Syntax while (expression) statement Expression is always true in an infinite loop Statements must change value of expression to false

The while Loop Example final int LIMIT = 3; int count = 0; while (count < LIMIT) { System.out.println (count); count++; } System.out.println (“All done!”); boolean condition loop body update initialization Output: All done!

The while Loop What's Going On? final int LIMIT = 3; int count = 0; while (count < LIMIT) { System.out.println (count); count++; } System.out.println (“All done!”); LIMIT count Output: All done! 2 3

The while Loop final int LIMIT = 3; int count = 0; while (count < LIMIT) { count++; System.out.println (count); } System.out.println (“All done!”); Output: All done!

The while Loop final int LIMIT = 3; int count = 0; while (count < LIMIT) { System.out.println (count); } System.out.println (“All done!”); Output: 0...

The while Loop final int LIMIT = 3; int count = 0; while (count <= LIMIT) { System.out.println (count); count++; } System.out.println (“All done!”); Output: All done!

The while Loop If the condition of a while statement is false initially, the loop body is never executed The body of a while loop will execute zero or more times

Counter-Controlled while Loop Used when exact number of times statements should execute is known Basic Form: counter = 0; while (counter < N) {... counter++;... } N is the number of times the loop should execute

Example final int NUM_STUDENTS = 100; int sum = 0; double average; int i = 0; // initialize loop control var while (i < NUM_STUDENTS) { sum += Integer.parseInt(inFile.readLine()); i++; // update loop control variable } average = (double) sum / NUM_STUDENTS; Often we use i, j, k as counter variable names.

Reading From a File How do you know how many items there will be? 1.ask the user 2.arrange the data file so that the first item is the number of items in the file Other ways to read all of the items: –use sentinel value –read until end-of-file (EOF)

Sentinel-Controlled while Loop Used when exact number of entry pieces is unknown but last entry (special / sentinel value) is known Basic Form: input the first data item into variable while (variable != sentinel) {... input a data item into variable }

Example int i = 0, sum = 0, score; final int SENTINEL = -99; double average; // initialize the loop control variable score = Integer.parseInt(inFile.readLine()); while (score != SENTINEL) { sum += score; // update the loop control variable score = Integer.parseInt(inFile.readLine()); } average = (double) sum / NUM_STUDENTS;

Average.java Example Read in integers and print their sum until the user enters 0 Print the average of the numbers entered

Input Validation while Loop Used to ensure that the user enters valid input Basic Form: input the first data item into variable while (variable is not in valid range) { ask the user for valid input input a data item into variable }

Example int num; // initialize loop control variable System.out.print (“Enter a number [0-100]: "); num = Integer.parseInt(keyboard.readLine()); while ((num 100)) { System.out.println (num + " is not [0-100]"); System.out.print (“Enter a number: “); // update the loop control variable num = Integer.parseInt(keyboard.readLine()); } System.out.println (“You entered “ + num);

WinPercentage.java Example Ask user for number of games won between (0 - 12) Keep asking until get a number between (0 - 12) Compute the winning percentage

Flag-Controlled while Loop Boolean value used to control loop Basic Form: boolean found = false; while (!found){... if(expression) found = true;... }

EOF-Controlled while Loop Used when input is from a file Sentinel value is not always appropriate Basic Form: inputLine = inFile.readLine(); while (inputLine != null) {... inputLine = inFile.readLine(); } null is a reserved word

TelephoneDigitProgram.java In textbook pg Ask user for uppercase letter Print telephone digit for that letter Enter 'Q' or 'Z' to quit

Next Time in COMP 110 for loops do...while loops Reading: Ch 5 (pgs )