Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Important Notes Pay attention to file names!!! You must name them what we tell you to. mkdir – make a folder (make the ones we tell you to) rm – remove.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
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.
LAB 10.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Computer Programming Lab(5).
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 5 Loops.
BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4: Control Structures II
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Loops Copyright © 2012 Pearson Education, Inc.. Conditionals and Loops (Chapter 5) So far, we’ve looked at: –making decisions with if –how to compare.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
Array Review Selection Sort Get out your notes.. Learning Objectives Be able to dry run programs that use arrays Be able to dry run programs that use.
COS 312 DAY 5 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 2 Posted – Due Feb 22 prior to class – Any issues? Assignment 3 will be posted soon Capstones.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Slides by Evan Gallagher
Slides by Evan Gallagher
CSC111 Quick Revision.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
using System; namespace Demo01 { class Program
CSC 1051 – Data Structures and Algorithms I
Chapter 6 More Conditionals and Loops
Maha AlSaif Maryam AlQattan
Repetition-Sentinel,Flag Loop/Do_While
Repetition.
TK1114 Computer Programming
SELECTION STATEMENTS (1)
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Chapter 6 More Conditionals and Loops
CSS161: Fundamentals of Computing
The for-loop and Nested loops
Introduction to Classes and Methods
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
class PrintOnetoTen { public static void main(String args[]) {
Module 3 Selection Structures 4/3/2019 CSE 1321 Module 3.
Methods and Data Passing
Module 4 Loops and Repetition 4/4/2019 CSE 1321 Module 4.
CSE Module 1 A Programming Primer
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Module 4 Loops and Repetition 4/15/2019 CSE 1321 Module 4.
Repetition Statements
Outline Boolean Expressions The if Statement Comparing Data
Lecture 1 Review of 1301/1321 CSE /26/2018.
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4

Ps Initial Problem int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter Ps

Initial Problem int counter = 1; while (counter <= 1000) { System.out.println (counter + “ I will not…”); counter++; }

Pseudocode – Input Validation Example userGuess ← 0, secretNumber ← 5 PRINT “Enter a number between 1 and 10” READ userGuess WHILE (userGuess < 1 OR userGuess > 10) PRINT “That is not between 1 and 10. Try again.” READ userGuess ENDWHILE IF (userGuess == secretNum) THEN PRINT “That’s right! ”, userGuess, “ is the secret!” ELSE PRINT userGuess, “ is not the secret!” ENDIF Ps 4/7/2019 CSE 1321 Module 4

Input Validation Example public static void main (String[] args) { int userGuess = 0, secretNum = 5; Scanner scan = new Scanner (System.in); System.out.println("Enter a number between 1 and 10: "); userGuess = scan.nextInt(); while(userGuess < 1 || userGuess > 10) System.out.println ("Not between 1 and 10. Try again!"); } if (userGuess == secretNum) System.out.println(userGuess + " is the secret number!"); else System.out.println(userGuess + " isn’t the secret number!"); 4/7/2019 CSE 1321 Module 4

Pseudocode – do-while Loop Example CREATE number ← 0, lastDigit ← 0, reverse ← 0 PRINT “Enter a positive number.” READ number DO lastDigit ← number % 10 reverse ← (reverse * 10) + lastDigit number ← number / 10 WHILE (number > 0) ENDDO Ps 4/7/2019 CSE 1321 Module 4

Java - do-while Loop Example public static void main (String[] args) { int number, lastDigit, reverse = 0; Scanner scan = new Scanner (System.in); System.out.print ("Enter a positive integer: "); number = scan.nextInt(); do lastDigit = number % 10; reverse = (reverse * 10) + lastDigit; number = number / 10; } while (number > 0); // NOTE THE SEMICOLON!!!!!!!!!!! System.out.println ("The reversed is " + reverse); } 4/7/2019 CSE 1321 Module 4

Pseudocode – for Loop Example CREATE userChoice ← 0 PRINT “Choose a number to see the multiplication table.” READ userChoice FOR (multiplier ← 1, multiplier < 12, multiplier ← multiplier + 1) PRINT userChoice, “ * “, multiplier, “ = “, (multiplier * userChoice) ENDFOR Ps 4/7/2019 CSE 1321 Module 4

Java - for Loop Example public static void Main(String[] args) { int choice = 0; Scanner scan = new Scanner (System.in); System.out.println("Enter a number to see the table."); choice = scan.nextInt(); for(int multiplier = 1; multiplier <= 12; multiplier += 1) System.out.println (multiplier + " * " + choice + " = " + (multiplier * choice)); } 4/7/2019 CSE 1321 Module 4

Pseudocode – Nested for Loop Example CREATE maxRows ← 10, row, star FOR (row ← 1, row < maxRows, row ← row + 1) FOR (star ← 1, star <= row, star ← star + 1) PRINT “*” ENDinnerFOR PRINTLINE () ENDouterFOR Ps 4/7/2019 CSE 1321 Module 4

Java – Nested for Loop Example public static void main (String[] args) { int maxRows = 10; for (int row = 1; row <= maxRows; row++) { for (int star = 1; star <= row; star++) { System.out.print ("*"); } System.out.println(); } } 4/7/2019 CSE 1321 Module 4