CS 106 Introduction to Computer Science I

Similar documents
CS 106 Introduction to Computer Science I

Introduction. C provides two styles of flow control:

Repetition, Looping CS101

Chapter 4: Control structures. Repetition

C++ Programming: From Problem Analysis to Program Design, Third Edition

CS 106 Introduction to Computer Science I

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Chapter 4: Control structures

CS 106 Introduction to Computer Science I

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

CS 206 Introduction to Computer Science II

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Repetition CSC 121 Fall 2014 Howard Rosenthal

CS 106 Introduction to Computer Science I

Repetition, Looping. While Loop

CS112 Lecture: Loops

Lecture Transcript While and Do While Statements in C++

Introduction to the Java Basics: Control Flow Statements

REPETITION CONTROL STRUCTURE LOGO

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

The for Loop. Lesson 11

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Control Structures II. Repetition (Loops)

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

CS 106 Introduction to Computer Science I

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

STUDENT LESSON A12 Iterations

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Chapter 5: Control Structures

Loops and Files. of do-while loop

CS111: PROGRAMMING LANGUAGE II

Unit 1 Lesson 4. Introduction to Control Statements

Why Is Repetition Needed?

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

CS 106 Introduction to Computer Science I

4 Algorithms. Definition of an Algorithm

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Math Modeling in Java: An S-I Compartment Model

Warmup : Name that tune!

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

CS112 Lecture: Repetition Statements

CompSci 125 Lecture 11

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Repetition and Loop Statements Chapter 5

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Lecture 10. Daily Puzzle

Chapter 4 Introduction to Control Statements

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Java Coding 3. Over & over again!

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

(6-1) Iteration in C H&K Chapter 5. Instructor - Andrew S. O Fallon CptS 121 (February 11, 2019) Washington State University

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

Theory of control structures

LECTURE 5 Control Structures Part 2

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

Computer Programming I - Unit 5 Lecture page 1 of 14

Object-Oriented Programming in Java

CS 106 Introduction to Computer Science I

(Refer Slide Time: 00:26)

Conditions, logical expressions, and selection. Introduction to control structures

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

Control Structures (Deitel chapter 4,5)

CS110D: PROGRAMMING LANGUAGE I

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Scope of this lecture. Repetition For loops While loops

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

CS302: Self Check Quiz 2

Lecture 7 Tao Wang 1

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Repetition Structures II

Fundamentals of Programming Session 13

Chapter 2: Functions and Control Structures

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Laboratory 5: Implementing Loops and Loop Control Strategies

COMP-202 Unit 4: Programming with Iterations

Chapter 5: Loops and Files

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Problem Solving With Loops

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Java Review. Fundamentals of Computer Science

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Recap: Assignment as an Operator CS 112 Introduction to Programming

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

8. Control statements

CS1150 Principles of Computer Science Loops (Part II)

Building Java Programs

Iteration statements - Loops

Control Statements. Musa M. Ameen Computer Engineering Dept.

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Introduction to Computer Science Unit 3. Programs

Transcription:

CS 106 Introduction to Computer Science I 02 / 15 / 2016 Instructor: Michael Eckmann

Questions? Comments? Loops to repeat code while loops for loops do while loops Today s Topics

Logical operators Example of a nice use of && (short circuit) if ((text.length() > 3) && (text.charat(3) == 'a')) vs. if (text.charat(3) == 'a') Assume text is a String variable and has a value. What benefit does the first if have over the second one?

while loops While loops are used to repeat lines of code until some condition is met. e.g. while (condition) // statements to do while condition is true. } condition is evaluated and if true the code within the curly braces executes. Then condition is tested again and if true the code within the curly braces executes. This continues until the condition is false (or a break; statement is hit.)

while loops It's extremely important for you to know exactly what code is executed in what order for you to be able to follow what a program is doing. Let's follow the code below line by line in order. (assume that it is inside a main method of a class.) int x = 1; System.out.println( Just before the loop ); while (x < 4) } System.out.print( * ); x++; System.out.println( \njust got out of the loop );

int total = 0; while (total < 100) } while loops total = total + 10; System.out.println( total = + total); System.out.println( finished printing totals ); // What would this code do?

while loops Sentinel controlled loops vs. counter controlled loops. Counter controlled loops are those loops that usually execute a set number of times every time. A sentinel is a value that is used to terminate a loop. For instance, if you wanted a user to enter in some numbers (but you didn't know how many s/he was going to enter) to be averaged (and assuming that they are all supposed to be positive) we can ask the user to enter in -1 after s/he enters all the numbers so we know s/he's finished. Let's see an example.

while loops int inputnum=0, count = 0; String inputstr; double total = 0.0; while (inputnum!= -1) } inputstr = JoptionPane.showInputDialog( Enter a number to be averaged ); inputnum = Integer.parseInt(inputStr); total += inputnum; count++; System.out.println( average = + (total / count)); // any problems with this code???? Hint: yes there's a logic error.

while loops Sentinel controlled loops vs. counter controlled loops. Counter controlled loops are those loops that usually execute a set number of times every time. As a programmer what value makes sense to choose as the sentinel is specific to the situation. Could anyone characterize what would be a bad value to choose as the sentinel?

while loops Order of execution of a while loop. First the condition is tested. If it is true the statements within the curly braces execute Then condition is tested again If it is true the statements within the curly braces execute again Then condition is tested again... This continues until the condition is false when tested. At that time, program control continues with the code after the right curly brace. Note: It IS possible that even on the first test of the condition, that it may be false. If it is, the statements within the loop don't execute at all.

while loops What happens to loops that do not alter the value of a variable in the condition?

while loops What happens to loops that do not alter the value of variable(s) in the condition? If that condition is true the first time, it will stay true forever and we will have an infinite loop. e.g. int x = 2; while (x > 0) System.out.println( x = + x); }

while loops Any other ways to get an infinite loop?

Would this be infinite? while loops int x = 2; while (x > 0) System.out.println( x = + x); x++; }

Loop terminology We say that a loop iterates some number of times. One iteration of a loop is one execution of the block of code inside it.

for loops The for loop is a counter controlled repetition structure that compactly lets a programmer specify the control variable, its initial value, the condition to be checked each time through the loop and the amount to increment (or decrement) the control variable for ( expression1; expression2; expression3 ) statement or compound statement

for loops for ( expression1; expression2; expression3 ) statement or compound statement where expression1 contains the declaration and initialization of the control variable expression2 contains the continuation condition and the ending value of the control variable expression3 contains the increment or decrement of the control variable (and the amount of this change in value each time through the loop)

for loops 1 4 for ( expression1; expression2; expression3 ) 2 } statements 3 (if 2 was true) Order of operations: 1. expression1 executes first (and only once) 2. expression2 executes and if the result is false, program control goes to the code after the right curly brace 3. if the result is true, then the statements within the curly braces of the for loop execute in order then expression3 executes (which usually alters the value of the loop variable.) Then goto 2 above.

for loop example for ( int x = 10; x >= 1; x-- ) do some statements here... } note that we used x-- to subtract 1 from the counter each time through the loop, we could have used: x = x - 1 or x -= 1 or --x

for loop Each of the three expressions in the for loop structure are optional. don t need expression1 if control variable declared and initialized before the loop don t need expression2 if you desire having an infinite loop don t need expression3 if you change the value of the control variable within the loop

for loop for ( int cntr = 1; cntr <= 20; cntr = cntr + 1 ) do some statements here... } this is essentially the same thing as the following while loop int cntr = 1; while ( cntr <= 20 ) do some statements here... cntr = cntr + 1; } except, the variable cntr is not available to the program after the for loop, but it is available to be referenced after the while loop

exercise Write an application that calculates the product of the odd integers from 1 to 15 and then displays the results in a message dialog. Let's write it using a while loop Then let's write it using a for loop

do while loops While loops and for loops all have the possibility of having the statements inside them execute zero or more times. Zero being the key word here. When would it be possible for those loops to execute zero times? The big difference between those kinds of loops and dowhile loops are that do-while loops execute their statements at least once (that is, one or more times.)

do statements to do } while (condition); do while loops In this kind of loop, the condition is tested after the loop executes the first time. If the condition is true it does the statements again, and so on until the condition is false.

do while loops do statements to do 1,3 (if 2 was true) } while (condition); 2 In this kind of loop, the condition is tested after the loop executes the first time. If the condition is true it does the statements again, and so on until the condition is false.

do while loops int cntr = 1; while ( cntr <= 20 ) } do some statements here... cntr = cntr + 1; // the above is basically equivalent to: int cntr = 1; do do some statements here... cntr = cntr + 1; } while (cntr <= 20);

break; We've seen how break; reacts in switch statements. break; acts similarly within the curly braces of a while loop, for loop or do-while loop. It terminates the loop and program control continues after the loop as if it ended normally.

break; int x = 0; while (x <= 10) if (x == 5) break; System.out.println( x = + x); x++; } // what's this code going to do?