Welcome back to Software Development!

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Loops Counting down from N to 1 in assembly we used JUMP statements to repeat previous instructions thus looping READ LOOP: WRITE SUB decrement JPOSLOOP.
CS150 Introduction to Computer Science 1
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Copyright © Texas Education Agency, Computer Programming For Loops.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
For Loops 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while Which loop to use? task with a specific.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
ADMIT TICKET WHAT DOES THIS OUTPUT? double y = 2.5; int x = 6 / (int) y; System.out.println(“x = “ + x);
Adding 10 items to a list… lstTest.Items.Add(1) lstTest.Items.Add(2) lstTest.Items.Add(3) lstTest.Items.Add(4) lstTest.Items.Add(5) lstTest.Items.Add(6)
Research Topics in Computational Science. Agenda Survey Overview.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.
Counting Loops.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition 8/9/2009. Learning to Program -- Suggestions Spend a lot of time fiddling around with code –Programming is something you have to learn by doing.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Lesson #5 Repetition and Loops.
Whatcha doin'? Aims: To start using Python. To understand loops.
while Repetition Structure
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
Lecture 6 Repetition Richard Gesick.
Lesson #5 Repetition and Loops.
Lecture 4B More Repetition Richard Gesick
الحلقات التكرارية وجمل الدوران (loops)
Manipulating Pictures, Arrays, and Loops part 2
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Lesson #5 Repetition and Loops.
Fencepost loops reading: 4.1.
Example Problems for Exam#2 (covers chapters 5, 6, 7, 8, 9)
Structured Program Development in C
Lecture Notes – Week 3 Lecture-2
Welcome back to Software Development!
Chapter 8 The Loops By: Mr. Baha Hanene.
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Welcome back to Software Development!
CS2011 Introduction to Programming I Loop Statements (II)
Week 6 CPS125.
Code Refresher Test #1 Topics:
Loop problem Write a method printLetters that prints each letter from a word separated by commas. For example, the call: printLetters("Atmosphere") should.
CHAPTER 21 LOOPS 1.
Welcome back to Software Development!
Welcome back to Software Development!
Lesson #5 Repetition and Loops.
Welcome back to Software Development!
True / False Variables.
Learning Plan 4 Looping.
Lec 6 Loop Statements Introduction to Computer Programming
Presentation transcript:

Welcome back to Software Development!

Loops

Loops 3 types of loops:

Loops 3 types of loops: while

Loops 3 types of loops: while used when you don’t know how many times to loop

Loops 3 types of loops: while for used when you don’t know how many times to loop for

Loops 3 types of loops: while for used when you don’t know how many times to loop for used when you do know how many times to loop

Loops 3 types of loops: while for foreach used when you don’t know how many times to loop for used when you do know how many times to loop foreach

Loops 3 types of loops: while for foreach used when you don’t know how many times to loop for used when you do know how many times to loop foreach used to work on parts of a group, one at a time

All Loops…

All Loops… Use a loop control variable

All Loops… Use a loop control variable First: Initialize the loop control variable

All Loops… Use a loop control variable First: Initialize the loop control variable If you don’t, you likely won’t be able to even get into the loop

All Loops… Use a loop control variable First: Initialize the loop control variable If you don’t, you likely won’t be able to even get into the loop Next: Check the loop control variable to see if keep looping

All Loops… Use a loop control variable First: Initialize the loop control variable If you don’t, you likely won’t be able to even get into the loop Next: Check the loop control variable to see if keep looping Finally: Change (often increment) the loop control variable

All Loops… Use a loop control variable First: Initialize the loop control variable If you don’t, you likely won’t be able to even get into the loop Next: Check the loop control variable to see if keep looping Finally: Change (often increment) the loop control variable If you don’t, you won’t be able to exit the loop…infinite loop

While Loop

While Loop Used when you don’t know how many times you need to loop

While Loop Used when you don’t know how many times you need to loop Init the loop control variable outside and before the loop

While Loop Used when you don’t know how many times you need to loop Init the loop control variable outside and before the loop Check the loop control variable in the while statement condition

While Loop Used when you don’t know how many times you need to loop Init the loop control variable outside and before the loop Check the loop control variable in the while statement condition Change the loop control variable in the body of the while loop

While Loop Used when you don’t know how many times you need to loop Init the loop control variable outside and before the loop Check the loop control variable in the while statement condition Change the loop control variable in the body of the while loop Often increment the loop control variable when simply counting

While Loop while ( check loop control variable) Used when you don’t know how many times you need to loop Init the loop control variable outside and before the loop Check the loop control variable in the while statement condition Change the loop control variable in the body of the while loop Often increment the loop control variable when simply counting while ( check loop control variable)

While Loop Example ArrayList myList = new ArrayList(); // create the list myList.Add(4); // fill the list int position; // create loop control variable position = 0; // init loop control variable while (position < myList.Count ) // loop until thru the list { Console.WriteLine(“Next #: ”, myList[position]); position++; // increment counter }

Task Write a program using a while loop to print the first 5 integers Name the project “loops” Display each number on its own line with “Next #: Before the loop, display the message “While loop”

For Loop

For Loop Used when you *do* know how many times you need to loop

For Loop Used when you *do* know how many times you need to loop for (

For Loop for ( init the loop control variable ; Used when you *do* know how many times you need to loop for ( init the loop control variable ;

For Loop for ( init ; check the loop control variable ; Used when you *do* know how many times you need to loop for ( init ; check the loop control variable ;

For Loop for ( init ; check ; change the loop control variable) Used when you *do* know how many times you need to loop for ( init ; check ; change the loop control variable)

For Loop for ( init ; check ; change ) Used when you *do* know how many times you need to loop for ( init ; check ; change )

For Loop

For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; position++ { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable Check loop control variable Increment loop control variable

For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list

For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable

For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number

For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( { Console.WriteLine(“Looped” + counter + “ times.”); }

For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; { Console.WriteLine(“Looped” + counter + “ times.”); }

For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable

For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable

For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable Check loop control variable

For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; position++ ) { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable Check loop control variable

For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; position++ ) { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable Check loop control variable Increment loop control variable

For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; position++ ) { Console.WriteLine(“Looped” + counter + “ times.”); }

Task Add to your “loops” program: After your while loop… Add a for loop to print the first 5 integers Display each number on its own line with “Next #: Before the loop, display the message “For loop”

Foreach Loop

Foreach Loop Used to work on parts of a group, one at a time

Foreach Loop Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement

Foreach Loop Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable

Foreach Loop foreach ( Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable foreach (

Foreach Loop foreach ( create loop control variable Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable foreach ( create loop control variable

Foreach Loop foreach ( create loop control variable in Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable foreach ( create loop control variable in

Foreach Loop Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable foreach ( create loop control variable in group to work with )

Foreach Example

Foreach Example ArrayList myList = new ArrayList(); // create list

Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list

Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item

Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( ) { … }

Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList { … }

Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList { … } Create loop control variable

Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList in { … } Create loop control variable

Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList in myList) { … } Create loop control variable

Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList in myList) { … } Create loop control variable Automagically check & change loop control variable

Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList in myList) { if ( numFmList == matchNum ) … }

Task Add to your “loops” program: After your for loop… Create an ArrayList and fill it with the first 5 integers Use a foreach loop to print the first 5 integers from the ArrayList Display each number on its own line with “Next #: Before the loop, display the message “Foreach loop”

Clear and Unclear Windows