Looping(1) For … to … do... Can you do this? 123456123456 Write a program to display:

Slides:



Advertisements
Similar presentations
E.g.9 For to do loop for i:=1 to 10 do writeln(i); While do loop i:=1;
Advertisements

Selection Process If … then … else.... Condition Process 2 Process 1 Y.
Playing computer with logic problems Please use speaker notes for additional information!
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Looping while … do …. Condition Process 2 Process 1 Y Repeated Loop.
Enter question text... 1.Enter answer text.... Enter question text... 1.Enter answer text...
Enter question text... 1.Enter answer text.... Enter question text... 1.Enter answer text...
1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem
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 TamThe Bubble Sort in Pascal Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
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.
James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam program investors (input, output, investorList, updatedInvestorList); const MAXCLIENTS = 10; NAMELENGTH = 30; LENGTH = 30; type Client =
J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate your code.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Input and Output (IO) CSCE 110 Drawn from James Tam's material.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Scripting CBIS BASH Scripting Step 1 – Create the bash file. Usually a good idea to end it in.sh file1.sh Step 2 – Using CHMOD make the bash file.
Pascal Course Spring Introduction Designed: 1968/9 by Niklaus Wirth Published: 1970 Imperative, structural, procedural Static and strong.
Principles of programming languages 2: Answers for exercises
Lecture 13 Midterm overview When you know a thing, to hold that you know it, and when you do not know a thing, to allow that you do not know it: this is.
Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal.
Lecture 26 Final review. Problem 1 (10 points) How many times would the FOR-DO loops beginning with the following statements be executed? If the loop.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
Arithmetic in Pascal A Short Glance We will learn the followings in this chapter Arithmetic operators Order of precedence Assignment statements Arithmetic.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Pascal Course Spring Introduction Designed: 1968/9 by Niklaus Wirth Published: 1970 Imperative, structural, procedural Static and strong.
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Pascal Programming Written by Leung King Yung. Simple Program 1 begin end.
FOR := to do Definite Iteration statements execute a statement repeatedly for a definite number of times where must be of integer, char, boolean.
Exercise 1: IF/ELSE Step 1: Open NotePad++ and create “number.php” in your webspace Step 2: Write codes to do the following 1.Generate a random number.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 10.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
2 nd Semester Looping Technique FOR…DO Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang
SATMathVideos.Net A set S consists of all multiples of 4. Which of the following sets are contained within set S? A) S2 only B) S4 only C) S2 and S4 D)
Adapted from  2004 Prentice Hall, Inc. All rights reserved. 5 th and 4 th ed: Chapters 6,7,8 SY306 Web and Databases for Cyber Operations SlideSet #6:
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 11.
A Level Computing#BristolMet Session Objectives#U2 S3 MUST use/read programme flow charts accurately SHOULD adapt the calculator programme to include a.
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
For loop. Exercise 1 Write a program to have the user input three (3) numbers: (f)rom, (t)o, and (i)ncrement. Count from f to t in increments of i, inclusive.
2 nd Semester Module3 Selection Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
15.1 More About High-Level Programming Languages Syntax and Semantics Each programming language contains its own rules for both syntax and semantics. Syntax.
Written by Al.So. Software solutions
Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. Repetition in Pascal:
Think What will be the output?
تهیه و تنظیم: فاطمه قاسمی دانشگاه صنعتی شریف – پاییز 86
Көңіл күйлеріңіз қалай?
How can you model playing rock-paper-scissors?
Computer Science 2 Take out a piece of paper for the following dry runs. Label It Recursive Dry Runs 4/12/2018. It will be turned in when completed
Computer Science 1 For..do loop Dry Run Take notes on the For loop
How can you make a guessing game?
Computer Science I: Get out your notes.
How can you model playing rock-paper-scissors?
Dry Run Fix it Write a program
A bit of Review Review, Dry Run, Program.
Conditional statements (2)
Presentation transcript:

Looping(1) For … to … do...

Can you do this? Write a program to display:

Program E1; begin writeln(1); writeln(2); writeln(3); writeln(4); writeln(5); writeln(6); end. Done!

What about this? Please enter a number: Write a program to display: Please enter a number:

Looping Program E1; var j, k:integer; begin write(‘Please enter a number’:); readln(n); for j:=1 to n do writeln(j); end.

How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:

How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:3

How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:3

How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:3 1

How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:3 1

How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:3 1 2

How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:3 1 2

How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:

How does it work? Program E1; var j, n:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number: j>n

How does it work? Program E1; var j, k:integer; begin write(‘Please enter a number:’); readln(n); for j:=1 to n do writeln(j); end. Please enter a number:

FOR … TO … DO… for I:=1 to 5 do writeln(I); writeln(1); writeln(2); writeln(3); writeln(4); writeln(5);

FOR … DOWNTO … DO… for I:=5 downto 1 do writeln(I); writeln(5); writeln(4); writeln(3); writeln(2); writeln(1);

For with multiple statements Program E1; begin for j:=1 to 3 do begin writeln(j); writeln(‘-’); end; end

Exercise 1 Program E1; var j, n:integer; begin write(‘Please enter a number’); readln(n); for j:=n to 6 do writeln(j*2); end. Write down the output of the following program if 3 is the input.

Exercise 2 Program E1; var j, n:integer; begin write(‘Please enter a number’); readln(n); for j:=n downto 4 do if (j mod 2)=0 then writeln(j div 2) else writeln(j*2); end. Write down the output of the following program if 6 is the input.

Exercise 3 Write programs to display: Enter a number:5 Enter a number: Enter a number:5 2,4,6,8,10, Enter a number:5 1,3,5,7,9 Enter a number:5 Enter a number:16 6,8,10,12,14,16,

Answer Q3 Program E1; var a, b, j:integer; begin write(‘Enter a number:’); readln(a); write(‘Enter a number:’; readln(b); for j:=a to b do writeln(j); end.

Answer Q3 Program E1; var a, j:integer; begin write(‘Enter a number:’); readln(a); for j:=1 to a do write(j*2, ’,’); end.

Answer Q3 Program E1; var a, j:integer; begin write(‘Enter a number:’); readln(a); for j:=1 to a-1 do write(j*2-1, ’,’); writeln(a*2-1); end.

Answer Q3 Program E1; var a, b, j:integer; begin write(‘Enter a number:’); readln(a); write(‘Enter a number:’; readln(b); for j:=a to b do if (j mod 2)=0 then write(j, ’,’); end.