Bjarne Stroustrup. creator of C++

Similar documents
Over and Over Again GEEN163

Loops. GEEN163 Introduction to Computer Programming

ITERATION WEEK 4: EXMAPLES IN CLASS

Loops. CSE 114, Computer Science 1 Stony Brook University

CS 177 Week 5 Recitation Slides. Loops

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Top-Down Program Development

CS 101 Spring 2007 Midterm 2 Name: ID:

Iteration statements - Loops

AP Computer Science Unit 1. Programs

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen

Decisions, Decisions, Decisions. GEEN163 Introduction to Computer Programming

Java Coding 3. Over & over again!

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Repetition CSC 121 Fall 2014 Howard Rosenthal

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

1 Short Answer (10 Points Each)

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Supplementary Test 1

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)

CS 177 Week 15 Recitation Slides. Review

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

AP COMPUTER SCIENCE A

CS111: PROGRAMMING LANGUAGE II

Recap: Assignment as an Operator CS 112 Introduction to Programming

Software Practice 1 Basic Grammar

Question: Total Points: Score:

CS 112 Introduction to Programming

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

CompSci 125 Lecture 11

Introduction to Computer Science Unit 2. Exercises

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

CS1083 Week 2: Arrays, ArrayList

Introduction to Java & Fundamental Data Types

Last Class. While loops Infinite loops Loop counters Iterations

Lecture 8 " INPUT " Instructor: Craig Duckett

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CS171:Introduction to Computer Science II

Topic 11 Scanner object, conditional execution

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

H212 Introduction to Software Systems Honors

CSE 114 Computer Science I

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Lecture 10. Instructor: Craig Duckett

Programming with Java

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Object Oriented Programming. Java-Lecture 6 - Arrays

AP Computer Science A Summer Assignment 2017

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Repetition, Looping. While Loop

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Using Classes. GEEN163 Introduction to Computer Programming

CS111: PROGRAMMING LANGUAGE II

Question: Total Points: Score:

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

CSCI 1226 A Test #1. Wednesday, 10 October, 2018 Name: Student #: General Instructions Read and follow all directions carefully.

Computer Science is...

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Chapter 3. Ch 1 Introduction to Computers and Java. Selections

COMP-202 Unit 4: Programming with Iterations

CP122 CS I. Iteration

Midterms Save the Dates!

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;

CS 101 Fall 2005 Midterm 2 Name: ID:

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Combo Lecture Lecture 14/15. Instructor: Craig Duckett

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Practice Midterm 1. Problem Points Score TOTAL 50

Lecture 14. 'for' loops and Arrays

Example: Monte Carlo Simulation 1

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Computer Science is...

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

Logic & program control part 3: Compound selection structures

CHAPTER : 9 FLOW OF CONTROL

CSE 113 A. Announcements - Lab

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

Selection Statements and operators

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

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

Introduction to the Java Basics: Control Flow Statements

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Transcription:

We Continue GEEN163

I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone. Bjarne Stroustrup creator of C++

Exam The second exam will be in lecture on Wednesday, October 23 The exam will cover everything since the first exam If statements Loops Files A sample exam is available on Blackboard under course materials You may have one 8½ x 11" page of notes

Recitation Quiz There will be a quiz in recitation on Thursday or Friday for the 10:00 lecture Monday or Tuesday for the 9:00 lecture

Lab Quiz There will be a quiz in lab next week The quiz will cover loops and ifs You will be required to complete a program on your own without the help of others You may use your book, notes and the web

for and while loop equivalence for ( initialization ; test; update ) { statements to repeat initialization; while (test) { is the same as statements to repeat update;

do while version To read 7 integers with a do while loop Scanner keys = new Scanner(System.in); int sum = 0, count; count = 0; do { sum = sum + keys.nextinteger(); count++; while(count < 7);

while version To read 7 integers with a while loop Scanner keys = new Scanner(System.in); int sum = 0, count; count = 0; while(count < 7){ sum = sum + keys.nextinteger(); count++;

for version To read 7 integers with a for loop Scanner keys = new Scanner(System.in); int sum = 0, count; for (count = 0; count < 7; count++){ sum = sum + keys.nextinteger();

Declaring the Loop Counter In a for loop, you can declare the variable that is used as the loop counter The loop counter can only be used in the loop for (int bull = 0; bull < 47; bull ++) { // loop body // the loop counter, bull, can be used here // You cannot use bull after the loop

What does this print? int dog = 1; for (int cat = 3; cat < 5; cat++) { dog = dog + cat; System.out.print( dog ); A. 1 B. 8 C. 13 D. none of the above

Loops in Loops Sometimes a program will need one loop in the body of another loop int sum = 0, cat = 5, dog = 3; for (int i = 0; i < cat; i++) { for (int k = 0; k < dog; k++) { sum += i * k; Note that the loop counter variables (i and k) must be different

Long Running Programs If a loop repeats n times is inside a loop that repeats m times, the total number of iterations of the loop is m * n If n and m are large, it may take a very long time for the program to complete Loops inside loops inside loops inside loops can take a long time to run

Selecting from Multiple Choices The if - else if statements are a way of selecting from multiple choices if ( x ) { // if x is true else if ( y ) { // if x is false and y is true else if ( z ) { // if x and y are false and z is true else { // if x, y and z are false

Write Java with your Team Set the String variable weather to "calm" if wind is less than 3.0, "strong" if wind is greater than 24.0 and "breeze" otherwise double wind = something;

Possible Solution Set the String variable weather to "calm" if wind is less than 3.0, "strong" if wind is greater than 25.0 and "breeze" otherwise double wind = something; if (wind < 3.0) { weather = "calm"; else if (wind > 25.0) { weather = "strong"; else { weather = "breeze";

What is weather when wind is 2.5? double wind = something; if (wind < 3.0) { weather = "calm"; if (wind > 25.0) { weather = "strong"; else { weather = "breeze"; A. calm B. strong C. breeze D. 3.0 E. 25.0

Scanner with Files When you create an object of the Scanner class, you can specify a File object instead of System.in java.io.file elephant = new java.io.file("mydata.txt"); java.util.scanner pachyderm = new java.util.scanner( elephant ); // read a number from the file int mouse = pachyderm.nextint();

Complete this program to average the numbers in mydata.txt public class SumFile { public static void main(string[] args) throws java.io.filenotfoundexception { java.io.file dog = new java.io.file("mydata.txt"); java.util.scanner cat = new java.util.scanner( dog );

Possible Solution public class SumFile { public static void main(string[] args) throws java.io.filenotfoundexception { java.io.file dog = new java.io.file("mydata.txt"); java.util.scanner cat = new java.util.scanner( dog ); double sum = 0.0; // sum of numbers int count = 0; // count of numbers while (cat.hasnextdouble()) { sum += cat.nextdouble(); count++; System.out.println("Average is"+ sum/count);

break statement The break statement stops a loop The break statement can be used in for loops, while loops, do-while loops and switch statements A break statement causes the loop to stop looping and starts executing after the loop

break example int sum = 0; for (int i = 0; i < 5; i++) { sum = sum + i * i; if ( sum > 10) break; i sum 0 0 1 1 2 5 3 14 System.out.println(sum); // prints 14

Loops in Loops If you have loops inside other loops, a break statement will stop the looping of the inner most loop where the break is executed Outer loops will not be effected by a break in an inner loop.

Break in a Loop in a Loop public class BreakSum { public static void main(string[] args) { int sum = 0; for (int i = 1; i < 3; i++ ) { for (int k = 1; k < 3; k++ ) { System.out.println("i:"+ i + " k:"+ k ); sum = sum + k; if (sum > 2 ) break; System.out.println( "sum: "+sum ); System.out.println( "final sum: "+sum ); i:1 k:1 sum: 1 i:1 k:2 i:2 k:1 final sum:4

What is Displayed? int apple = 1; for(int pear = 1; pear<4; pear++) { apple++; if (apple > 4 ) { break; apple += pear; System.out.println(apple); A. 1 B. 4 C. 6 D. 7 E. 10

Labeled Statements You can put a label before any statement Labels start the Java statement and are separated from the statement by a colon Label names follow the usual rules fred : for (int i = 0; i < 100; i++) { mary : while ( dog < 13 ) {

break to a label To avoid problems with loops in loops, you can put a label on the loop and then specify the label on the break fred : for (int i = 0; i < 13; i++ ) { if (i * 2 > 20 ) break fred; When you have loops in loops, this specifies exactly which loop you are terminating

break Labels The label on a break statement must indicate a loop statement (for, do or while) that encompasses the break tom: for (int i = 0; i < 47; i++ ) { sum += i * i; if (sum > 100) break george; // ERROR george: System.out.println("after loop");

Nested Loop Break int cow, bull, sum = 0; daisy: for (cow = 1; cow <= 3; cow++ ) { ferdinand: for (bull = 1; bull <= 5; bull++ ) { sum += cow * bull; if (sum > 25) break daisy; // ends both loops

continue Statement The continue statement makes it go the beginning of the loop and start the next iteration for (int cow = 0; cow < 100; cow++) { // something if ( cow < 47 cow > 53 ) continue; // some other thing System.out.println( cow ); This prints 47 to 53

Continue in a Loop in a Loop public class BreakSum { public static void main(string[] args) { int sum = 0; for (int i = 1; i < 3; i++ ) { for (int k = 1; k < 3; k++ ) { System.out.println("i:"+ i + " k:"+ k ); sum = sum + k; if (sum > 2 ) continue; System.out.println( "sum: "+sum ); // other stuff System.out.println( "final sum: "+sum ); i:1 k:1 sum: 1 i:1 k:2 i:2 k:1 i:2 k:2 final sum:6

What is the final value of apple? int apple = 1; for(int pear = 1; pear<4; pear++) { apple++; if (apple > 4 ) { continue; apple += pear; System.out.println(apple); A. 1 B. 4 C. 5 D. 8 E. none of the above

Labels on Continue Just like the break statement, you can put labels on a continue statement The label must be on a surrounding loop, either for, while or do while

Labels are Recommended Many Java programs do not use labels on the break or continue statements Many Java programs have errors due to misunderstood break and continue statements The use of labels makes your intentions clear The compiler will tell you if you are doing something wrong

Software Failure Due to a programmer s misunderstanding of how a break statement works, a NASA probe crashed into the surface of Venus

Modify this to average the first 100 numbers in mydata.txt public class SumFile { public static void main(string[] args) throws java.io.filenotfoundexception { java.io.file dog = new java.io.file("mydata.txt"); java.util.scanner cat = new java.util.scanner( dog ); double sum = 0.0; // sum of numbers int count = 0; // count of numbers while (cat.hasnextdouble()) { sum += cat.nextdouble(); count++; System.out.println("Average is "+sum/count);

Possible Solution public class SumFile { public static void main(string[] args) throws java.io.filenotfoundexception { java.io.file dog = new java.io.file("mydata.txt"); java.util.scanner cat = new java.util.scanner( dog ); double sum = 0.0; // sum of numbers int count = 0; // count of numbers while (cat.hasnextdouble() && count < 100) { sum += cat.nextdouble(); count++; System.out.println("Average is "+sum/count);

Possible Solution public class SumFile { public static void main(string[] args) throws java.io.filenotfoundexception { java.io.file dog = new java.io.file("mydata.txt"); java.util.scanner cat = new java.util.scanner( dog ); double sum = 0.0; // sum of numbers int count = 0; // count of numbers readloop: while (cat.hasnextdouble()) { sum += cat.nextdouble(); count++; if (count == 100) { break readloop; System.out.println("Average is"+sum/count);

Lab Quiz There will be a quiz in lab next week The quiz will cover loops and ifs You will be required to complete a program on your own without the help of others You may use your book, notes and the web

Exam The second exam will be in lecture on Wednesday, October 23 The exam will cover everything since the first exam If statements Loops Files A sample exam is available on Blackboard under course materials You may have one 8½ x 11" page of notes

Recitation Quiz There will be a quiz in recitation on Thursday or Friday for the 10:00 lecture Monday or Tuesday for the 9:00 lecture

Programming Assignment A reading level program is due Friday at midnight