Java for Beginners.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

CS0007: Introduction to Computer Programming
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
* 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.
Java for Beginners University Greenwich Computing At School DASCO
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Selection Using IF THEN ELSE CASE Introducing Loops.
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.
Java for Beginners University Greenwich Computing At School DASCO
Lecture 4b Repeating With Loops
CSC111 Quick Revision.
Java for Beginners University Greenwich Computing At School DASCO
REPETITION CONTROL STRUCTURE
Java for Beginners Level 6 University Greenwich Computing At School
Java for Beginners University Greenwich Computing At School DASCO
while Repetition Structure
Java for Beginners University Greenwich Computing At School DASCO
Loop Structures.
Chapter 5: Control Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
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.
ALGORITHMS & FLOWCHARTING II
Repetition-Counter control Loop
( Iteration / Repetition / Looping )
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Manipulating Pictures, Arrays, and Loops part 2
Control Structure Senior Lecturer
While Loops.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Iteration with While You can say that again.
Java for Beginners University Greenwich Computing At School DASCO
Java for Beginners University Greenwich Computing At School DASCO
Java for Teachers Intermediate
Introduction to pseudocode
Coding Concepts (Sub- Programs)
Lec 4: while loop and do-while loop
Building Java Programs
Console.WriteLine(“Good luck!”);
Java for Beginners University Greenwich Computing At School DASCO
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Repetition Statements (Loops) - 2
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
PROGRAM FLOWCHART Iteration Statements.
Announcements Lab 3 was due today Assignment 2 due next Wednesday
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.
Random Numbers while loop
Presentation transcript:

Java for Beginners

Levels of Java coding 1: Syntax, laws, variables, output 2: Input, calculations, String manipulation 3: Selection (IF-ELSE) 4: Iteration/Loops (FOR/WHILE) 5: Complex algorithms 6: Arrays 7: File management 8: Methods 9: Objects and classes 10: Graphical user interface elements

What do you learn last time? Wordle.org

for(counter; condition; change) A logic condition (like in IF) that has to be true for the loop to continue. Something Operator Something (usually involving the counter variable) A simple variable (usually an int) that allows the loop to ‘step through’ Normally called “i” for(counter; condition; change) What should happen to the counter variable value at the end of the loop Usually i++ or i--

What is the difference? “Now count from one to ten…” Loops FOR Known number of repetitions WHILE Unknown number of repetitions “Now count from one to ten…” “Are we there yet?...”

condition variable while(condition) change happens inside loop A variable is created and given a value so that the loop will run. Usually String, int or boolean A logic condition (like in IF) that has to be true for the loop to continue. Something Operator Something (usually involving the counter variable) condition variable while(condition) change happens inside loop There must be an opportunity for the condition variable to change, sometimes done in an IF block

Typical example (while loop) Create int called i Set i to 0 Continue while i is less than 3 int i = 0; while (i < 3) { System.out.println(“gum”); i++; } At end of for loop, increase i by 1 Output gum

Another example (while loop) boolean done = false; while (done == false) { System.out.println(“Are you done? Y/N ”) String answer = kb.nextLine(); if (answer.equals(“Y”) ) done = true; } System.out.println(“Goodbye!”); Create boolean called done. Set to false Continue while done is false If user enters Y, set done to true Output Are you done? Y/N Y Goodbye!

Students struggle with… FOR LOOPS – WHILE not so much Some students will bizarely understand while loops much easier than for loops. They can use whichever on they prefer Most common mistake: while(done == false);

Practice time Generate two random numbers between 1 and 10. Calculate their sum (num1 + num2). Ask the user for their sum. If they get it right, the program congratulates them and ends. If they get it wrong, the program repeats by generating two more numbers and asking the user again… Read problem Make list of sub-tasks Draw flowchart Code in Java

False True Display num1 and num 2 Sum: num1+ num2 Num2: random number between 1 and 10 Ask user for total Num1: random number between 1 and 10 if total = sum False Start True Display Well done! End

Possible solution boolean done = false; while (done == false) { int num1 = 1 +(int)(Math.random()*((10 - 1) + 1)); int num2 = 1 +(int)(Math.random()*((10 - 1) + 1)); int sum = num1 + num2; System.out.println(“What is ”+num1+” + “+num2+” ?”); String answer = kb.nextLine(); int total = Integer.parseInt(answer); if (total == sum) done = true; } System.out.println(“Well done!”);