Programming Methodology (1). Iteration Learning objectives explain the term iteration; repeat a section of code with a for loop; repeat a section of.

Slides:



Advertisements
Similar presentations
LOOPS Loops are lines of code that can be run more than one time. Each time is called an iteration and in for loops there is also an index that is changing.
Advertisements

Programming Methodology (1). Implementing Methods main.
Looping Structures: Do Loops
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Flow of Control Instruction/Control structure Looping structure Looping structure Branching structure Branching structure For assembly language program.
Looping while … do …. Condition Process 2 Process 1 Y Repeated Loop.
A Level Computing#BristolMet Session Objectives#U2 S8 MUST identify the difference between a procedure and a function SHOULD explain the need for parameters.
ALGORITHMS & FLOWCHARTING II
Scripts and Flow Control. Scripts So far we have been entering commands directly into the command line But there is a better way Script files (and functions)
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Repetition. Examples When is repetition necessary/useful?
Objectives By the end of today’s class you will be able to… –Describe the major steps in the interaction design process –Explain the importance of iterative.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Presented by Lee Zenke 2015 Java Programming PT. 2.
A revision guide for programming with python. 1.A program is a set of instructions that tells a computer what to do. 2.The role of a programmer is to.
1 Shawlands Academy Higher Computing Software Development Unit.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
1 The Software Development Process  Systems analysis  Systems design  Implementation  Testing  Documentation  Evaluation  Maintenance.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
read and learn from example loop programs develop modular program
1. Understand the application of Pseudo Code for programming purposes 2. Be able to write algorithms in Pseudo Code.
The Software Development Process
For Code Next For Code Next A loop is a segment of a code that repeats (changing slightly each time)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
In this activity, we are going to type a simple Chinese sentence with Microsoft Word by Tsang-jei Input Method and Simplified Tsang-jei Input Method. 1Start.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
GCSE Computing#BristolMet Session Objectives #23 MUST understand what is meant by the programming term iteration SHOULD describe methods of looping used.
CS 104 – Fall 2011 Exploring Computer Science Build Your Own Blocks September 19, 2011.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Selection Using IF THEN ELSE CASE Introducing Loops.
EasyCode Foundations Vocabulary Terms.
ALGORITHMS & FLOWCHARTING II
Do it now activity Green pen activity in books.
Digital Technologies Lesson Yrs 5/6
Introduction to pseudocode
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Looping and Random Numbers
Hour of Code.
Early - I can develop a sequence of instructions and run them using programmable devices or equivalent Designs a simple sequence of instructions/algorithm.
A LESSON IN LOOPING What is a loop?
Flow of Control.
ICT Programming Lesson 5:
The structure of programming
Iteration Learning Objective: to be able to design algorithms that use iteration.
Vocabulary Memory Cards--Sample
Introduction to Repetition
Introduction to Computer Science
Print the following triangle, using nested loops
Introduction to Repetition
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Week 7: Computer Tools for Problem Solving and Critical Thinking
Starter Look at the hand-out.
Learning Objective: to be able to design programs that use iteration.
Presentation transcript:

Programming Methodology (1)

Iteration

Learning objectives explain the term iteration; repeat a section of code with a for loop; repeat a section of code with a while loop; repeat a section of code with a do...while loop; select the most appropriate loop for a particular task; explain the term input validation and write simple validation routines.

When to use a loop?

Display a square of stars (five by five) on the screen as follows: * * * * * * * * * * * * * * * * * * * * * * * * * System.out.println("*****"); System.out.println("*****"); System.out.println("*****"); System.out.println("*****"); System.out.println("*****"); REPEAT 5 times { } System.out.println("*****");

The ‘for’ loop

for ( ; ; ) { } start countertest counterchange counter // instruction(s) to be repeated go here i = 1i++i <= 5 System.out.println(“*****”); int i;

Variable Scope

public static void main (String [] args) { } if ( /* some test */) { } int x; int y; x = 10; System.out.print(x); System.out.print(y); System.out.print(y); y = x+1;

for ( ; ; ) { } i = 1 i++i <= 5 System.out.println(“*****”); RUN ***** ***** ***** ***** ***** i int i; int i = 1

Different ways of using the loop counter

for ( ; ; ) { } i ++i <= 5 System.out.println(“*****”); ***** int i = 1

for ( ; ; ) { } i = i + 2i <= 5 System.out.println(“*****”); ***** int i = 1

for ( ; ; ) { } i = i + 2i <= 10 System.out.println(“*****”); ***** int i = 1

for ( ; ; ) { } i = i + 2i <= 10 System.out.println(“*****”); RUN ***** ***** ***** ***** ***** i int i = 1

for ( ; ; ) { } start countertest counterchange counter // instruction(s) to be repeated go here int i = 10i--i >= 1 System.out.println( i ); RUN i

The body of the loop

for ( ; ; ) { } i ++i <= 3 System.out.println(“First line”); ***** int i = 1

for ( ; ; ) { } i ++i <= 3 System.out.println(“First line”); ***** int i = 1 System.out.println(“Second line”); First line Second line First line Second line First line Second line

for ( ; ; ) { } i --i >= 1 System.out.println( i ); ***** int i =

for ( ; ; ) { } i --i >= 1 if ( i > 5 ) { System.out.println( i ); } int i =

Nested loops

for (int i = 1; i <= 5; i++) { } System.out.println("*****");

for (int i = 1; i <= 5; i++) { } System.out.print("*"); System.out.println( ); System.out.print("*"); System.out.print("*"); System.out.print("*"); System.out.print("*");

for (int i = 1; i <= 5; i++) { } for (int j = 1; j <= 5; j++) { } System.out.print("*"); System.out.println( );

Allowing the user to fix the size of the square * * * * * * * * * * * * * * * * * * * * * * * * *

* * * * * * * * *

for (int i = 1; i <= 5 ; i++) { for (int j = 1; j <= 5 ; j++) { System.out.print("*"); } System.out.println( ); } System.out.println("Size of square?"); num = sc.nextInt(); num; i++) num; j++)

Running the program Size of square? 8 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Size of square? 3 * * * * * * * * *

Non-fixed repetitions

The ‘while’ loop

3422

Error!

7343

Error!

1234

CASH

System.out.print(“Enter PIN: ”); pin = sc.nextInt( ); System.out.print(“Invalid PIN, please re-enter: ”); pin = sc.nextInt( ); System.out.println(“Correct PIN, have some money! ”); while ( ) { } */ test goes here */pin != 1234 RUN Enter PIN:1234 Correct PIN, have some money! Correct PIN, have some money! Invalid PIN, please re-enter: Invalid PIN, please re-enter:1234

Input Validation

import java.util.*; public class DisplayResult { public static void main(String[] args) { int mark; Scanner sc = new Scanner (System.in); System.out.println("What exam mark did you get?"); mark = sc.nextInt(); if (mark >= 40) { System.out.println("Congratulations, you passed"); } else { System.out.println("I‘m sorry, but you failed"); } System.out.println("Good luck with your other exams"); } CHECK MARK HERE

System.out.println("What exam mark did you get?"); mark = sc.nextInt( ); if (mark >= 40) { System.out.println("Congratulations, you passed"); } else { System.out.println("I‘m sorry, but you failed"); } System.out.println("Good luck with your other exams"); CHECK MARK HERE System.out.println(“Invalid mark: please re-enter“); mark = sc.nextInt( ); while ( ) { } mark > 100mark < 0 ||

Sample Program Run What exam mark did you get? 101 Invalid mark: please re-enter -10 Invalid mark: please re-enter 10 I'm sorry, but you failed Good luck with your other exams

The ‘do…while’ loop

// some code here // some code here // some code goes here { } while ( /*test goes here*/ ) do ;

import java.util.*; public class FindCost4 { public static void main(String[] args ) { double price, tax; Scanner sc = new Scanner(System.in); } } // code for rest of program here char reply; System.out.print (“Enter another product (y/n)?: ”); reply = sc.next().charAt(0); { } while ( ); ? reply == ‘y’reply == ‘Y’ ||

Sample Program Run *** Product Price Check *** Enter initial price: 50 Enter tax rate:10 Cost after tax = 55.0 Enter another product (y/n)?: y *** Product Price Check *** Enter initial price: 70 Enter tax rate:5 Cost after tax = 73.5 Enter another product (y/n)?: n

Menu Driven Programs

*** Product Price Check *** [1] Enter Price [2] Enter tax [3] Calculate Cost [4] Quit Enter choice [1-4]: 1 Enter initial price:12.5

*** Product Price Check *** [1] Enter Price [2] Enter tax [3] Calculate Cost [4] Quit Enter choice [1-4]: 1 Enter initial price:100

*** Product Price Check *** [1] Enter Price [2] Enter tax [3] Calculate Cost [4] Quit Enter choice [1-4]: 2 Enter tax rate:12.5

*** Product Price Check *** [1] Enter Price [2] Enter tax [3] Calculate Cost [4] Quit Enter choice [1-4]: 3 Cost = 112.5

*** Product Price Check *** [1] Enter Price [2] Enter tax [3] Calculate Cost [4] Quit Enter choice [1-4]: 4 Goodbye!

Menu Driven Timetable Program

*** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 2 1.OOp.m

*** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 5 Options 1-4 only!

*** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 1 10.OOa.m

*** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: a.m

*** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 4 Goodbye

// code to declare variables System.out.println(“[1] TIME FOR GROUP A”); System.out.println(“[2] TIME FOR GROUP B”); System.out.println(“[3] TIME FOR GROUP C”); System.out.println(“[4] QUIT”); System.out.print(“Enter choice [1-4]: “); choice =sc.next().charAt(0); switch(choice) { } case ‘1’: case ‘2’: case ‘3’: default: System.out.println(“10.00a.m”); System.out.println(“1.00p.m”); System.out.println(“11.00a.m”); System.out.println(“Options 1-4 only!”); { } choice != ‘4’ case ‘4’:System.out.println(“Goodbye”); // CODE TO DISPLAY MENU // CODE TO ENTER CHOICE // CODE TO PROCESS CHOICE do while( ); char choice; break; break; break; break;

public class IterationQ3 { public static void main(String[] args) { for(int i=1; i<=10; i++) { if (i%2 == 0) { System.out.println(i); } } } } i =

Assignment

Aaron’s Shop Guitar (£100) - How many?:

Aaron’s Shop Guitar (£100) - How many?: 2

Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required?

Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n

Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?:

Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?: 1

Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?: 1 Total cost required?

Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?: 1 Total cost required? y

Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?: 1 Total cost required? y Total cost so far = £1100

Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?: 1 Total cost required? y Total cost so far = £1100 Mike (£45) - How many?:

Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?: 1 Total cost required? y Total cost so far = £1100 Mike (£45) - How many?: 0

Using a for loop, write a program that displays a "6 times" multiplication table; the output should look like this: 1  6 = 6 2  6 = 12 3  6 = 18 4  6 = 24 5  6 = 30 6  6 = 36 7  6 = 42 8  6 = 48 9  6 =  6 =  6 =  6 = 72

Using a for loop, write a program that displays a "6 times" multiplication table; the output should look like this: 1  6 = 6 2  6 = 12 3  6 = 18 4  6 = 24 5  6 = 30 6  6 = 36 7  6 = 42 8  6 = 48 9  6 =  6 =  6 =  6 = 72