13.07.2015 Views

A practical introduction to Pascal programming language - GIARA

A practical introduction to Pascal programming language - GIARA

A practical introduction to Pascal programming language - GIARA

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Iterative constructionsProgram 46: Exercise 3.8program c03e08;vara,b,i:integer;beginwrite(’Please input the first integer: ’);readln(a);write(’Please input the second integer: ’);readln(b);for i:=b down<strong>to</strong> a doif (i mod 2 =1) thenwrite(i,’ ’);writeln;(* Jumping the line *)for i:=a <strong>to</strong> b doif (i mod 2 =0) thenwrite(i,’ ’);writelnend.⊡3.4.2 Computing with sequencesThe exercises on sequences are based on the analysis of a series of numbers or characters. Wenever know in advance how many elements the sequence will have, so while and repeat arethe mainstream options <strong>to</strong> handle the iteration. Moreover, the program is required <strong>to</strong> capturesome information about the elements of the sequence, so that we need <strong>to</strong> mantain some kindof data about what we have seen so far, but at the same time having the ability <strong>to</strong> modify thatinformation depending upon the elements in the sequence.Exercise 3.9. Write a program that demands the user for as many characters as necessary untilthe user inputs the same character 2 times in a row.□Now we don’t have any idea of how many attempts it will take the user <strong>to</strong> satisfy thecondition. Hence, we use a while structure. In this exercise we use the variable b <strong>to</strong> save thelast value, while the one before the last one is s<strong>to</strong>red in a. In this way, each time the user inputs anew number, a is updated (taking the value in b), while b s<strong>to</strong>res the new one.⊡Program 47: Exercise 3.9program c03e09;vara,b:char;beginwrite(’Please input a char and press enter: ’);readln(a);write(’Please input the second char and press enter: ’);readln(b);while (ab) dobegina:=b;write(’Please another char and press enter: ’);readln(b)endend.45

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!