CS100J Lecture 3 Previous Lecture This Lecture Programming Concepts

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Factors Objective – To be able to find all the factors of a given integer.
Special Names for Numbers
CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Copyright © Texas Education Agency, Computer Programming For Loops.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 5 Loops.
Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
CS101 Computer Programming I Chapter 4 Extra Examples.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Chapter 4 Practice cont.. Practice with nested loops 1.What will be the output of the following program segment: 1.for (int i = 1; i
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
Expressions Methods if else Statements Loops Potpourri.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CS 100Lecture 11 Introduction to Programming n What is an algorithm? n Input and output n Sequential execution n Conditional execution Reading: Chapter.
UCT Department of Computer Science Computer Science 1015F Iteration
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 6 More Conditionals and Loops
CS1010 Programming Methodology
Topic 5 for Loops -Arthur Schopenhauer
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Iteration with While You can say that again.
The for-loop and Nested loops
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
More Loops.
Lecture Notes – Week 3 Lecture-2
CS100J Lecture 11 Previous Lecture This Lecture
CS 200 Loops Jim Williams, PhD.
Looping Topic 4.
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Computer Science Core Concepts
Control Structures Part 1
The structure of programming
Building Java Programs
Repetition Statements
Lesson Prime Factorization Page 486
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
Loops and Iteration CS 21a: Introduction to Computing I
To factor a whole number as a product of prime numbers
Programming Fundamental
REPETITION Why Repetition?
Presentation transcript:

CS100J Lecture 3 Previous Lecture This Lecture Programming Concepts variables and declarations, assignment, expressions Java Constructs variable = expression ; expressions in.readInt() if ( expression ) statement1 else statement2 System.out.println( expression ); // any-text-to-end-of-line /* any-text */ { list-of-statements } boilerplate Reading: L&L, Ch. 2, 3.1-3.5; or S. 2, 3.1 This Lecture iteration while-loops Reading: L&L, Sec. 3.6-3.7; or S., Sec. 3.2-3.4 CS 100 Lecture 3

Iteration while ( expression ) Template: The expression is called the condition and the statement is called the body Meaning: Execute the body 0 or more times until the condition is false When several statements must be executed on each iteration, use a block statement while ( expression ) { list-of-statements } while ( expression ) statement CS 100 Lecture 3

Iteration, Example 1 Problem statement A list of integers is given as input data. For each integer, output “even” if it is even, otherwise output “odd”. Program Segment in Java while ( true ) { num = in.readInt(); rem = num % 2; if ( rem == 0 ) System.out.println("even"); else System.out.println("odd"); } CS 100 Lecture 3

Example 1: Execution Trace Sample input data: 7 8 11 ... Variables as a function of time: time Sample output: odd even Remark: If the condition never becomes false, the body is repeated forever CS 100 Lecture 3

Pattern for Iterating n Times /* Do whatever n times. */ j = 1; while ( j <= n ) { whatever j = j + 1; } Remarks: Think of such a pattern as a whole, e.g., as “do whatever n times” whatever can refer to variable j, Any variables can be used instead of j and n CS 100 Lecture 3

Iteration, Example 2 Problem statement An integer n is given as input data. Output a list of the perfect squares between 1 and n2 inclusive. Program Segment in Java n = in.readInt(); /* Output the perfect squares between 1 and n*n inclusive. */ j = 1; while ( j <= n ) { System.out.println( j*j ); j = j + 1; } CS 100 Lecture 3

Example 2: Execution Trace Sample input data: 3 Variables as a function of time: time Sample output: 1 4 9 Remark: If the condition is initially false, the body is executed 0 times, e.g., if the input data is negative or 0 CS 100 Lecture 3

Iteration: Example 3 Problem statement Assume variable m contains some integer. Output a line of m asterisks (*’s). Program Segment in Java /* Output a line of m *’s. */ k = 1; while ( k <= m ) { System.out.print( ”*” ); k = k + 1; } CS 100 Lecture 3

Iteration: Example 4 Problem statement An integer n is given as input data. Output a triangle of asterisks with base and height n , e.g., if n is 3: * ** *** Program Segment in Java n = in.readInt(); /* Output n lines of triangle. */ j = 1; while ( j <= n ) { /* Output line of j *’s. */ ... j = j + 1; } CS 100 Lecture 3

Iteration: Example 4, cont. Program Segment in Java n = in.readInt(); /* Output n lines of triangle */ j = 1; while ( j <= n ) { /* Output line of j *’s. */ k = 1; while ( k <= j ) System.out.print( ”*” ); k = k + 1; } System.out.println(); j = j + 1; CS 100 Lecture 3

Example 4: Execution Trace Sample input data: 2 Variables as a function of time: time Sample output: * ** CS 100 Lecture 3

Iteration: Example 5 Problem statement Program Segment in Java An integer p is given as input data. If the smallest integer >= 2 that divides p is p itself, output “prime”, otherwise output “composite” Program Segment in Java p = in.readInt(); // Let d be smallest integer >= 2 that // divides p exactly. d = 2; while ( (p % d) != 0 ) d = d + 1; if ( d == p ) System.out.print(”prime”); else System.out.print(”composite”); CS 100 Lecture 3