Lecture Notes – Week 3 Lecture-2

Slides:



Advertisements
Similar presentations
Java Control Statements
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.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Loops.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
CS100J October 07, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
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.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 5 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Repetition Statements while and do while loops
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 9 Control Structures.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Follow up from lab See Magic8Ball.java Issues that you ran into.
Loops A loop is: Java provides three forms for explicit loops:
Chapter 9 Repetition.
Lecture 4b Repeating With Loops
CSC111 Quick Revision.
Chapter 4 Repetition Statements (loops)
ECE Application Programming
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Topic 5 for Loops -Arthur Schopenhauer
Chapter 4 Control structures and Loops
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
OPERATORS (2) CSC 111.
The University of Texas Rio Grande Valley
The University of Texas – Pan American
Outline Altering flow of control Boolean expressions
Chapter 9 Control Structures.
Repetition Control Structure
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Lab5 PROGRAMMING 1 Loop chapter4.
Building Java Programs
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Lecture Notes – Week 4 Chapter 5 (Loops).
Lecture Notes – Week 2 Lecture-2
Week 4 Lecture-2 Chapter 6 (Methods).
Lecture Notes – Week 2 Lecture-2
Seating “chart” Front - Screen rows Back DOOR.
PROGRAM FLOWCHART Iteration Statements.
The for-statement.
Building Java Programs
Python While Loops.
Lec 6 Loop Statements Introduction to Computer Programming
Chapter 3 Flow of Control Loops in Java.
Chapter 4: Loops and Iteration
Presentation transcript:

Lecture Notes – Week 3 Lecture-2 Chapter 5 (Loops)

Outline Introduction to loops Three forms of loops To do list while do-while for To do list

Introduction to Loops Motivating example: Example: Print "Welcome to Java!” 100 times. How would we do it? Write the following statement 100 times? System.out.println("Welcome to Java!"); That would be too tedious! We would need to write a loop.

Loops Loops are constructs that control repeated execution of a block of statements. int count = 0; while (count < 100) { System.out.println("Welcome to Java!"); count++; } A while loop starts with keyword while, followed by a Boolean expression for the condition, then followed by a statement body. We also need to declare/initialise a variable for counting.

while Statements } while (Boolean-expression) { statement(s); int count = 0; while (count < 100) { System.out.println(”Welcome to Java!”); count++; } while (Boolean-expression) { statement(s); } 5 5

Trace while Loop Initialize count to 0 int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } 6 6 6

While (count < 2) is true Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } While (count < 2) is true (count is 0 at moment) 7 7 7 7

Print “Welcome to Java!” Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Print “Welcome to Java!” 8 8 8 8 8

(count is increased to 1 now) Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Increase count by 1 (count is increased to 1 now) 9 9 9 9 9 9

While (count < 2) is true Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } While (count < 2) is true (count is 1 at moment) 10 10 10 10 10

Print “Welcome to Java!” Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Print “Welcome to Java!” 11 11 11 11 11 11

(count is increased to 2 now) Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Increase count by 1 (count is increased to 2 now) 12 12 12 12 12 12 12

While (count < 2) is false Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } While (count < 2) is false (count is 2) 13 13 13 13 13 13

Exit the loop and execute the next statement after the loop. Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Exit the loop and execute the next statement after the loop. 14 14 14 14 14 14 14 14

Increment and decrement operators 15

do-while Statements Now compare it to: do { statement(s); } while (Boolean-expression); int count = 0; do { System.out.println(”Welcome to Java!”); count++; } while (count < 100); Now compare it to: int count = 0; while (count < 100) { System.out.println(”Welcome to Java!”); count++; } 16 16 16

for Statements for (initial-action; Boolean-expression; action-after) { statement(s); }; int i; for (i = 0; i < 100; i++) { System.out.println(”Welcome to Java!”); }; 17 17 17

Trace for Loop Declare variable i int i; for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } ; 18 18 18

Trace for Loop int i; for (i = 0; i < 2; i++) { Initialise I to 0 System.out.println("Welcome to Java!"); } ; Initialise I to 0 (i is 0) 19 19 19 19

Trace for Loop int i; for (i = 0; i < 2; i++) { (i < 2) is true System.out.println("Welcome to Java!"); } ; (i < 2) is true (i is 0) 20 20 20 20 20

Print “Welcome to Java!” Trace for Loop int i; for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } ; Print “Welcome to Java!” 21 21 21 21 21 21 21

Trace for Loop int i; Increase i by 1 for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } ; Increase i by 1 (i is increased to 1) 22 22 22 22 22 22 22

Trace for Loop int i; for (i = 0; i < 2; i++) { (i < 2) is true System.out.println("Welcome to Java!"); } ; (i < 2) is true (i is 1) 23 23 23 23 23

Print “Welcome to Java!” Trace for Loop int i; for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } ; Print “Welcome to Java!” 24 24 24 24 24 24 24

Trace for Loop int i; Increase i by 1 for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } ; Increase i by 1 (i is increased to 2) 25 25 25 25 25 25 25

Trace for Loop int i; for (i = 0; i < 2; i++) { (i < 2) is false System.out.println("Welcome to Java!"); } ; (i < 2) is false (i is 2) 26 26 26 26 26 26

Exit the loop and execute the next statement after the loop. Trace for Loop int i; for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } ; Exit the loop and execute the next statement after the loop. 27 27 27 27 27 27 27

Exercise Write a program that displays the squares of the numbers from 0 to 15. Using For Loop While Loop Do-While Loop Output should be as : 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225

To Do List Before Next Lecture Read Week 3 lecture slides. Read and run Week 3 program examples. Selectively read those sections in Chapter 5 that cover the topics in this lecture. Attend your practical session in Week 4, consisting of a tutorial and a set of programming exercises. Glance through those sections in Chapter 6 that cover the topics in Week 4’s lecture.