Computer Science II TURBO PASCAL

Similar documents
Computer Programming: C++

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Repetition Structures

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

Ch. 7: Control Structures

Annex A (Informative) Collected syntax The nonterminal symbols pointer-type, program, signed-number, simple-type, special-symbol, and structured-type

COMPUTER SCIENCES II Spring Term 2017 Asst.Prof.Elgin KILIÇ

Quick Reference Guide

A/L 2011_revision. PASCAL PROGRAMMING

Loops / Repetition Statements

520 Principles of Programming Languages. Arithmetic. Variable Declarations. 19: Pascal

Computer Science II TURBO PASCAL

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

Quick Reference Guide

Making Decisions In Pascal

Flow Control. CSC215 Lecture

3 The L oop Control Structure

CS110D: PROGRAMMING LANGUAGE I

Chapter 4: Control structures. Repetition

Fundamentals of Programming Session 13

Chapter 4: Control structures

Programming for Experimental Research. Flow Control

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Example: Monte Carlo Simulation 1

In Delphi script, when values are assigned to variables, the colon-equal operator is used; :=

Introduction. C provides two styles of flow control:

5. Selection: If and Switch Controls

Making Decisions In Pascal

Outline. 1 If Statement. 2 While Statement. 3 For Statement. 4 Nesting. 5 Applications. 6 Other Conditional and Loop Constructs 2 / 19

CHAPTER 9 FLOW OF CONTROL

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Getting Started With Pascal Programming

Loops / Repetition Statements

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

MARK SCHEME for the October/November 2013 series 9691 COMPUTING. 9691/21 Paper 2 (Written Paper), maximum raw mark 75

1.00 Lecture 5. Floating Point Anomalies

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

Flow of Control Execution Sequence

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CSE 130 Introduction to Programming in C Control Flow Revisited

ENGR 1181 MATLAB 09: For Loops 2

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

Getting Started With Pascal Programming

Structured Programming. Dr. Mohamed Khedr Lecture 9

Special Topics: Programming Languages

Assignment: 1. (Unit-1 Flowchart and Algorithm)

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

G Programming Languages - Fall 2012

Problem Solving and 'C' Programming

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Loops (while and for)

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

The PCAT Programming Language Reference Manual

CHAPTER : 9 FLOW OF CONTROL

Looping statement While loop

PERL MOCK TEST PERL MOCK TEST II

Programming, numerics and optimization

CS 4100 Block Structured Languages. Fixed vs Variable. Activation Record. Activation Records. State of an Activation

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Computational Physics - Fortran February 1997

Beginning Programming (Pascal) Lecture 75. Figure 1. type-declarations. simple-type. pointer-type. array-type. file-type. set-type.

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Object-Oriented Programming

Dept. of CSE, IIT KGP

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Chapter 7 Arrays. One-Dimensional Arrays. Fred Jack. Anna. Sue. Roy

APPM 2460: Week Three For, While and If s

Decision Making in C

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Programming for Engineers Iteration

Iterative Languages. Scoping

Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs to execute alternatives

(Refer Slide Time: 00:26)

X Language Definition

Quiz Determine the output of the following program:

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Lecture 7 Tao Wang 1

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Today s Topics. Team Project Introduce this year s team project. S/SL S/SL, the Syntax/Semantic Language. CISC 458 Winter J.R.

Why Is Repetition Needed?

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Information Science 1

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

Programming Languages

REPETITIVE EXECUTION: LOOPS

Prepared by: Shraddha Modi

Principles of Computer Science I

DECISION CONTROL AND LOOPING STATEMENTS

Maple for Math Majors. 14. Maple's Control Statements

Language Reference Manual simplicity

Transcription:

Computer Science II TURBO PASCAL WEEK6 LOOP structures Dr.ELGİN KILIÇ

Loop Type Description while-do loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. for-do loop (for-down to) repeat-until loop nested loops Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. Like a while statement, except that it tests the condition at the end of the loop body. You can use one or more loop inside any another while, for or repeat until loop.

Loop Control Statements Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Pascal supports the following control statements. Click the following links to check their details.

Control Statement Description break statement continue statement Terminates the loop or case statement and transfers execution to the statement immediately following the loop or case statement. Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. goto statement Transfers control to the labeled statement. Though it is not advised to use goto statement in your program.

For-do Loop A for-do loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Syntax The syntax for the for-do loop in Pascal is as follows for < variable > := < initial > to [down to] < final > do S ; name value value For example; for i:= 1 to 10 do writeln(i);

If there are more than one process to be repated. For-Do Loop is as follows FOR< variable > := < initial > to [down to] < final > do name value value BEGIN Processes; END; For example; FOR i:= 1 to 10 DO BEGIN writeln( Value of i is ); writeln(i); END;

Where, the variable-name specifies a variable of ordinal type, called control variable or index variable; initial_value and final_value values are values that the control variable can take; and S is the body of the for-do loop that could be a simple statement or a group of statements.

Here is the flow of control in a for-do loop The initial step is executed first, and only once. This step allows you to declare and initialize any loop control variables. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for-do loop. After the body of the for-do loop executes, the value of the variable is either increased or decreased. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for-do loop terminates.

Example: a) for i:=1 to 10 do writeln( Bilgisayar ); b) for i:=1 to 10 do; 10 defa döngü boşuna writeln( Bilgisayar ); döner ve döngüden sonra 1 tane Bilgisayar yazar!

Example: Write a Pascal Program to show ASCII (0-255 tane) codes on screen. Program w6_p2; Var i:char; begin writeln ( ASCII kod tablosu ); for i:= #0 to #255 do begin writeln (i); readln; end.

Computer Sciences II Dr.Elgin KILIÇ

EXAMPLE: For given 100 a, b integer pairs Y is calculated as Y=2a-3b Write a pascal program to find i) Average of positive Y values when a>=b ii) The minimum negative Y value and a,b pair giving this value when a<b

var k,p,n,a,b,tp,y,min,mina,minb: integer; avg: real; Begin p:=0; tp:=0; n:=0; for k:= 1 to 100 do BEGIN writeln( Enter, k,. a,b pair---------> );readln (a,b); y:=2*a-3*b; if (a > = b) and (y>0) then begin p: = p+1; tp: = tp+y;

if (a < b) and (y<0) then begin n: = n+1; if (n=1) then begin min:=y; mina:=a; minb:=b; if (y<min) then begin min:=y; mina:=a; minb:=b; END; {for için} write ( Answer of i =, p, tp/p); write ( Answer of ii=, min,mina,minb); End. readln;

NESTED For-Do LOOPS for variable1:=initial_value1 to [downto] final_value1 do begin for variable2:=initial_value2 to [downto] final_value2 do begin statement(s); FOR i:=1 to N DO BEGIN.. FOR j:=1 to M DO BEGIN statements END; END; One loop executes inside of another loop(s). Example structure: Outer loop (runs n times) Inner loop (runs m times) Body of inner loop (runs n x m times)

INITIAL VALUE of LOOP VARIABLE DOES NOT NEED TO START FROM ZERO or ONE. Example: for i : = a to b do İki for arasında başka for j : = c to d do işlem yoksa begin begin. Process / Processes; koymaya gerek yok.

What is the output of following source code? BEGIN for i:=1 to 4 do begin for j:=1 to 5 do begin writeln(i,' ',j); if j=3 then break; writeln('next i loop'); writeln('finished!'); END.

It yields : 1 1 1 2 1 3 Next i loop 2 1 2 2 2 3 Next i loop 3 1 3 2 3 3 Next i loop 4 1 4 2 4 3 Next i loop Finished!

What is the output of following source code? BEGIN for i:=1 to 5 do begin for j:=1 to 3 do begin writeln(i,' ',j); if i=2 then break; writeln('finished!'); END.

It yields : 1 1 1 2 1 3 2 1 2 2 2 3 Finished!

Example: Write a Pascal Program to find the PRIME NUMBERS betwen 2 to 50.

Program nestedprime; var number, j:integer; begin for number := 2 to 50 do begin for j := 2 to number do if (number mod j)=0 then break; {* if factor found, not prime *} if(j = number) then writeln(inumber, ' is prime' ); end.

Example: 480 integers are given in 20 groups each containing 24 integers. Write a Pascal Program to find a) Product of elements in each group which are >5 b) Average of positive values in each group c) The maximum of all PRODUCTS of Groups and the group which gives this MAXIMUM VALUE. PS: The solution in next slide based on a assumption that we have numbers which are greater than 5 in EACH GROUP. If not solution MUST CHANGE.

Var H, i, j, SS:byte; Enb, x,c,t,k:integer; ort:real; Begin for i :=1 to 20 DO begin c:=1; t:=0; ss:=0; for k:=1 to 24 DO Begin writeln( Enter numbers of group,i, element, k ); Read (x); if x > 5 then c: = c * x; if x > 0 then begin T: =T+x; SS: = SS + 1; {for k için}

if i = 1 then begin enb:= C; H : = i; if enb < c then begin enb: = c ;H: = i; ORT: = T/ss; write( answer of a =,c); write( answer of b =,ORT); {for i için} write ( Answer of c =,enb,h); Readln; End.

Example: 480 tourists came to İzmir by ship to visit Ephesus. There are 20 busses to carry them. Write a Pascal Program to find a) Average age of each bus b) The youngest tourists and oldest tourist in each BUS c) The maximum of all AVERAGES of BUSES and the BUS which gives this MAXIMUM VALUE. d) The youngest and oldest tourists and the number of the buses they travel in.

Homework: For given 10 a,b and n integers write a Pascal Program to calculate the following formula and find the following Y= i) Biggest Y value and a,b,n pair giving this. ii) n i 1 a a b b i i 1 i 3 Average of Y values when a>b