2 nd Semester 2004 1 Looping Technique FOR…DO 204111 Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Lesson 6A – Loops By John B. Owen All rights reserved ©2011.
Looping Structures: Do Loops
2 nd Semester Selection Statement Computer and Programming (204111)
James Tam Repetition In Pascal In this section of notes you will learn how to have a section of code repeated without duplicating the code.
James Tam Repetition In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Do Loop The syntax of DO loop: DO variable = initial_value, final_value[, increment] [statements] END DO Example: PROGRAM LINES ! Illustration of DO-loops.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Chapter 6 - Visual Basic Schneider
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Iterative Statements Concepts Covered: Iterative Statements (counter-based) Iterative.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 10.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 11.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
COMP Loop Statements Yi Hong May 21, 2015.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
Pendalaman Materi Conditional dan Repetition (Pengulangan)
2 nd Semester Module1 Introduction to Computer and Programming อภิรักษ์ จันทร์สร้าง Aphirak Jansang
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
2 nd Semester Module3 Selection Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Looping(1) For … to … do... Can you do this? Write a program to display:
Loops Brent M. Dingle Texas A&M University Chapter 7 – part C (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
UCT Department of Computer Science Computer Science 1015F Iteration
Repetition (While-Loop) version]
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
` Structured Programming & Flowchart
Chapter 8: More on the Repetition Structure
CS2011 Introduction to Programming I Loop Statements (II)
CS150 Introduction to Computer Science 1
Module5 Looping Techniques: Part 2
Module5 Looping Techniques: Part 2
Week 6 CPS125.
Module8 Multi-dimensional Array
Let’s all Repeat Together
Computer Science 1 For..do loop Dry Run Take notes on the For loop
Lec 6 Loop Statements Introduction to Computer Programming
Programming Techniques
Presentation transcript:

2 nd Semester Looping Technique FOR…DO Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND

2 nd Semester Last week … Review…  Flowchart Represent Looping  While…DO Syntax  While…DO Template Count Control Event Control

2 nd Semester Summary Before Today!!!  We learned how to program in this way Sequential Programming Selection Programming (IF/Case) Repetition Programming (Looping, Iteration) Repetition Programming (Looping, Iteration)

2 nd Semester Outline  WHILE…DO VS FOR...DO  For…TO…DO Syntax Example

2 nd Semester Flowchart 1: While…DO in Flowchart View Condition Statement1; Statement2; Statement3; Out False True In WHILE…DO

2 nd Semester While…DO Categories While Loop Count ControlledEvent Controlled Special Case Special Case: For…DO Increase/Decrease by 1

2 nd Semester Count Controlled Loop Example n! flowchart Start End read(n) fac:=n; Tot:=1; fac>0 Tot:=Tot*fac; fac:=fac-1; Write(Tot); TRUE FALSE

2 nd Semester n! program: WHILE…DO Program N_FACTORIAL(input, output); VAR total, fac, n : integer; begin write(’Please input n ’); readln(n); Fac:=n; Total:=1; {WHILE N! Calculate} {WHILE N! Calculate} while (fac > 0) do begin total := fac * total; fac:=fac-1;end; writeln(n, ’! = ’, total) end.

2 nd Semester n! program: FOR…DO Program N_FACTORIAL(input, output); VAR total, fac, n : integer; begin write(’Please input n ’); readln(n); TOTAL:=1; {FOR N! Calculate} {FOR N! Calculate} FOR FAC:=N downto 1 do begin total := fac * total; end; writeln(n, ’! = ’, total) end.

2 nd Semester Outline  WHILE…DO VS FOR...DO  For…TO…DO Syntax Example

2 nd Semester For Loop count-controlled  Special statement for writing count-controlled loops. forcounter := initialtofinaldo for counter := initial to final dobegin …end; counter := initial; while (counter <= final) do begin… counter := counter + 1; end;

2 nd Semester For Loop Syntax for todo for counter :=initial to final do statement; fordowntodo for counter:=initial downto final do statement;  Increase by 1  Decrease by 1 The value of counter must not be modified in statement. #note The value of counter must not be modified in statement.

2 nd Semester For Example  Write into the screen fori := 1todo for i := 1 to 5 do write( i, ‘ ‘ );  Write into the screen fori := 5downto1do for i := 5 downto 1 do write( i, ‘ ‘ );

2 nd Semester Nested For Loop fortodo for x := 1 to 3 do fortodo for y := 20 to 25 do writeln(x:5, y:5);

2 nd Semester Nested For Loop fortodo for i:=1 to n dobegin for j:=1 to i do begin write( j, ' ' ); end; writeln;end;

2 nd Semester Outline  WHILE…DO VS FOR...DO  For…TO…DO Syntax Example

2 nd Semester Example: Convert Celcius to Farenheit Celcius Farenheit … … Farenheit = Celcius * (9/5) + 32

2 nd Semester WHILE…DO: Convert Celcius to Farenheit Program Celsius_to_Fahrenheit(input, output); Var Far : Real; Cel : Integer; begin Writeln(’Celsius’:10, ’Fahrenheit’:15); {While Loop Begin Calculate Celsius to Fahrenheit} Cel := 0; {While Loop Begin Calculate Celsius to Fahrenheit} while (Cel <= 100) do begin Far := Cel * (9/ 5) + 32; writeln(cel:10, far:15:1); Cel := Cel + 1; end; end.

2 nd Semester FOR...DO: Convert Celcius to Farenheit Program Celsius_to_Fahrenheit(input, output); Var Far : Real; Cel : Integer; begin Writeln(’Celsius’:10, ’Fahrenheit’:15); {FOR Loop Begin Calculate Celsius to Fahrenheit} FOR CEL:=0 TO 100 DO begin Far := Cel * (9/ 5) + 32; writeln(cel:10, far:15:1); end; end.

2 nd Semester Conclusions Iteration While Loop Repeat-until LoopFor Loop

2 nd Semester Conclusions  For loop Syntax Used only in Count Controlled Style for todo for counter := initial to final do statement; fordowntodo for counter:=initial downto final do statement;

2 nd Semester What will we learn in Next Class? Looping Techniques REPEAT…UNTIL