CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,

Slides:



Advertisements
Similar presentations
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
Advertisements

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.
CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
JAVA Control Statement.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
1 Interactive Applications (CLI) Interactive Applications Command Line Interfaces Project 1: Calculating BMI Example: Factoring the Solution Reading for.
Topic 11 Scanner object, conditional execution Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
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 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Lecture 2: Static Methods, if statements, homework uploader.
Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.
CSC 204 Programming I Loop I The while statement.
Chapter 5 Loops.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
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.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
Chapter 4: Control Structures II
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
1 Reasoning about assertions Readings: Assertions assertion: A statement that is either true or false. Examples:  Java was created in (true)
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
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.
Introduction to programming in java
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
(Dreaded) Quiz 2 Next Monday.
Slides by Evan Gallagher
Slides by Evan Gallagher
Chapter 2 Clarifications
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Introduction to programming in java
Chapter 6 More Conditionals and Loops
Repetition-Sentinel,Flag Loop/Do_While
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
SELECTION STATEMENTS (1)
BIT115: Introduction to Programming
Control Statement Examples
Building Java Programs
Outline Boolean Expressions The if Statement Comparing Data
Know for Quiz Everything through Last Week and Lab 7
Self study.
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Lecture Notes – Week 2 Lecture-2
CIS 110: Introduction to Computer Programming
Building Java Programs
Repetition Statements
CSC 1051 – Data Structures and Algorithms I
Building Java Programs
Building Java Programs
Outline Software Development Activities
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

CSCI S-1 Section 6

Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday, July 10, 10:00 EST, 1 Storey St., Room 306 Mid-Term Quiz – Monday, July 13, 15:15 EST, Science Center A

More Conditions We can set up conditions with a == b (equals), a != b (does not equal), a <= b (less than or equal to) or a >= b (greater than or equal to). We can also form more complex conditions using && (like AND in Scratch) and || (like OR in Scratch) if && Statement 1 else if || Statement 2 else Statement 3 Here, if BOTH condition 1 and condition 2 are true, then statement 1 will be executed. If, however, condition 1 or condition 2 are false, statement 1 will not be executed. If EITHER condition 3 or condition 4 is true, then statement 2 will be executed. If conditions 1-4 are false, statement 3 will be executed.

DeMorgan’s Law !(a && b) = !a II !b !(a II b) = !a && !b

Example 7 – Hungry // deduce hunger from the # of meals eaten so far and the time since the last meal. class Hungry { public static void main(String[] args) { System.out.print("Enter the number of meals you had today: "); System.out.print("Enter the number of hours elapsed since you ate: "); }

Example 7 – Hungry // deduce hunger from the # of meals eaten so far and the time since the last meal. import java.util.*; class Hungry { public static void main(String[] args) { System.out.print("Enter the number of meals you had today: "); Scanner scan = new Scanner(System.in); int meals = scan.nextInt(); System.out.print("Enter the number of hours elapsed since you ate: "); int hours = scan.nextInt(); if (meals 4) System.out.println("You are hungry!"); else if (meals > 3 || hours < 4) System.out.println("you are not hungry."); else System.out.println("You tell me!"); }

Example 8 – Error Checking // A man should weigh 106 pounds for the first 5 feet of height … // Write an error loop to prevent entry of height values less than 5. class ErrCheck { public static void main(String[] args) { }

Example 8 – Error Checking // A man should weigh 106 pounds for the first 5 feet of height … // Write an error loop to prevent entry of height values less than 5. import java.util.*; class ErrCheck { public static void main(String[] args) { Scanner keyboard= new Scanner (System.in); int feet= 0; while (feet <5) do { system.out.println(“please enter a value for height in feet, minimum 5: ”); feet= keyboard.nexInt(); }

Example 9 – Random Numbers // generate random numbers between 2-6. print out each hit. // then print out the hits as a % of TRIES numbers generated between 0-9 class Random1 { final int TRIES= 1000; public static void main(String[] args) { }

Example 9 – Random Numbers // generate random numbers between 2-6. print out each hit. // then print out the hits as a % of TRIES numbers generated between 0-9 class Random1 { final int TRIES= 1000; public static void main(String[] args) { double hits= 0; double d= 0; for (int i = 1; i <= TRIES; i++) { d= (10*Math.random()); if (d > 2 && d < 6) { hits++; System.out.println(d); } System.out.println( hits/TRIES + “ % of all attempts were between 2 and 6”); }