Download presentation

Presentation is loading. Please wait.

Published bySteven Munoz Modified over 5 years ago

1
COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

2
COMP 110: Spring 20092 Announcements Program 1 due tonight by midnight Lab 2 due tomorrow by midnight Lab 1 has been graded

3
COMP 110: Spring 20093 Questions?

4
COMP 110: Spring 20094 Today in COMP 110 Lab 1 In-Class Exercise Floating-Point Numbers Loops

5
COMP 110: Spring 20095 Floating-Point Numbers Floating-point numbers (double, float) store real numbers in a computer Numbers with some fractional component such as 8.5, 99.9, 3.14159 Floating-point numbers are approximate quantities and are not exact!

6
COMP 110: Spring 20096 Floating-Point Numbers Consider the number 1/3 = 0.333333 Computers have finite storage and cannot store infinitely repeating decimals In a computer, the number 1/3 is stored as 0.333333, with part of the fraction missing or truncated Since 0.333333 is < 1/3, this representation is inexact

7
COMP 110: Spring 20097 Floating-Point Numbers When using floating-point numbers, small errors will accumulate over time as operations are performed == tests whether two numbers are exactly equal You should never use == or != to compare floating-point numbers

8
COMP 110: Spring 20098 Floating-Point Comparison To compare two floating-point numbers, check if the difference is within some tolerance float a,b; final float EPSILON = 1e-6; //10^-6 … if(Math.abs(a – b) < EPSILON) //Math.abs() is absolute value System.out.println(a and b are the same!); else System.out.println(a and b are different!);

9
COMP 110: Spring 20099 Loops Programs often need to repeat some action For example, when grading a test, the process of grading is repeated for each student

10
COMP 110: Spring 200910 Loops A portion of a program that repeats some action is called a loop The statements that are repeated are called the loop body Each repetition of the loop body is called an iteration

11
COMP 110: Spring 200911 Loops All loops must also have a stopping condition Describes under what condition(s) iteration should cease Example When the process of grading is completed for all students, the instructor will stop grading

12
COMP 110: Spring 200912 Loop Example A loop that counts up to a certain number n Evaluate i <= n ? Execute Print i i = i + 1; Execute End Program Start i = 1 false true Loop Body Stopping Condition

13
COMP 110: Spring 200913 While Statements The while statement can be used to construct a loop in Java while(Boolean_Expression) { Statement_1 Statement_2 … Statement_N } As long as Boolean_Expression is true, the statements in the loop body are executed

14
COMP 110: Spring 200914 While Example A program that counts up to a certain number int number; … //number is set to some value, e.g. user input int count = 1; while(count <= number) { System.out.print(count + ", "); count++; }

15
COMP 110: Spring 200915 While Example int number = 4; int count = 1; while(count <= number) { System.out.print(count + ", "); count++; } IterationcountOutput 111, 221, 2, 331, 2, 3, 441, 2, 3, 4,

16
COMP 110: Spring 200916 Programming Demo How can we extend MenuCalculator.java to allow the user to re-enter input when the input is invalid? Step 1: Ask the user for input Step 2: Check the input Step 3: If the input is invalid, go to Step 1 Step 4: Perform the selected operation

17
COMP 110: Spring 200917 Programming Demo Evaluate Input Valid? Execute Present Menu Read Input Execute Perform User Selection Start true false Loop Body Stopping Condition Allowing the user to re-enter input

18
COMP 110: Spring 200918 Friday Recitation Bring Laptops (fully charged) Textbook

Similar presentations

OK

M. Mayer SEWG Fuel Retention June 20091 Sample Analysis for TS, AUG and JET: Depth Profiling of Deuterium M. Mayer Max-Planck-Institut für Plasmaphysik,

M. Mayer SEWG Fuel Retention June 20091 Sample Analysis for TS, AUG and JET: Depth Profiling of Deuterium M. Mayer Max-Planck-Institut für Plasmaphysik,

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google