CCN 3 EstruturasRepeticao
-
Upload
rimla-oten -
Category
Documents
-
view
228 -
download
0
description
Transcript of CCN 3 EstruturasRepeticao
1
Introdução à Programação Pascal
Estruturas de Repetição
Profa. Cassilda Maria Ribeiro
1
Estruturas de Controle
z z z y y y
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
2
2
Estruturas de Repetição
3
Estrutura de Repetição (Teste no Início)Formas de Representação no Algoritmo
T
F
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
3
4
Estrutura Repetição While-Do
5
Estrutura While-doz
z
z
z
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
4
Exemplo - Somar números de 1 a Nprograma somainicio
leia(N)soma ← 0num ← 1Enquanto (Num <= N) faça
iniciosoma ← soma + numnum ← num + 1
fimescreva(soma)
fim Obs:Se N = 1, soma = 1, 1 iteraçãoSe N < 1, soma = 0, 0 iteração
Ex. calcular h(n)=1+1/2+1/3+...+1/nprogram exwhile;var n: integer;
h: real;beginwriteln(‘Entre com o valor de n’)read(n); write (‘Para n = ‘,n);h:=0;while n>0 do
beginh := h + 1/n;n := n-1;
end;writeln(‘o valor de h eh:‘,h);
end.
Ex: Input 10Output 2.928968253968E 00
writeln(h:10:4) 2.9289 writeln(h:8) 2.92E 00
Ex: Comando Repetitivo While-do
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
5
8
Estrutura de Repetição (Teste no Final)Formas de Representação no Algoritmo
F
T
9
Estrutura Repetição
Repeat-until
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
6
10
Estrutura Repeat-until
z
z
z
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.
Ex: Comando Repeat-Until
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
7
12
Estrutura de Repetição (Contada)
z
z z z
y y
13
Estrutura de Repetição (Contada)Formas de Representação no Algoritmo
←
F
←
←
V
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
8
14
Estrutura Repetição For
For-to
For-downto
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.
Ex: Comando for
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
9
16
Exercícios de Estruturas de Repetição
z
z
z
17
Valores de variáveis nas repetições
z
z
z
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
10
18
z
y
19
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')) dobegin
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.
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
11
20
z
y
21
program ACUMULA1;uses crt;var NUMERO,CONT:integer;beginclrscr;CONT:=0;write('FORNECA O NUMERO: ');readln(NUMERO);while (NUMERO <> -1) dobegin
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.
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
12
22
z
y
23
program ACUMULA2;uses crt;var NUMERO, CONT, CONT2 : integer;beginclrscr;CONT2:=0; CONT:=0;write('FORNECA O NUMERO: ');readln(NUMERO);
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
13
24
while (NUMERO <> -1) do beginCONT2:=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.
25
z y
x x
y
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
14
26
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) dobegin
if (PONTOS >=) 100 then ALTAS:=ALTAS+1
27
else if (PONTOS < 50) then BAIXAS:=BAIXAS+1;
N:=N+1;Writeln(‘forneça 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.
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
15
28
z y
5099...
35
23
11
++++=S
29
program TESTE4;uses crt;var N,D: integer;
S:real;beginclrscr;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.
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
16
30
z y
31
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.
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
17
32
z y
33
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 dobeginwrite('Forneca a ',I:1,'a. nota: '); readln(X);S:=S+X;
end;writeln('Media: ',S/N:5:2);readln; readln;
end.
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
18
34
z y
35
program TESTE7;uses crt;var
N,I:integer;FAT:real;
beginclrscr;write('Forneca um numero inteiro
maior ou igual a zero:');readln(N);writeln;FAT:=1;
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
19
36
for I:=1 to N dobegin
FAT:=FAT*I;end;
writeln('Fatorial: ',FAT:5:2);readln; readln;
end.
37
z y
∑n
ii!
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
20
38
program REPETE1;uses crt;varN,K,SOMA,PROD,MULT:integer;
beginclrscr;write('FORNECA O VALOR DE N: '); readln(N);SOMA:=0;
39
for k:=1 to N dobegin
PROD:=1;for MULT:=1 to K do
PROD:=PROD*MULT;SOMA:= SOMA+PROD;
end;writeln('RESULTADO DA SOMATORIA:‘
,SOMA);readln;readln;
end.
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com
21
40
z
z
.
Printed with FinePrint - purchase at www.fineprint.comPDF created with pdfFactory trial version www.pdffactory.com