Repetition Structures: Do-while Loop Random Number Generation CSC 1401: Introduction to Programming with Java Week 6 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
Looping Structures: Do Loops
Advertisements

LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Repeating Structures Do While Loops. Do While Loop While loops have a test condition before the loop –This means that java will test the condition before.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Repetition Structures: Nested Loops CSC 1401: Introduction to Programming with Java Week 6 – Lecture 2 Wanda M. Kunkle.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle.
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
File Input and Output CSC 1401: Introduction to Programming with Java Week 4 – Lecture 2 Wanda M. Kunkle.
Introduction to Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lecture 1 Wanda M. Kunkle.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 4: Loops and Files
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.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Algorithm Design.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Iteration Hussein Suleman UCT Dept of Computer Science CS115 ~ 2004.
 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 Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
ARITHMETIC OPERATORS COMPARISON/RELATIO NAL OPERATORS LOGIC OPERATORS ()Parenthesis>Greater than &&And ^Exponentiation=>=
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 05 (Part II) Control Statements: Part II.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
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.
UCT Department of Computer Science Computer Science 1015F Iteration
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
Unit-1 Introduction to Java
Chapter 2.2 Control Structures (Iteration)
JavaScript: Control Statements I
الحلقات التكرارية وجمل الدوران (loops)
MSIS 655 Advanced Business Applications Programming
do/while Selection Structure
Lec 6 Loop Statements Introduction to Computer Programming
Presentation transcript:

Repetition Structures: Do-while Loop Random Number Generation CSC 1401: Introduction to Programming with Java Week 6 Wanda M. Kunkle

Repetition Structures do-while do-while Usage: Usage: Used to specify that an action is to be repeated as long as some condition remains true Used to specify that an action is to be repeated as long as some condition remains true Always executes at least once (Why?) Always executes at least once (Why?) Format: do { // Braces are only needed when there // are multiple statements Statement(s) } while (Condition);// Note the semicolon after the condition Format: do { // Braces are only needed when there // are multiple statements Statement(s) } while (Condition);// Note the semicolon after the condition Example: int counter = 0; do { out.writeln(counter); // Display counter counter = counter + 1; // Increment counter by 1 } while (counter < 100); // Loops as long as counter is less than 100 Example: int counter = 0; do { out.writeln(counter); // Display counter counter = counter + 1; // Increment counter by 1 } while (counter < 100); // Loops as long as counter is less than 100

Repetition Structures Examples of all three types: int counter = 0; Examples of all three types: int counter = 0; for (counter = 0; counter < 100; counter++) out.writeln(counter); for (counter = 0; counter < 100; counter++) out.writeln(counter); while (counter < 100) { out.writeln(counter); counter = counter + 1; } while (counter < 100) { out.writeln(counter); counter = counter + 1; } do { out.writeln(counter); counter = counter + 1; } while (counter < 100); do { out.writeln(counter); counter = counter + 1; } while (counter < 100);

Sample Programs Now let’s look at a sample program that demonstrates the use of the do-while loop (It should look familiar!): Now let’s look at a sample program that demonstrates the use of the do-while loop (It should look familiar!): twoPowersWithDoWhile.java twoPowersWithDoWhile.java twoPowersWithDoWhile.java

(Pseudo) Random Numbers A computer cannot perform a task without being provided with instructions on how to do so. A computer cannot perform a task without being provided with instructions on how to do so. It is therefore logical to assume that a computer is not capable of generating truly random numbers. It is therefore logical to assume that a computer is not capable of generating truly random numbers. Since it does not possess this capability, we call the so-called random numbers generated by a computer pseudo random numbers. Since it does not possess this capability, we call the so-called random numbers generated by a computer pseudo random numbers.

(Pseudo) Random Numbers The software that comes with our book provides us with a class for generating (pseudo) random numbers. The software that comes with our book provides us with a class for generating (pseudo) random numbers. To use it, we must specify a range in which to generate the numbers. To use it, we must specify a range in which to generate the numbers. Example: // Generate a number in the range 0 through 1, // inclusive, to represent the possible values (tails vs. // heads) when a coin is flipped random toss = new random(0, 1); // Retrieve an integer within that range int flip = toss.readint(); Example: // Generate a number in the range 0 through 1, // inclusive, to represent the possible values (tails vs. // heads) when a coin is flipped random toss = new random(0, 1); // Retrieve an integer within that range int flip = toss.readint();