int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

Similar documents
The for Loop. Lesson 11

Loops. CSE 114, Computer Science 1 Stony Brook University

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

Introduction. C provides two styles of flow control:

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

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

AP CS Fall Semester Final

Computer Programming I - Unit 5 Lecture page 1 of 14

Problem Solving With Loops

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Repetition Structures

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

REPETITION CONTROL STRUCTURE LOGO

(Refer Slide Time: 00:26)

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

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

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

Iteration statements - Loops

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

Loops / Repetition Statements

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

For Loop. Variations on Format & Specific Examples

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

STUDENT LESSON A12 Iterations

Object-Oriented Programming in Java

CS110D: PROGRAMMING LANGUAGE I

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Tutorial 3: Conditionals and Iteration COMP 202: Intro to Computing 1

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

3 The L oop Control Structure

Loops and Expression Types

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Chapter 2: Functions and Control Structures

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

CS112 Lecture: Loops

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

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

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

Loops / Repetition Statements

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

ECE 122. Engineering Problem Solving with Java

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Loops (while and for)

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

Iterative Languages. Scoping

CS 177 Week 5 Recitation Slides. Loops

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

Chapter Goals. Contents LOOPS

3chapter C ONTROL S TATEMENTS. Objectives

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

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

Recap: Assignment as an Operator CS 112 Introduction to Programming

CS1150 Principles of Computer Science Loops (Part II)

Fundamentals of Programming Session 13

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

CS 231 Data Structures and Algorithms, Fall 2016

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Chapter 17. Iteration The while Statement

LAMBDA EXPRESSIONS. Summer 2018

AP Programming - Chapter 6 Lecture

CS 106 Introduction to Computer Science I

Flow Control. CSC215 Lecture

CS 112 Introduction to Programming

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137

CS112 Lecture: Repetition Statements

5. Control Statements

CT 229 Java Syntax Continued

Computer Science is...

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

Control Flow Statements

In this chapter you will learn:

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

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

Introduction to the Java Basics: Control Flow Statements

Lecture 14. 'for' loops and Arrays

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

COMP Introduction to Programming If-Else Statement, Switch Statement and Loops

Motivations. Chapter 5: Loops and Iteration. Opening Problem 9/13/18. Introducing while Loops

Arithmetic Compound Assignment Operators

Repetition, Looping. While Loop

Repetition with for loops

Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Lecture 5: Methods CS2301

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

LOOPS. Repetition using the while statement

Scanner Objects. Zheng-Liang Lu Java Programming 82 / 133

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

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

Conditionals and Loops

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Fundamentals of Programming Session 4

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

Transcription:

11-1 One of the most important structures in Java is the -loop. A loop is basically a block of code that is with certain rules about how to start and how to end the process. Suppose we want to sum up all the integers from 3 to 79. One of the statements that will help us do this is: sum = sum + j; However, this only works if we repeatedly execute this line of code, first with, then with, and finally with. The full structure of the -loop that will do this is: int j = 0, sum = 0; for (j = 3; j <= 79; j++) sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop. System.out.println( The final sum is + sum); // prints 3157 Now let s examine the three parts in the parenthesis of the -loop.. If we had wanted to start summing at 19, this part would have read,.. We continue looping as long as this expression. In general this expression can be expression. For example, it could be: count = = 3 s + 1 < alphb s > m +19 etc. There is something really bad that can happen here. You must write your code so as to insure that this control statement will eventually become, thus causing the loop to terminate. Otherwise you will have an endless loop which is about the worst thing there is in programming. ++ This tells us how our variable changes as we proceed through the loop. In this case we are incrementing each time; however, other possibilities are: j-- j = j + 4 j = j * 3 etc. For our example above, exactly when does the increment ++ occur? Think of the step expression being at the bottom of the loop as follows:

11-2 for (j = 3; j <= 79;... ) some code j++; //Just think of the j++ as being the last line of code inside the //braces. If the keyword is executed inside a -loop, the loop is immediately exited (regardless of the control statement). Execution continues with the statement immediately following the closing brace of the -loop. It is possible to declare the loop variable in the initializing portion of the parenthesis of a -loop as follows: for ( j = 3; j <= 79; j++) In this case the of is confined to the interior of the loop. If we write in statement outside the loop (without redeclaring it to be an ), it won t compile. The same is true of any other variable declared inside the loop. Its scope is limited to the interior of the loop and is not recognized outside the loop as is illustrated in the following code: for (j = 3; j <= 79; j++) double d = 102.34; System.out.println(d); //won t compile because of this line If there is only or just one basic structure (an -structure or another loop) inside a loop, then the braces are unnecessary. In this case it is still correct (and highly recommended) to still have the braces but you leave them off. for (j = 3; j <= 79; j++) is equivalent to for (j = 3; j <= 79; j++) sum = sum + j; sum = sum + j; It is often useful to know what the loop variable is after the loop finishes:

11-3 for (j = 3; j <= 79; j++) some code System.out.println(j); // On the last iteration of the loop, increments up to 80 and this is when the control statement finally is. Thus, the loop is exited. Nested loops is the term used when one loop is placed inside another as in the following example: for(int j = 0; j < 5; j++) System.out.println( Outer loop ); // for(int k = 0; k < 8; k++) System.out.println(...Inner loop ); // The inner loop iterates eight times for of the five iterations of the outer loop. Therefore, the code inside the inner loop will execute 40 times. ********************************************************************** A very common mistake is to put a semicolon immediately after the parenthesis of a loop as is illustrated by the following code: for (j =3; j <= 79; j++); //This block of code is only executed once because of the inappropriately //placed semicolon above.... some code...

11-4 In each problem below state what is printed unless directed otherwise. 1. int j = 0; for (int g = 0; g <5; g++) j++; System.out.println(j); 2. int s = 1; for (int j = 3; j >= 0; j--) s = s + j; System.out.println(s); 3. int p = 6; int m = 20, j; for (j = 1; j < p; j++); //Notice the semicolon on this line m = m + j * j; System.out.println(m); 4. double a = 1.0; for (int j = 0; j < 9; j++) a*=3; System.out.println(j); 5. for (int imus = 0; imus < 10; imus++) int b = 19 + imus; System.out.println(b); 6. double d = 100.01; int b = 0; for (int imus = 0; imus < 10; imus++) b = 19 + imus; d++; System.out.println(d); 7. Write a for-loop that will print the numbers 3, 6, 12, and 24 8. Write a for-loop that will print the numbers 24, 12, 6, 3

9. int k = 0; for(int j = 0; j <= 10; j++) if (j = = 5) break; else k++; System.out.println(k); 11-5 10. What is the name of the part of the parenthesis of a for-loop that terminates the loop? 11. What is the value of for each iteration of the following loop? int i, j; for( i = 10; i <= 100; i = i+ 10) j = i / 2; 12. What is the value of after the following statements have executed? int r, j; for (j = 1; j < 10; j = j * 2) r = 2 * j; 13. What is the worst sin you can commit with a for-loop (or any loop for that matter)? 14. How many times does the following loop iterate? for (p = 9; p <= 145; p++) Write a program that will allow a user to input his name. The prompt and input data would look something like this: Please enter your name. Peter Ustinov Using a for-loop and the method, produce a printout of the reversal of the name. For example, the name would be: vonitsu retep Notice that the printout is in all lower-case. Use the method, to accomplish this.

11-6 1. What is output? A. 0 B. 10 C. 15 D. 5 2. What is output? A. 66 B. 100 C. 101 D. 99 int sum=0; for (int k=0; k<5; k++) sum+=k; System.out.println(sum); double kk = 3; int j = 0; for( j = 0; j <= 100; j++) kk = kk + Math.pow(j, 2); ++kk; System.out.println(j); 3. What is the final value of p? double p = 0; for(int m =10; m > 6; --m) A. 10 B. 4 if(m= =7) C. 5 p = p+m; D. 12 else ++p; 4. Which of the following will print the set of odd integers starting at 1 and ending at 9? A. for(int j=0; j<=9; j++) System.out.println(j); B. for(int j=1; j<10; j= j+2) System.out.println(j); C. for(int j=1; j<=9; j+=1) System.out.println(j); D. for(int j=1; j<=9; j+=2) System.out.println(j); E. Both B and D 5. What is output? A. 4950 B. 101 C. 100 D. Nothing, it s an endless loop double x = 0; for(int b=0; b<101; b++) x = x + 1; b--; System.out.println(x); 6. What is output? A. 5 6 B. 6 6 C. 5 10 D. 5 5 int p, q=5; for(p=0; p<5; p++); //notice the semicolon q = q+1; System.out.println(p + + q);

7. What is output? A. 98 B. 3939 C. 109 D. 4039 int j, k; int count = 0; for(j=0; j<4; j++) for( k = 0; k < 10; k++ ) count++; System.out.print(count--); System.out.println(count); 11-7