Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213

Slides:



Advertisements
Similar presentations
Iteration While / until/ for loop. Iteration: while/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initalise.
Advertisements

Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 10: Java: Strings Stewart Blakeway FML 213
Karel – Chapter 6 Instructions That Repeat
How SAS implements structured programming constructs
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 5: Steps in Problem Solving Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 8: Java: Selection and Repetition Stewart Blakeway.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 6: Problem Solving Exercises Stewart Blakeway FML.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 3 Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 7: Java basics Stewart Blakeway
Faculty of Sciences and Social Sciences HOPE PHP Flow Control Website Development Stewart Blakeway FML
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 12: Data Structures 1 Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Examination Revision Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Data Structures 2 Stewart Blakeway
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving An Introduction Stewart Blakeway
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
For Next Looping My first Looping Structure. For…Next FOR counter_variable = initial_value TO end_value STEP increment Statement-1 Statement-2 ……. Statement-n.
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.
Copyright © Texas Education Agency, Computer Programming For Loops.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
CIS Computer Programming Logic
Faculty of Sciences and Social Sciences HOPE JavaScript Advanced Stewart Blakeway FML
BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Repetition Statements while and do while loops
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 3: Algorithms Stewart Blakeway FML 213
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
Control Structures: Conditionals, If/Else and Loops David Millard
Chapter 4 Repetition Statements (loops)
Karel J Robot Chapter 6.
Think What will be the output?
Topic 5 for Loops -Arthur Schopenhauer
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Outline Altering flow of control Boolean expressions
Computers & Programming Languages
Introduction to Computer Programming Counting Loops 2
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Lecture Notes – Week 3 Lecture-2
MSIS 655 Advanced Business Applications Programming
Structured Program
Building Java Programs
Computer Science Core Concepts
ICT Gaming Lesson 2.
Introduction to Computer Science
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
The structure of programming
Thinking procedurally
Presentation transcript:

Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213

Faculty of Sciences and Social Sciences HOPE What we have done already Seen what an algorithm is – a set of instructions that, if carried out, will lead to a successful conclusion Learned how to represent algorithms in – Structured English – Flow charts Used variables to remember 2

Faculty of Sciences and Social Sciences HOPE What we have done already Applied the top down, stepwise refinement approach to creating algorithms Looked at problems more oriented towards being solved on a computer – stacks, queues, functions, procedures Seen how high level languages like Java are used to create programs with the aide of compilers The problem solving constructs in Java 3

Faculty of Sciences and Social Sciences HOPE What we shall do today Nested for loops 4

Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } 5 Loop Recap ONLY when NOT already executing loop 1

Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } 6 2

Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } 7 3 execute instruction. execute instruction TRUE

Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } 8 4

Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } 9 5

Faculty of Sciences and Social Sciences HOPE Repetition Keep doing (iterating, repeating, looping) Until 2 evaluates to FALSE 2,3,4 and 5

Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } execute instruction. execute instruction FALSE

Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } Will do this 3 times 12 Loop Recap

Faculty of Sciences and Social Sciences HOPE for (j = 1; j <= 5; j++) { } Will do this 5 times 13

Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } Will do this 3 times 14 for (j = 1; j <= 5; j++) { } Will do this 5 times

Faculty of Sciences and Social Sciences HOPE i = 1 i = 2 i = 3 j = 1, 2, 3, 4, 5 15

Faculty of Sciences and Social Sciences HOPE A variation of a loop within a loop 16 for (i = 1; i <= 3; i++) { } Will do this 3 times for (j = 1; j <= i; j++) { } How many times?

Faculty of Sciences and Social Sciences HOPE A variation of a loop within a loop i = 1j goes from 1 TO 1 17 i = 2 i = 3 j goes from 1 TO 2 j goes from 1 TO 3

Faculty of Sciences and Social Sciences HOPE 18 Pencil & Paper exercises in pairs Predict the resulting output for each of the following programs.

Faculty of Sciences and Social Sciences HOPE int i; int j; for ( i = 1; i <= 5; i++) { for ( j = 1; j <= 8; j++) { System.out.print("X"); } System.out.println(); } XXXXXXXX 19 Exercise 1

Faculty of Sciences and Social Sciences HOPE int i; int j; for ( i = 1; i <= 5; i++) { for ( j = 1; j <= i; j++) { System.out.print("X"); } System.out.println(); } 20 Exercise 2

Faculty of Sciences and Social Sciences HOPE int i; int j; for ( i = 1; i <= 5; i++) { for ( j = 1; j <= i; j++) { System.out.print("X"); } System.out.println(); } X XX XXX XXXX XXXXX 21 Exercise 2

Faculty of Sciences and Social Sciences HOPE int i; int j; for ( i = 5; i >= 1; i--) { for ( j = 1; j <= i; j++) { System.out.print("X"); } System.out.println(); } 22 Exercise 3

Faculty of Sciences and Social Sciences HOPE int i; int j; for ( i = 5; i >= 1; i--) { for ( j = 1; j <= i; j++) { System.out.print("X"); } System.out.println(); } XXXXX XXXX XXX XX X 23 Exercise 3

Faculty of Sciences and Social Sciences HOPE int i; int j; int k; for ( i = 1; i <= 4; i++) { for ( j = 1; j <= i ; j++) { for ( k = 1; k<= j; k++) { System.out.print("X"); } System.out.println(); } System.out.println(); } X X XX X XXX X XX XXX XXXX 24 Exercise 4

Faculty of Sciences and Social Sciences HOPE int i; int j; int k; for ( i = 1; i <= 4; i++) { for ( j = 1; j <= i ; j++) { for ( k = 1; k<= j; k++) { System.out.print("X"); } System.out.println(); } System.out.println(); } X X XX X XXX X XX XXX XXXX 25 Exercise 4

Faculty of Sciences and Social Sciences HOPE int i; int j; int k; int count; for ( i = 1; i <= 5; i++) { count = 0; for ( j = 1; j <= i; j++) { for ( k = 1; k<= j; k++) { count++; } System.out.println(count); } Exercise 5

Faculty of Sciences and Social Sciences HOPE Careful with Loops! for (i = 1; i <= 3; i++) { } for (i = 1; i <= 5; i++) { }

Faculty of Sciences and Social Sciences HOPE Careful with Loops! for (i = 1; i <= 5; i++) { } for (i = 1; i <= 3; i++) { }

Faculty of Sciences and Social Sciences HOPE Careful with Loops! for (i = 1; i <= 3; i++) { } for (i = 1; i <= j; j++) { }

Faculty of Sciences and Social Sciences HOPE Any Questions? Next Week – Strings