Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
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,
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
CSC 142 I 1 CSC 142 Iterations [Reading: chapter 6]
Dialog Boxes.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
AP CS. Enhanced for loop for(object type, declared object) This loop is not necessarily iterative. It is ideal to traverse array arrays and array lists.
Loops. More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Nested For Loops. First, the rules to these loops do not change. We are sticking one loop inside another. while(condition) { // loop body } do { // loop.
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.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Computer Programming -1-
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must.
Loops A loop is: Java provides three forms for explicit loops:
4. Java language basics: Function
Categories of loops definite loop: Executes a known number of times.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
5. Function (2) and Exercises
Java for Beginners University Greenwich Computing At School DASCO
Iterative Constructs Review
Loop Structures.
Java for Beginners.
CMPT 201 Functions.
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.
Programming Fundamentals
Repetition.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Arrays, For loop While loop Do while loop
Conditional Branching
Java for Beginners University Greenwich Computing At School DASCO
Outline Altering flow of control Boolean expressions
More Loops.
More Loops.
Chapter 4: Loops and Files
Java Programming Loops
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
LOOPS BY: LAUREN & ROMEO.
For Loops.
Lec 4: while loop and do-while loop
Control structures to direct program flow
Loop Strategies Repetition Playbook.
Arrays.
Let’s all Repeat Together
A LESSON IN LOOPING What is a loop?
CIS 110: Introduction to Computer Programming
Java Programming Loops
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Random Numbers while loop
Input, Variables, and Mathematical Expressions
Chapter 4: Loops and Files
Computer Science Club 1st November 2019.
Presentation transcript:

Loops Review

How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1 = If we want integer we need to typecast this thing int num1 = (int)Math.random(); num1 = 0;  This is because integer conversion drops decimals int num1 = (int)(Math.random()*100); num1 = 69;  Multiply the 100 before conversion.

For loop declaration | condition | iteration for(int k = 0; k < 10; k++) This is how this loop reads: Start integer k at 0. Loop continues as long as k is less than 10 Increase the value of k by 1 every time you loop

For loop for(int k = 0; k < 100; k++) for(int k = 10; k < 10; k++) for(int k = 0; k < 100; k += 5) for(int k = 0; k > 5; k++)

While Loops while(condition) { // body of code } Unlike a for loop which is an iteration loop, a while loop is a conditional loop. It executes as long as the condition is true. The condition must be a logical operation.

Do while Loops do { // body of code } while(condition); Like a while loop, a do while loop is a conditional loop. However, a do while loop will execute at least once before checking its condition. Essentially, the do while loop checks its condition last.

Example Scanner type = new Scanner(System.in) int X = 0; do { System.out.println(“Please enter a number”); X = type.nextInt(); }while(X % 5 != 0);

Example int k = 0; while(k < 50) { k = (int)(Math.random()*100); System.out.println(k); }

Exercise 1 Write a while loop that generates random numbers between repeatedly inside that loop. The loop stops when the number generated is greater than 80 and is an even number.

Exercise 2 Ask user to input a value between Get the squared root of that number. Run a for loop as many times as that number. Display the value of that number every time you loop.

Exercise 3 Use a double for loop to display the following