Java Coding 3. Over & over again!

Size: px
Start display at page:

Download "Java Coding 3. Over & over again!"

Transcription

1 Java Coding 3 Over & over again!

2 Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java statement condition is a boolean expression

3 The while statement Does statement while condition true does statement 0 or more times while (condition) statement; true condition loop statement false

4 Syntax 5.1 while Statement Copyright 2014 by John Wiley & Sons. All rights reserved. 4

5 Examples (1) Print 5 asterisk characters Use 5 println statements! Use a single println statement! Use a while loop * * * * * done starsprinted is 0 while starsprinted < 5 do print a star add 1 to starsprinted print done Remember: curly brackets!

6 Examples (1) Print 5 stars (asterisk characters) Use 5 println statements! Use a single println statement! Use repetition (a loop ) * * * * * done starslefttoprint is 5 while there are starslefttoprint do print a star subtract 1 from starslefttoprint print done

7 Examples (1) Print 5 asterisk characters Use 5 println statements! Use a single println statement! Use a while loop count = 0; while ( count < 5 ) { System.out.println( * ); count = count + 1; } System.out.println( done ); * * * * * done If you print out count as well as the star, what do you get?

8 Stars import java.util.scanner; public class Play1 { public static void main( String[] args) { Scanner scan = new Scanner( System.in); System.out.println( "Start of Play1\n"); // CONSTANTS final int STARS_REQUIRED = 5; // VARIABLES int int starsprinted; starslefttoprint; double d;

9 Stars // ********************************** // First solution // ********************************** starsprinted = 0; while ( starsprinted < STARS_REQUIRED ) { System.out.println( "* " + starsprinted); starsprinted = starsprinted + 1; } // assert: starsprinted >= 5 System.out.println( "\ndone " + starsprinted); } // ********************************** System.out.println( "\nend of Play1\n" ); } // end of class Play1

10 Stars // ********************************** // Alternative solution // ********************************** starslefttoprint = STARS_REQUIRED; while ( starslefttoprint > 0) { System.out.println( "* " + starslefttoprint); starslefttoprint = starslefttoprint - 1; } } // assert: starslefttoprint <= 0 System.out.println( "\ndone " + starslefttoprint); } // end of class Play1

11 Stars d = 0.0; while ( d!= 1.0) // change to (d <= 1.0) to see what happens! { System.out.println( d); d = d + 0.1; } System.out.println( "done " + d); System.out.println(); } } // end of class Play1

12 Examples (2) Read & sum 5 values sum is 0 count is 0 while count < 5 do read read value value add add value value to sum to sum add 1 to count sum is 20 report sum

13 Examples (2) Read & sum 5 values Named constant or ask for & get value from user sum = 0; count = 0; while ( count < 5 ) { value = scan.nextint(); sum = sum + value; count = count + 1; } System.out.println( sum is + sum); sum is 20 Modify to ask user number of values to sum up.

14 Generic form of while initialise any variables in condition while (test condition variable) do statement & update condition variables; What happens if you fail to Initialize loop variables Unpredictable behavior Update loop variables Infinite loop! (in console app s use Ctrl-C to exit) In general condition must be falsifiable by applying update to initial values need proof!

15 Common Error: Infinite Loops Do these print done? count = 0; while ( count < 5 ) { System.out.println( * ); count = count - 1; } System.out.println( done ); i = 1; while ( i!= 50 ) { System.out.println( i); i = i + 2; } System.out.println( done );

16 Common Error: Infinite Loops What about this one? i = scan.nextint(); while ( i!= 1 ) { if ( i % 2 == 0) i = i / 2; else i = 3 * i + 1; } System.out.println( done ); Proof that cannot write program to determine whether every algorithm will halt or not. (ref. Halting Problem Alan Turing.)

17 Common Error: Off-by-One Errors Off-by-one error: a loop executes one too few, or one too many, times. Example: int years = 0; while (balance < targetbalance) { years++; balance = balance * (1 + RATE / 100); } System.out.println("The investment doubled after " + year + " years."); Should years start at 0 or 1? Should the test be < or <=? Copyright 2014 by John Wiley & Sons. All rights reserved. 17

18 Avoiding Off-by-One Error Look at a scenario with simple values: initial balance: $100 interest rate: 50% after year 1, the balance is $150 after year 2 it is $225, or over $200 so the investment doubled after 2 years the loop executed two times, incrementing years each time Therefore: years must start at 0, not at 1. interest rate: 100% after one year: balance is 2 * initialbalance loop should stop Therefore: must use < not <= Think, don't compile and try at random Copyright 2014 by John Wiley & Sons. All rights reserved. 18

19 Reading a set of data First approach How many? sum is 20 It is ok, but not really userfriendly (easy to mis-count!)

20 Reading a set of data Second approach More? Y 3 More? Y 5 More? Y 7 More? Y 4 More? Y 1 More? N sum is 20 Ok, but in simple cases adds lots of typing overhead. Fine for entering lots of data (eg. name & address) Often applied to re-run/repeat some existing code/program (to play a game again)

21 Reading a set of data Third approach sum is 20 Must say no more Sentinel value non-data value marks end of list More user-friendly for low volume data entry Want to simply enter data, then somehow say no more or stop If reading int values such a string would crash the program Could read strings instead, but then need to convert into number for maths Sentinel is value that guards/marks the end of the set same type as actual data non-data value (not always possible to find)

22 Sentinel-controlled input Sum set of values terminated by -1 sum = 0 read value while value is not the sentinel do add value to sum read next value print sum Extract another design pattern read value while value is not the sentinel process the value read next value

23 Sentinel-controlled input Look at wrong options sum = 0 while value is not the sentinel do read next value add value to sum print sum adds sentinel to sum Ensure solution: (a) does not process the sentinel (b) processes all other values including the first (c) processes the empty set correctly

24 Examples (sentinel input) Next week

25 More While Examples

26 The while Loop Investment problem You put $10,000 into a bank account that earns 5 percent interest per year. How many years does it take for the account balance to be double the original investment? The algorithm Start with a year value of 0, a column for the interest, and a balance of $10,000. Repeat the following steps while the balance is less than $20,000. Add 1 to the year value. Compute the interest as balance x 0.05 (i.e., 5 percent interest). Add the interest to the balance. Report the final year value as the answer. Copyright 2014 by John Wiley & Sons. All rights reserved. 26

27 The while Loop The code: while (balance < targetbalance) { year++; double interest = balance * RATE / 100; balance = balance + interest; } Copyright 2014 by John Wiley & Sons. All rights reserved. 27

28 The while Loop Figure 1 Flowchart of a while Loop Copyright 2014 by John Wiley & Sons. All rights reserved. 28

29 while Loop Examples Copyright 2014 by John Wiley & Sons. All rights reserved. 29

30 Problem Solving: Hand-Tracing A simulation of code execution in which you step through instructions and track the values of the variables. What value is displayed? int n = 1729; int sum = 0; while (n > 0) { int digit = n % 10; sum = sum + digit; n = n / 10; } System.out.println(sum); Copyright 2014 by John Wiley & Sons. All rights reserved. 30

31 Problem Solving: Hand-Tracing - Step by Step Step 1 Step 2 Copyright 2014 by John Wiley & Sons. All rights reserved. 31

32 Problem Solving: Hand-Tracing - Step by Step Step 3 Copyright 2014 by John Wiley & Sons. All rights reserved. 32

33 Problem Solving: Hand-Tracing - Step by Step Step 4 Copyright 2014 by John Wiley & Sons. All rights reserved. 33

34 Problem Solving: Hand-Tracing - Step by Step Step 5 Copyright 2014 by John Wiley & Sons. All rights reserved. 34

35 Problem Solving: Hand-Tracing - Step by Step Step 6 Copyright 2014 by John Wiley & Sons. All rights reserved. 35

36 Problem Solving: Hand-Tracing - Step by Step Step 7 Copyright 2014 by John Wiley & Sons. All rights reserved. 36

37 Problem Solving: Hand-Tracing - Step by Step Step 8 Copyright 2014 by John Wiley & Sons. All rights reserved. 37

38 Problem Solving: Hand-Tracing - Step by Step Step 9 Step 10 The sum, which is 19, is printed Copyright 2014 by John Wiley & Sons. All rights reserved. 38

39 Self Check 5.6 Hand-trace the following code, showing the value of n and the output. int n = 5; while (n >= 0) { n--; System.out.print(n); } Answer: n output Copyright 2014 by John Wiley & Sons. All rights reserved. 39

40 Self Check 5.8 Hand-trace the following code, assuming that a is 2 and n is 4. Then explain what the code does for arbitrary values of a and n. int r = 1; int i = 1; while (i <= n) { r = r * a; i++; } Answer: a n r i The code computes a n. Copyright 2014 by John Wiley & Sons. All rights reserved. 40

41 Self Check 5.9 Trace the following code. What error do you observe? int n = 1; while (n!= 50) { System.out.println(n); n = n + 10; } Answer: n output This is an infinite loop. n is never equal to 50. Copyright 2014 by John Wiley & Sons. All rights reserved. 41

42 Self Check 5.10 The following pseudo-code is intended to count the number of digits in the number n: count = 1 temp = n while (temp > 10) Increment count Divide temp by 10.0 Trace the pseudocode for n = 123 and n = 100. What error do you find? Continued Copyright 2014 by John Wiley & Sons. All rights reserved. 42

43 Self Check 5.10 Answer: count temp This yields the correct answer. The number 123 has 3 digits. count temp This yields the wrong answer. The number 100 also has 3 digits. The loop condition should have been while (temp >= 10). Copyright 2014 by John Wiley & Sons. All rights reserved. 43

44 Application: Processing Sentinel Values To compute the average of a set of salaries use -1 to indicate termination Inside the loop Read the input process it if the input is not -1 Stay in the loop while the sentinel value is not -1. Initialize the input to something other than -1. Copyright 2014 by John Wiley & Sons. All rights reserved. 44

45 section_5/sentineldemo.java 1 import java.util.scanner; 2 3 /** 4 This program prints the average of salary values that are terminated with a sentinel. 5 */ 6 public class SentinelDemo 7 { 8 public static void main(string[] args) 9 { 10 double sum = 0; 11 int count = 0; 12 double salary = 0; 13 System.out.print("Enter salaries, -1 to finish: "); 14 Scanner in = new Scanner(System.in); // Process data until the sentinel is entered 17 Continu ed Copyright 2014 by John Wiley & Sons. All rights reserved. 45

46 section_5/sentineldemo.java 16 // Process data until the sentinel is entered while (salary!= -1) 19 { 20 salary = in.nextdouble(); 21 if (salary!= -1) 22 { 23 sum = sum + salary; 24 count++; 25 } 26 } // Compute and print the average if (count > 0) 31 { 32 double average = sum / count; 33 System.out.println("Average salary: " + average); 34 } 35 else 36 { 37 System.out.println("No data"); 38 } 39 } 40 } Continu ed Copyright 2014 by John Wiley & Sons. All rights reserved. 46

47 section_5/sentineldemo.java Program Run Enter salaries, -1 to finish: Average salary: 20 Copyright 2014 by John Wiley & Sons. All rights reserved. 47

48 Application: Processing Sentinel Values Using a Boolean variable to control a loop. Set the variable before entering the loop Set it to the opposite to leave the loop. System.out.print("Enter salaries, -1 to finish: "); boolean done = false; while (!done) { value = in.nextdouble(); if (value == -1) { done = true; } else { Process value } } Copyright 2014 by John Wiley & Sons. All rights reserved. 48

49 Application: Processing Sentinel Values When any number can be an acceptable input Use a sentinel value that is not a number (such as the letter Q) in.hasnextdouble() returns false if the input is not a floating-point number Use this loop System.out.print("Enter values, Q to quit: "); while (in.hasnextdouble()) { value = in.nextdouble(); Process value. } Copyright 2014 by John Wiley & Sons. All rights reserved. 49

50 Self Check 5.21 What does the SentinelDemo.java program print when the user immediately types -1 when prompted for a value? Answer: No data Copyright 2014 by John Wiley & Sons. All rights reserved. 50

51 Self Check 5.22 Why does the SentinelDemo.java program have two checks of the form salary!= -1? Answer: The first check ends the loop after the sentinel has been read. The second check ensures that the sentinel is not processed as an input value. Copyright 2014 by John Wiley & Sons. All rights reserved. 51

52 Self Check 5.23 What would happen if the declaration of the salary variable in SentinelDemo.java was changed to double salary = -1; Answer: The while loop would never be entered. The user would never be prompted for input. Because count stays 0, the program would then print "No data". Copyright 2014 by John Wiley & Sons. All rights reserved. 52

53 Self Check 5.24 In the last example of this section, we prompt the user "Enter values, Q to quit: " What happens when the user enters a different letter? Answer: The nextdouble method also returns false. A more accurate prompt would have been: "Enter values, a key other than a digit to quit: " But that might be more confusing to the program user who would need to ponder which key to choose. Copyright 2014 by John Wiley & Sons. All rights reserved. 53

54 For & Do-while other forms of repetition in Java

55 Java for statements Same as while loop! Use as short-hand counting style loop for ( init; condition; update) statement; false init condition true statement Example: for ( i = 0; i < 5; i = i + 1) System.out.println( * ); update

56 Java for statements for ( init; condition; update) statement; Has advantage of explicitly reminding you about init & update Easy to forget when writing a while loop Variable i is often defined inside the for statement for ( int i = 0; i < 5; i++) System;out.println( * ); System;out.println( i = + i); // will not compile! i will not be visible outside (after the loop) Variables only visible inside block they are defined in. important! i++ is shorthand notation for i = i + 1;

57 The for Loop To execute a sequence of statements a given number of times: Could use a while loop controlled by a counter int counter = 1; // Initialize the counter while (counter <= 10) // Check the counter { System.out.println(counter); counter++; // Update the counter } Use a special type of loop called for loop for (int counter = 1; counter <= 10; counter++) { System.out.println(counter); } Use a for loop when a variable runs from a starting value to an ending value with a constant increment or decrement. Copyright 2014 by John Wiley & Sons. All rights reserved. 57

58 Syntax 5.2 for Statement Copyright 2014 by John Wiley & Sons. All rights reserved. 58

59 The for Loop The initialization is executed once, before the loop is entered. The condition is checked before each iteration. The update is executed after each iteration. Copyright 2014 by John Wiley & Sons. All rights reserved. 59

60 The for Loop A for loop can count down instead of up: for (int counter = 10; counter >= 0; counter--)... The increment or decrement need not be in steps of 1: for (int counter = 0; counter <= 10; counter = counter + 2)... Copyright 2014 by John Wiley & Sons. All rights reserved. 60

61 The for Loop If the counter variable is defined in the loop header, It does not exist after the loop for (int counter = 1; counter <= 10; counter++) {... } // counter no longer declared here If you declare the counter variable before the loop, You can continue to use it after the loop int counter; for (counter = 1; counter <= 10; counter++) {... } // counter still declared here Copyright 2014 by John Wiley & Sons. All rights reserved. 61

62 Java do-while statements Repeat 1 or more times Example: do statement; while (condition); statement i = 0; do { System.out.println( * ); i++; } while ( i < 5); condition false true { on same line as while (); can help avoid confusion! Also, ; is required here (in do-while statement), but not in while statement

63 Examples (do-while) Data validation e.g. Read positive value from user do ask for a positive value and get value while value is not positive do { System.out.print( Enter positive value: ); value = scan.nextint(); } while ( value <= 0); // assert: value > 0

64 Examples (do-while) Menus - set of options for user to select from do display menu get selection from user perform selection while selection is not exit print goodbye if selection is SALES then // do sales things else if selection is STOCK then // do stock things else if selection is ADMIN then // do admin things else if selection is not EXIT then print invalid selection msg ABC Trading Co sales 2 stock 3 admin Select (0 to exit): _

65 Self Check 5.16 Suppose that we want to check for inputs that are at least 0 and at most 100. Modify the do loop for this check. int value; do { System.out.print("Enter an integer < 100: "); value = in.nextint(); } while (value >= 100); Answer: do } { System.out.print( "Enter a value between 0 and 100: "); value = in.nextint(); while (value < 0 value > 100); Copyright 2014 by John Wiley & Sons. All rights reserved. 65

66 Self Check 5.17 Rewrite the input check do loop using a while loop. What is the disadvantage of your solution? int value; do { System.out.print("Enter an integer < 100: "); value =in.nextint(); } while (value >= 100); Answer: int value = 100; while (value >= 100) { System.out.print("Enter a value < 100: "); value = in.nextint(); } Here, the variable value had to be initialized with an artificial value to ensure that the loop is entered at least once. Copyright 2014 by John Wiley & Sons. All rights reserved. 66

67 Self Check 5.18 Suppose Java didn't have a do loop. Could you rewrite any do loop as a while loop? Answer: Yes. The do loop do { Body } while (condition); is equivalent to this while loop: boolean first = true; while (first condition) { body; first = false; } Copyright 2014 by John Wiley & Sons. All rights reserved. 67

Chapter Goals. Contents LOOPS

Chapter Goals. Contents LOOPS CHAPTER 4 LOOPS Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30, 2011 Chapter Goals To implement while, for, and do loops To hand-trace the execution of a program To become familiar with common

More information

Loops. CSE 114, Computer Science 1 Stony Brook University

Loops. CSE 114, Computer Science 1 Stony Brook University Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?

More information

Introduction to Software Development (ISD) Week 3

Introduction to Software Development (ISD) Week 3 Introduction to Software Development (ISD) Week 3 Autumn term 2012 Aims of Week 3 To learn about while, for, and do loops To understand and use nested loops To implement programs that read and process

More information

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

Chapter Goals. Chapter 5 - Iteration. Calculating the Growth of an Investment

Chapter Goals. Chapter 5 - Iteration. Calculating the Growth of an Investment Chapter Goals To be able to program loops with the while and for statements To avoid infinite loops and off-by-one errors To be able to use common loop algorithms To understand nested loops To implement

More information

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

COMP 202 Java in one week

COMP 202 Java in one week CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language

More information

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements COMP-202 Unit 4: Programming With Iterations CONTENTS: The while and for statements Introduction (1) Suppose we want to write a program to be used in cash registers in stores to compute the amount of money

More information

COMP 202. Java in one week

COMP 202. Java in one week COMP 202 CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator Java in one week The Java Programming Language A programming language

More information

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website: Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when

More information

CSE 114 Computer Science I

CSE 114 Computer Science I CSE 114 Computer Science I Iteration Cape Breton, Nova Scotia What is Iteration? Repeating a set of instructions a specified number of times or until a specific result is achieved How do we repeat steps?

More information

Java I/O and Control Structures

Java I/O and Control Structures Java I/O and Control Structures CSC 2014 Java Bootcamp Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Some slides in this presentation are adapted from the slides accompanying

More information

Chapter 3. Selections

Chapter 3. Selections Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

More information

Java I/O and Control Structures Algorithms in everyday life

Java I/O and Control Structures Algorithms in everyday life Introduction Java I/O and Control Structures Algorithms in everyday life CSC 2014 Java Bootcamp Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Source: http://xkcd.com/627/

More information

Iteration Advanced Programming

Iteration Advanced Programming Iteration Advanced Programming ICOM 4015 Lecture 6 Reading: Java Concepts Chapter 7 Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one

More information

1/9/2015. Intro to CS: Java Review Assignment Due Dates Chapter 7 7.1: while Loops 7.2: for Loops Ch7 Work Time. Chapter 7 Iteration WHILE LOOPS

1/9/2015. Intro to CS: Java Review Assignment Due Dates Chapter 7 7.1: while Loops 7.2: for Loops Ch7 Work Time. Chapter 7 Iteration WHILE LOOPS Chapter 7 Iteration The Plan For Today Intro to CS: Java Review Assignment Due Dates Chapter 7 7.1: while Loops 7.2: for Loops Ch7 Work Time WHILE LOOPS Executes a block of code repeatedly A condition

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

Chapter 4 Introduction to Control Statements

Chapter 4 Introduction to Control Statements Introduction to Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives 2 How do you use the increment and decrement operators? What are the standard math methods?

More information

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade; Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

More information

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

Introduction to Software Development (ISD) David Weston and Igor Razgon

Introduction to Software Development (ISD) David Weston and Igor Razgon Introduction to Software Development (ISD) David Weston and Igor Razgon Autumn term 2013 Course book The primary book supporting the ISD module is: Java for Everyone, by Cay Horstmann, 2nd Edition, Wiley,

More information

Repetition CSC 121 Fall 2014 Howard Rosenthal

Repetition CSC 121 Fall 2014 Howard Rosenthal Repetition CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables

More information

Problem Solving With Loops

Problem Solving With Loops To appreciate the value of loops, take a look at the following example. This program will calculate the average of 10 numbers input by the user. Without a loop, the three lines of code that prompt the

More information

Midterm Examination (MTA)

Midterm Examination (MTA) M105: Introduction to Programming with Java Midterm Examination (MTA) Spring 2013 / 2014 Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. 1. Compilers and

More information

Loops and Expression Types

Loops and Expression Types Software and Programming I Loops and Expression Types Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline The while, for and do Loops Sections 4.1, 4.3 and 4.4 Variable Scope Section

More information

Repetition. Chapter 6

Repetition. Chapter 6 Chapter 6 Repetition Goals This chapter introduces the third major control structure repetition (sequential and selection being the first two). Repetition is discussed within the context of two general

More information

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables

More information

Repetition. Chapter 6

Repetition. Chapter 6 Chapter 6 Repetition Goals This chapter introduces the third major control structure repetition (sequential and selection being the first two). Repetition is discussed within the context of two general

More information

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2 CONTENTS: Compilation Data and Expressions COMP 202 More on Chapter 2 Programming Language Levels There are many programming language levels: machine language assembly language high-level language Java,

More information

Last Class. While loops Infinite loops Loop counters Iterations

Last Class. While loops Infinite loops Loop counters Iterations Last Class While loops Infinite loops Loop counters Iterations public class January31{ public static void main(string[] args) { while (true) { forloops(); if (checkclassunderstands() ) { break; } teacharrays();

More information

Control Statements: Part 1

Control Statements: Part 1 4 Let s all move one place on. Lewis Carroll Control Statements: Part 1 The wheel is come full circle. William Shakespeare How many apples fell on Newton s head before he took the hint! Robert Frost All

More information

AP CS Unit 3: Control Structures Notes

AP CS Unit 3: Control Structures Notes AP CS Unit 3: Control Structures Notes The if and if-else Statements. These statements are called control statements because they control whether a particular block of code is executed or not. Some texts

More information

Controls Structure for Repetition

Controls Structure for Repetition Controls Structure for Repetition So far we have looked at the if statement, a control structure that allows us to execute different pieces of code based on certain conditions. However, the true power

More information

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures Introduction to Computer Science, Shimon Schocken, IDC Herzliya Lectures 3.1 3.2 Control Structures Control Structures, Shimon Schocken IDC Herzliya, www.intro2cs.com slide 1 Control structures A program

More information

Repetition, Looping. While Loop

Repetition, Looping. While Loop Repetition, Looping Last time we looked at how to use if-then statements to control the flow of a program. In this section we will look at different ways to repeat blocks of statements. Such repetitions

More information

Top-Down Program Development

Top-Down Program Development Top-Down Program Development Top-down development is a way of thinking when you try to solve a programming problem It involves starting with the entire problem, and breaking it down into more manageable

More information

IST 297D Introduction to Application Programming Chapter 4 Problem Set. Name:

IST 297D Introduction to Application Programming Chapter 4 Problem Set. Name: IST 297D Introduction to Application Programming Chapter 4 Problem Set Name: 1. Write a Java program to compute the value of an investment over a number of years. Prompt the user to enter the amount of

More information

Introduction to the Java Basics: Control Flow Statements

Introduction to the Java Basics: Control Flow Statements Lesson 3: Introduction to the Java Basics: Control Flow Statements Repetition Structures THEORY Variable Assignment You can only assign a value to a variable that is consistent with the variable s declared

More information

Assignment 2.4: Loops

Assignment 2.4: Loops Writing Programs that Use the Terminal 0. Writing to the Terminal Assignment 2.4: Loops In this project, we will be sending our answers to the terminal for the user to see. To write numbers and text to

More information

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection

More information

STUDENT LESSON A12 Iterations

STUDENT LESSON A12 Iterations STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of

More information

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya, slide 1

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya,  slide 1 Introduction to Computer Science Shimon Schocken IDC Herzliya Lectures 3-1, 3-2 Control Structures Control Structures, Shimon Schocken IDC Herzliya, www.intro2cs.com slide 1 Shorthand operators (increment

More information

Introduction to Computer Science Unit 2. Exercises

Introduction to Computer Science Unit 2. Exercises Introduction to Computer Science Unit 2. Exercises Note: Curly brackets { are optional if there is only one statement associated with the if (or ) statement. 1. If the user enters 82, what is 2. If the

More information

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

double float char In a method: final typename variablename = expression ;

double float char In a method: final typename variablename = expression ; Chapter 4 Fundamental Data Types The Plan For Today Return Chapter 3 Assignment/Exam Corrections Chapter 4 4.4: Arithmetic Operations and Mathematical Functions 4.5: Calling Static Methods 4.6: Strings

More information

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18 Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical

More information

Oct Decision Structures cont d

Oct Decision Structures cont d Oct. 29 - Decision Structures cont d Programming Style and the if Statement Even though an if statement usually spans more than one line, it is really one statement. For instance, the following if statements

More information

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops Introductory Programming Imperative Programming III, sections 3.6-3.8, 3.0, 3.9 Anne Haxthausen a IMM, DTU 1. Loops (while, do, for) (sections 3.6 3.8) 2. Overview of Java s (learnt so far) 3. Program

More information

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4) Loops! Flow of Control: Loops (Savitch, Chapter 4) TOPICS while Loops do while Loops for Loops break Statement continue Statement CS 160, Fall Semester 2014 2 An Example while Loop Step- by- step int count

More information

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled Interpreted vs Compiled Python 1 Java Interpreted Easy to run and test Quicker prototyping Program runs slower Compiled Execution time faster Virtual Machine compiled code portable Java Compile > javac

More information

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1 COMP 202 Programming With Iterations CONTENT: The WHILE, DO and FOR Statements COMP 202 - Loops 1 Repetition Statements Repetition statements or iteration allow us to execute a statement multiple times

More information

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process Entry Point of Execution: the main Method Elementary Programming EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG For now, all your programming exercises will

More information

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A CSC 1051 Algorithms and Data Structures I Midterm Examination February 25, 2016 Name: KEY A Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in

More information

Control Structures II. Repetition (Loops)

Control Structures II. Repetition (Loops) Control Structures II Repetition (Loops) Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1 to 100 The answer will be 1 + 2 + 3 + 4 + 5 + 6 + +

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time Tester vs. Controller Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG For effective illustrations, code examples will mostly be written in the form of a tester

More information

Elementary Programming

Elementary Programming Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Learn ingredients of elementary programming: data types [numbers, characters, strings] literal

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java

More information

COMP-202 Unit 4: Programming with Iterations

COMP-202 Unit 4: Programming with Iterations COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables

More information

CEN 414 Java Programming

CEN 414 Java Programming CEN 414 Java Programming Instructor: H. Esin ÜNAL SPRING 2017 Slides are modified from original slides of Y. Daniel Liang WEEK 2 ELEMENTARY PROGRAMMING 2 Computing the Area of a Circle public class ComputeArea

More information

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14 int: integers, no fractional part 1, -4, 0 double: floating-point numbers (double precision) 0.5, -3.11111, 4.3E24, 1E-14 A numeric computation overflows if the result falls outside the range for the number

More information

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

! definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. ! We often use language like Indefinite loops while loop! indefinite loop: A loop where it is not obvious in advance how many times it will execute.! We often use language like " "Keep looping as long as or while this condition is

More information

Conditional Execution

Conditional Execution Unit 3, Part 3 Conditional Execution Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Simple Conditional Execution in Java if () { else {

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-4: do/while loops, assertions reading: 5.1, 5.5 1 The do/while loop do/while loop: Performs its test at the end of each repetition. Guarantees that the loop's

More information

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved Chapter Four: Loops Slides by Evan Gallagher The Three Loops in C++ C++ has these three looping statements: while for do The while Loop while (condition) { statements } The condition is some kind of test

More information

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class CS0007: Introduction to Computer Programming Review General Form of a switch statement: switch (SwitchExpression) { case CaseExpression1:

More information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly

More information

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct. Example Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct. 1... 2 Scanner input = new Scanner(System.in); 3 int x = (int) (Math.random()

More information

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Learn about repetition (looping) control structures Explore how to construct and use: o Counter-controlled

More information

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals Chapter Four: Fundamental Data Types Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To become aware of causes for overflow and roundoff

More information

Full file at

Full file at Chapter 3 Flow of Control Multiple Choice 1) An if selection statement executes if and only if: (a) the Boolean condition evaluates to false. (b) the Boolean condition evaluates to true. (c) the Boolean

More information

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #2

More information

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times Iteration: Intro Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times 2. Posttest Condition follows body Iterates 1+ times 1 Iteration: While Loops Pretest loop Most general loop construct

More information

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014 M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014 Question One: Choose the correct answer and write it on the external answer booklet. 1. Java is. a. case

More information

Chapter 4 Lab. Loops and Files. Objectives. Introduction

Chapter 4 Lab. Loops and Files. Objectives. Introduction Chapter 4 Lab Loops and Files Objectives Be able to convert an algorithm using control structures into Java Be able to write a while loop Be able to write a do-while loop Be able to write a for loop Be

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-4: do/while loops, assertions reading: 5.1, 5.5 1 The do/while loop do/while loop: Performs its test at the end of each repetition. Guarantees that the loop's

More information

Date: Dr. Essam Halim

Date: Dr. Essam Halim Assignment (1) Date: 11-2-2013 Dr. Essam Halim Part 1: Chapter 2 Elementary Programming 1 Suppose a Scanner object is created as follows: Scanner input = new Scanner(System.in); What method do you use

More information

Scope of this lecture. Repetition For loops While loops

Scope of this lecture. Repetition For loops While loops REPETITION CITS1001 2 Scope of this lecture Repetition For loops While loops Repetition Computers are good at repetition We have already seen the for each loop The for loop is a more general loop form

More information

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes Entry Point of Execution: the main Method Elementary Programming EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG For now, all your programming exercises will be defined within the

More information

AP Computer Science Unit 1. Programs

AP Computer Science Unit 1. Programs AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated

More information

CMPT 125: Lecture 4 Conditionals and Loops

CMPT 125: Lecture 4 Conditionals and Loops CMPT 125: Lecture 4 Conditionals and Loops Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 17, 2009 1 Flow of Control The order in which statements are executed

More information

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation. Chapter 4 Lab Loops and Files Lab Objectives Be able to convert an algorithm using control structures into Java Be able to write a while loop Be able to write an do-while loop Be able to write a for loop

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1 self-check: Ch. 4 #2; Ch. 5 # 1-10 exercises: Ch. 4 #2, 4, 5, 8; Ch. 5 # 1-2 Copyright 2009

More information

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Learn about repetition (looping) control structures Explore how to construct and use: o Counter-controlled

More information

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Computer Programming, I. Laboratory Manual. Experiment #6. Loops Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #6

More information

Introduction to Java & Fundamental Data Types

Introduction to Java & Fundamental Data Types Introduction to Java & Fundamental Data Types LECTURER: ATHENA TOUMBOURI How to Create a New Java Project in Eclipse Eclipse is one of the most popular development environments for Java, as it contains

More information

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..

More information

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2 Java Foundations Introduction to Program Design and Data Structures 4th Edition Lewis TEST BANK Full download at : https://testbankreal.com/download/java-foundations-introduction-toprogram-design-and-data-structures-4th-edition-lewis-test-bank/

More information

4CCS1PRP, Programming Practice 2012 Lecture 6: Arrays - Part 1

4CCS1PRP, Programming Practice 2012 Lecture 6: Arrays - Part 1 4CCS1PRP, Programming Practice 2012 Lecture 6: Arrays - Part 1 Martin Chapman Java for Everyone by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved. Slides by Donald W. Smith TechNeTrain.com

More information

McGill University School of Computer Science COMP-202A Introduction to Computing 1

McGill University School of Computer Science COMP-202A Introduction to Computing 1 McGill University School of Computer Science COMP-202A Introduction to Computing 1 Midterm Exam Thursday, October 26, 2006, 18:00-20:00 (6:00 8:00 PM) Instructors: Mathieu Petitpas, Shah Asaduzzaman, Sherif

More information

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures Chapter 5: Control Structures II Java Programming: Program Design Including Data Structures Chapter Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,

More information

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn CPS109 Course Notes 6 Alexander Ferworn Unrelated Facts Worth Remembering Use metaphors to understand issues and explain them to others. Look up what metaphor means. Table of Contents Contents 1 ITERATION...

More information

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name: CSC 1051-001 Algorithms and Data Structures I Midterm Examination February 25, 2016 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the

More information

Chapter 2. Elementary Programming

Chapter 2. Elementary Programming Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

More information

Algorithms and Conditionals

Algorithms and Conditionals Algorithms and Conditionals CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information