Unit 3 - The while Loop - Extending the Vic class - Examples

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
1 Repetition structures Overview while statement for statement do while statement.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Looping Yong Choi School of Business CSU, Bakersfield.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
UNIT II Decision Making And Branching Decision Making And Looping
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Repetition Statements while and do while loops
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Iterative Statements: while, for, do-while Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
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
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
Control Structures.
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Counted Loops.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Building Java Programs
While Statement.
Building Java Programs
Loops October 10, 2017.
Iteration with While You can say that again.
Conditional Loops.
Java I.
LRobot Game.
Building Java Programs
Chapter 8: More on the Repetition Structure
Chapter 6: Repetition Statements
A LESSON IN LOOPING What is a loop?
Seating “chart” Front - Screen rows Back DOOR.
PROGRAM FLOWCHART Iteration Statements.
Statements in C Programming
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops CGS3416 Spring 2019 Lecture 7.
Control Statements:.
Presentation transcript:

Unit 3 - The while Loop - Extending the Vic class - Examples

Loops in Java A structure that allows the programmer to repeat something while a condition is true. (also called “iteration”) Three kinds of loops in Java 1. The “while” loop // we will see today 2. The “for” loop // we will see next lesson 3. The “do-while” loop // not on AP exam…in your book

Loops in Java The “while” loop – used when we want to repeat something while a condition remains true. Syntax: while(condition) { statement; … } The program will execute these statements while the condition remains true. When the condition becomes false, the program will resume after the curly brace.

Example Rewrite Program 2 from Unit 7 using less than 10 lines of program code.

public class Program2 { public static void main (String[ ] args) Vic one = new Vic(); Vic two = new Vic(); while(one.seesSlot() && two.seesSlot()) if (!two.seesCD() && one.seesCD()) one.takeCD(); two.putCD(); } one.moveOn(); two.moveOn(); } … // Repetition done in the loop!! public class Program2 { public static void main (String[ ] args) Vic one = new Vic(); Vic two = new Vic(); if(one.seesSlot() && two.seesSlot()) if (!two.seesCD() && one.seesCD()) one.takeCD(); two.putCD(); } one.moveOn(); two.moveOn(); … // Repeat this code 8 times

Example: Explain what this application program does. public class Mystery { public static void main (String [] args) Vic.reset(args); Vic v = new Vic(); while(Vic.stackHasCD()) v.putCD(); v = new Vic(); }

The while Loop (cont’d) Example: // Returns the smallest n // such that 2^n >= x public static int intLog2 (int x) { int n = 0, p = 1; while ( p < x ) p *= 2; n++; } return n; Initialization Testing Change

The while Loop (cont’d) Initialization: The variables tested in the condition must be initialized to some values. If the condition is false at the outset, the loop is never entered. Testing: The condition is tested before each iteration. If false, the program continues with the first statement after the loop. Change: At least one of the variables tested in the condition must change within the body of the loop.

Loop Invariants A loop invariant is an assertion that is true before the loop and at the end of each iteration. Invariants help us reason about the code. int n = 0, p = 1; while (p < x) { p *= 2; n++; } ... Loop invariant: p = 2n

Extending the Vic Shortcoming: Vic does not have a method to go back to the front of the sequence (the first slot). Extension: called inheritance in OOP. Allows us to keep all of the qualities of a class, plus add any new ones. Introducing the DualDirectionVic Allows us to keep all of the qualities of a Vic, plus add any new ones. The new ones: 1. A method to back up to the first slot. 2. A method to go to the last slot. 3. A boolean method to check for first slot.

class DualDirectionVic extends Vic { private String itsFirstPosition; public DualDirectionVic() super(); itsFirstPosition = getPosition(); } public void goToFirst() while(!seesFirstSlot()) backUp(); public void goToLast() while(seesSlot()) moveOn(); public boolean seesFirstSlot() if(itsFirstPosition.equals(getPosition())) return true; else return false;