Vous êtes sur la page 1sur 21

Introduo Programao

Pascal
Estruturas de Repetio

Profa. Cassilda Maria Ribeiro

Estruturas de Controle
z
z
z
y
y
y

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

Estruturas de Repetio

Estrutura de Repetio (Teste no Incio)


Formas de Representao no Algoritmo

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

Estrutura Repetio While-Do

Estrutura While-do
z
z
z

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

Exemplo -

Somar nmeros de 1 a N

programa soma
inicio
leia(N)
soma 0
num 1
Enquanto (Num <= N) faa
inicio
soma soma + num
num num + 1
fim
escreva(soma)
Obs:
fim
Se N = 1, soma = 1, 1 iterao
Se N < 1, soma = 0, 0 iterao

Ex: Comando Repetitivo While-do


Ex. calcular h(n)=1+1/2+1/3+...+1/n
program exwhile;
var n: integer;
h: real;
begin
writeln(Entre com o valor de n)
read(n);
write (Para n = ,n);
Ex:
h:=0;
while n>0 do
Input 10
begin
Output
h := h + 1/n;
2.928968253968E 00
n := n-1;
end;
writeln(o valor de h eh:,h); writeln(h:10:4) 2.9289
end.
writeln(h:8) 2.92E 00

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

Estrutura de Repetio

(Teste no Final)
Formas de Representao no Algoritmo

Estrutura Repetio
Repeat-until

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

Estrutura Repeat-until
z
z
z

10

Ex: Comando Repeat-Until


Ex. Calcular h(n)=1+1/2+1/3+...+1/n
program exrepeat;
var n: integer;
h: real;
begin
writeln(entre com o valor de n)
read(n);
write (Para n =,n);
h: = 0;
repeat
h: = h + 1/n;
n: = n-1
until n=0;
write (O valor de h eh:);
writeln(h);
end.

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

Estrutura de Repetio

(Contada)

z
z
z
z
y
y

12

Estrutura de Repetio (Contada)


Formas de Representao no Algoritmo

13

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

Estrutura Repetio For


For-to

For-downto

14

Ex: Comando for


Ex. calcular h(n)=1+1/2+1/3+...+1/n
program exfor;
var n: integer; h: real;
begin
writeln(Entre com o valor de n)
read(n); write(Para n = ,n);
h:= 0;
for i: = n downto 1 do
h:= h + 1/i;
writeln(O valor de h = ,h)
end.

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

Exerccios de Estruturas de Repetio


z

16

Valores de variveis nas repeties

17

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

z
y

18

program TESTE1;
uses crt;
var ANO : integer; SEXO : char;
begin
clrscr;
write(Entre com o SEXO: ');
readln(SEXO);
while ((SEXO<>'F') and (SEXO<>'M') and
(SEXO<>'f') and (SEXO<>'m')) do
begin
gotoxy(7,2); writeln ('ERRO!!!');
gotoxy(7,1); readln(SEXO);
end;
gotoxy(7,2);
writeln('
');
write(Entre com o ANO: ');
readln(ANO); writeln;
writeln('':14,'SAIDA');
writeln('ANO = ',ANO:4,'':12,'SEXO:
',SEXO);
readln;
end.

19

10

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

z
y

20

program ACUMULA1;
uses crt;
var NUMERO,CONT:integer;
begin
clrscr;
CONT:=0;
write('FORNECA O NUMERO: ');
readln(NUMERO);
while (NUMERO <> -1) do
begin
if (NUMERO > 8) then CONT:=CONT+1;
write('FORNECA O NUMERO: ');
readln(NUMERO);
end;
writeln('QUANTIDADE DE NUMEROS MAIORES
QUE 8: ',CONT);
readln;
readln;
end.

21

11

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

z
y

22

program ACUMULA2;
uses crt;
var NUMERO, CONT, CONT2 : integer;
begin
clrscr;
CONT2:=0;
CONT:=0;
write('FORNECA O NUMERO: ');
readln(NUMERO);
23

12

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

while (NUMERO <> -1) do


begin
CONT2:=CONT2+1;
if (NUMERO > 8)then CONT:=CONT+1;
write('FORNECA O NUMERO: ');
readln(NUMERO);
end;
writeln('QUANTIDADE DE NUMEROS LIDOS:',
CONT2);
writeln('QUANTIDADE DE NUMEROS MAIORES
QUE 8: ',CONT);
readln;
end.
24

z
y

x
x

25

13

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

program TESTE2;
var N,ALTAS,BAIXAS,PONTOS:integer;
begin
N:=0;
ALTAS:=0;
BAIXAS:=0;
writeln('FORNECA OS PONTOS OBTIDOS');
readln(PONTOS);
while (PONTOS <= 150) do
begin
if (PONTOS >=) 100
then ALTAS:=ALTAS+1
26

else

if (PONTOS < 50)


then BAIXAS:=BAIXAS+1;
N:=N+1;
Writeln(fornea os pontos do aluno,
digite um no. > 150 para parar)
readln(PONTOS);
end;
writeln('NUMERO DE ALUNOS: ',N:2);
writeln('NUMERO DE NOTAS ALTAS: ',ALTAS:2);
writeln('NUMERO DE NOTAS BAIXAS: ',BAIXAS:2);
readln;
readln;
end.
27

14

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

z
y

1 3 5
99
S = + + + ... +
1 2 3
50

28

program TESTE4;
uses crt;
var N,D: integer;
S:real;
begin
clrscr;
S:=0;
N:=1;
D:=1;
repeat
S:=S+(N/D);
N:=N+2;
D:=D+1;
until (D = 50);
writeln('VALOR DA SERIE: ',S:5:2);
readln;readln;
end.
29

15

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

z
y

30

program TESTE5;
uses crt;
var
PENULT,ULT,SOMA:integer;
begin
clrscr;
PENULT:=0;
ULT:=1;
write(PENULT,',',ULT,',');
repeat
SOMA:=PENULT+ULT;
write(SOMA,',');
PENULT:=ULT;
ULT:=SOMA
until (SOMA > 5000);
readln;readln;
end.
31

16

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

z
y

32

program TESTE6;
uses crt;
var
N,I:integer;
S,X:real;
Begin {Inicio do Programa}
clrscr;
write('Forneca o numero de notas: ');
readln(N);
writeln;{pula uma linha}
S:=0;
for I:=1 to N do
begin
write('Forneca a ',I:1,'a. nota: ');
readln(X);
S:=S+X;
end;
writeln('Media: ',S/N:5:2);
readln; readln;
end.

33

17

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

z
y

34

program TESTE7;
uses crt;
var
N,I:integer;
FAT:real;
begin
clrscr;
write('Forneca um numero inteiro
maior ou igual a zero:');
readln(N);
writeln;
FAT:=1;
35

18

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

for I:=1 to N do
begin
FAT:=FAT*I;
end;
writeln('Fatorial: ',FAT:5:2);
readln;
readln;
end.

36

z
y

i!
i

37

19

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

program REPETE1;
uses crt;
var
N,K,SOMA,PROD,MULT:integer;
begin
clrscr;
write('FORNECA O VALOR DE N: ');
readln(N);
SOMA:=0;

38

for k:=1 to N do
begin
PROD:=1;
for MULT:=1 to K do
PROD:=PROD*MULT;
SOMA:= SOMA+PROD;
end;
writeln('RESULTADO DA SOMATORIA:
,SOMA);
readln;
readln;
end.
39

20

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

40

21

Printed
PDF created
with FinePrint
with pdfFactory
- purchase
trial at
version
www.fineprint.com
www.pdffactory.com

Vous aimerez peut-être aussi