Computer Science Club 1st November 2019.

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

CS110 Programming Language I
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
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.
Computer Programming Lab(4).
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
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.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Repetition Statements while and do while loops
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Building java programs, chapter 5 Program logic and indefinite loops.
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:
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
 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.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
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.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Sophomore Scholars Java
CSC111 Quick Revision.
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 6 More Conditionals and Loops
Chapter 5: Control Structures II
Computer Programming Methodology Input and While Loop
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Repetition.
TK1114 Computer Programming
Topic 11 Scanner object, conditional execution
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Conditional Loops.
Building Java Programs
Building Java Programs
Module 4 Loops.
Control Statements Loops.
Building Java Programs
Control Statements Loops.
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Indefinite loop variations
Building Java Programs
Outline Boolean Expressions The if Statement Comparing Data
Building Java Programs
Welcome back! October 11, 2018.
Building Java Programs
Building Java Programs
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

Computer Science Club 1st November 2019

https://goo.gl/SGWMBB Attendance https://goo.gl/SGWMBB

While Loops While loops will run whatever code is inside of the loop until whatever parameter is set is no longer true. while(true) { parameter //code here }

While Loops Syntax boolean x = true; while(x == true) { System.out.println(“True!!”); x = false; } //this will print “True!!” once

While Loops Practice Problems Use a while loop to print all the numbers until n Use a while loop to print numbers from 10 to 2, decreasing Write an infinite while loop

While Loops Advanced Problems Use a while loop to print all characters in the alphabet Use a while loop to print all even numbers between 1 - 100 Then, all odd numbers between 1 and 100 Use a while loop to print the sum of numbers from 1 to n Use while loop to print a multiplication table for numbers from 1 - 12 Then, from 1 to n

Do While Loops boolean x = false; do { System.out.println(“Hello!”); } while(x == true); //this will print “Hello!” once

Do while loop project! Use a do-while loop to test whether a user has entered data of the correct form and, if not, ask repeatedly until the data entered is correct. Hint: this will require knowledge we learned previously. Can you remember?

Scanners Allow user input for your programs import java.util.Scanner; public class App { public static void main( String[] args ) Scanner scanner = new Scanner(System.in); }

Example of asking for input Scanner scanner = new Scanner(System.in); System.out.print("What is your name: "); String userName = scanner.next();

Example for asking for a Number Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int num = scanner.nextInt();

Scanner Practice Problems Ask a user for their name and print their name again like “Hello Sam!” Keep asking for using input until they enter “quit” Investigate the .hasNextInt() method for Scanner to keep asking for user input until they enter a number Keep asking the user for numbers until they enter “quit”. Then print the sum of all the numbers entered by the user. If user enters 4 then 6 then 8 then quit, the output should be “Sum: 18” Extra Challenge: Output each number as well like “4 + 6 + 8 = 18” public static void main( String[] args ) { Scanner scanner = new Scanner(System.in); }

Thanks for coming to CS Club! Next Meeting - 15th Nov