Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
The switch Statement, DecimalFormat, and Introduction to Looping
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
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");
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Lecture 8: Bits and Pieces Tami Meredith. Roadmap Today's lecture has a bit of everything. Going back over previous chapters and pulling out little bits.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
By Mr. Muhammad Pervez Akhtar
Expressions Methods if else Statements Loops Potpourri.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
JAVA PROGRAMMING Control Flow. Jeroo: Finish Activities 1-7 Finish InputTest program w/changes.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
John Hurley Spring 2011 Cal State LA CS 201 Lecture 5:
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Repetition Statements
Switch statement.
Java Fundamentals 4.
Chapter 4 – C Program Control
INTERMEDIATE PROGRAMMING USING JAVA
Input/Output.
The setw Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where.
Chapter 5: Control Structures II
The switch Statement, and Introduction to Looping
Chapter 5: Control Structures II
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
User input We’ve seen how to use the standard output buffer
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
Expressions and Control Flow in JavaScript
Chapter 5: Control Structures II
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
While Statement.
Starting JavaProgramming
Lec 4: while loop and do-while loop
CS 200 Loops Jim Williams, PhD.
The Java switch Statement
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Arrays October 6, 2006 ComS 207: Programming I (in Java)
CIS 110: Introduction to Computer Programming
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Dale Roberts, Lecturer IUPUI
Repetition Statements
Building Java Programs
Chap 7. Advanced Control Statements in Java
Conditionals and Loops
Loops CGS3416 Spring 2019 Lecture 7.
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Chapter 13 Control Structures
Presentation transcript:

Lecture 4 CS140 Dick Steflik

Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using regular expressions. Can be used to extract tokens from strings, System.in, files, Streams…. Import java.util.Scanner; // System.in is the keyboard; create a Scanner for the keyboard Scanner in = new Scanner(System.in); // print a prompt System.out.print("Enter an integer"); int myInteger = in.nextInt();

Output System.out.print( ) System.out.println( ) System.out.printf("formatting string", v0,v1…)

Decisions This should be review Since C, C++ and Java are all syntactically very similar the if statement should be familiar. The general format is: // in its simplest form if (some predicate to evaluate to true or false) // a statement (simple or complex) { … }; -or- // slightly more complex if (some predicate to evaluate to true or false) // a statement (simple or complex) { … }; else // a statement (simple or complex) { … };

Statements A simple statement ends with a ";" character can stand on their own Compound statements (sequences of simple statements) must be enclosed between "{ }" characters. As a matter of coding style if the then (assumed) part of an if or the else part of an if is a simple statement surround the statement with "{ }"; its safer in the long run if (a == b) { c = 5; } else { c = 6;}

Decisions (more) Note: the ";" belongs as the terminator of the simple statement, not the compound statement if (a == b) { c = 5; d = d * c + a; } else { c = 6; d = d * c + b; }

Decisions (more) We can also use if to make ladders/filters/demultoplexers: if (expression) { statement } else if (expression) { statement } else if ( expression ) { statement } else if (expression) { statement } else // optional { statement }

Decisions (more) if is used to make a 1 of 2 decision; if true do this else do that another way to accomplish what an if ladder does is to use the switch statement: switch (value) { case 1: statement ; break; case 2: statement ; break; case 3: statement ; break; default: statement; break; } is equivalent to: if (value ==1) statement ; else if (value == 2) statement ; else if (value == 3) statement; else statement;

Strings Something to note about using Strings in the predicate of an if statement – == can be used to test primitives for equality but not objects – Strings are immutable objects, objects must have their own methods for doing comparisons The String class has a method called equals that takes a string as its argument and compares the value of the String to that argument and returns true or false: Scanner in = new Scanner(System.in); String name = in.next(); String dick = "Dick Steflik"; if ( name.equals(dick) // it must be Dick else // it isn't Dick

Loops Definite iteration; we know how many times we want to execute the body – for Indefinite iteration, we don't know how many times we want to execute the body – while – may never execute the body of the loop – do – will always execute the body of the loop at least once

for general form is: for ( initialization ; termination ; increment) { … } ex: for (i=0 ; i < 10 ; i++) System.out.print( i + " "); // prints : for ( i=0 ; i < 10 ; i = i + 2) System.out.print(I + " "); // prints:

while something in the body of the loop or something happening before entering the loop must terminate the loop : while (predicate) { body of loop} ex int v = 10; while (v != 0) { System.out.print(v + " "); v--; } // print: In this case the variable v is called a sentinal value, it is being watched to know when to terminate the loop note that if the loop were entered with v set to 0 then the body would never be executed

do in some languages this construct is called a do/until loop do {body of loop} while (some predicate) ex: int v = 0 do { System.out.print( v + " ") v=v+1; } while ( v != 10) // prints : note : if v was > 10 at the beginning of the loop we would have a never terminating loop