CS2011 Introduction to Programming I Loop Statements (II)

Slides:



Advertisements
Similar presentations
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
Advertisements

Chapter 4 Loops Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter 4 Loops Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Introduction to Computer Programming in c
CSCI 130 for Loops Chapter 7 - A. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
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.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Iteration Hussein Suleman UCT Dept of Computer Science CS115 ~ 2004.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
UCT Department of Computer Science Computer Science 1015F Iteration
Chapter 4: Looping Structures LECTURER : MRS ROHANI HASSAN
Lecture 4b Repeating With Loops
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
Review Materials I (T) Subject : T0016 – ALGORITHM AND PROGRAMMING
Control Statements: Part 2
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 4 Loops DDC 2133 Programming II.
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 2.2 Control Structures (Iteration)
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 4 Control structures and Loops
Chapter 8 Repetition Statements
Repetition and Loop Statements
Programming Fundamentals Lecture #6 Program Control
Outline Altering flow of control Boolean expressions
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 2.2 Control Structures (Iteration)
Chapter 6 Control Statements: Part 2
CS2011 Introduction to Programming I Arrays (II)
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CS2011 Introduction to Programming I Methods (II)
CS2011 Introduction to Programming I Arrays (I)
CS2011 Introduction to Programming I Selections (II)
Lab5 PROGRAMMING 1 Loop chapter4.
CS2011 Introduction to Programming I Multidimensional Arrays
Computer programming Lecture 3.
Chapter 5: Control Structures II (Repetition)
CS2011 Introduction to Programming I Strings
CS2011 Introduction to Programming I Selections (I)
Chap 7. Advanced Control Statements in Java
CS2011 Introduction to Programming I Loop Statements (I)
Chapter 3 Flow of Control Loops in Java.
Programming Fundamental
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 4: Loops and Iteration
Presentation transcript:

CS2011 Introduction to Programming I Loop Statements (II) Chengyu Sun California State University, Los Angeles

Three Keys Things in a Loop Initial State Loop Condition int count = 0; while ( count < 20 ) { System.out.println( "Welcome" ); ++count; } Change of state that affects the loop condition

for Statement All three things in one place for( int count = 0 ; count < 20 ; ++count ){ System.out.println( "Welcome" ); } Suitable when the number of iterations is known in advance, e.g. Display "Welcome" 20 times  for Convert any binary number to decimal  while

Syntax and Flow for ( initialization ; termination ; increment ) { statement(s) } initialization termination false true increment statement(s)

More on For Loop Syntax Initialization is usually the declaration and initialization of a single variable The variable name is usually i (for iteration or index) The scope of the variable is within the loop Initialization and Increment may consist of multiple statements separated by commas Initialization, termination, and increment are all optional

For Loop Examples for ( int i=0 ; i < 10 ; ++i ) for ( int x=3, y=4 ; x+y < 15; x++, y++ ) int n = 0, m = 10; for ( ; n < 10 || m > 0; ++n, m -= 2) Does it make any difference to use ++i or i++?

Example: Multiplication Table

Be Careful With Nested Loops for ( int i = 0 ; i < 1000 ; ++i ) for( int j = 0 ; j < 1000 ; ++j ) for( int k = 0 ; k < 1000 ; ++k ) { // Perform an action … … } How many times is the action performed??

Example: Find The Greatest Common Divisor The greatest common divisor of two integers n1 and n2 are the largest number d that meet the condition n1%d==0 && n2%d==0 Strategy (that involves repetition)??

break Statement Example: check for palindrome If we find a mismatch, we don't need to check the rest of the string break allows us to break out of a loop (or a switch statement as we see before)

continue Statement continue allows us to skip the rest of the current iteration Example: calculate 1 + 2 + 3 + … + 9 + 11 + 12 + 13 + … + 19 + 21 + 22 + 13 + … + 29 + … … 91 + 92 + 93 + … + 99 i.e. skipping 10, 20, 30, …, 100

Readings Chapter 5 of the textbook (there will be a quiz the week after next)