Professional Documents
Culture Documents
InCo - FING
2013
Contents
1 La instruccin while 2
1.1 Ejemplo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Sintaxis de la instruccin while . . . . . . . . . . . . . . . . . . . 2
1.3 Semntica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Indentacin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.5 Lectura con centinela . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.6 Determinar si un nmero es primo . . . . . . . . . . . . . . . . . 3
1.7 Factorial de un nmero . . . . . . . . . . . . . . . . . . . . . . . 4
1.8 Divisin entera por restas . . . . . . . . . . . . . . . . . . . . . . 4
1.9 Banderas booleanas . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.10 Condicin compuesta . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 La instruccin repeat 5
2.1 Sintaxis de la instruccin repeat. . . . . . . . . . . . . . . . . . . 5
2.2 Semntica del repeat. . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Relacin con while . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4 Procesamiento de palabras . . . . . . . . . . . . . . . . . . . . . . 6
2.5 Esquema del procesamiento . . . . . . . . . . . . . . . . . . . . . 6
2.6 Versin final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1
1 La instruccin while
1.1 Ejemplo
(* inicializacion *)
suma:= 0;
(* mostrar resultado *)
WriteLn('La suma de los numeros es: ', suma)
end.
Donde:
1.3 Semntica
La ejecucin de una instruccin:
while expresin do instruccin
1.4 Indentacin
2
(* correcto *)
while condicin do
begin
hacer_algo;
hacer_otra_cosa
end
(* incorrecto *)
while condicin do
begin
hacer_algo;
hacer_otra_cosa
end
(* incorrecto *)
while condicin do
begin
hacer_algo;
hacer_otra_cosa
end
(* lectura adelantada *)
ReadLn(dato);
{inicializacin de acumuladores }
...
(* iteracin *)
while "dato no es centinela" do
begin
(* procesar dato *)
...
(* acumular *)
...
(* siguiente lectura *)
ReadLn(dato)
end
var
fin,numero, divisor : integer;
begin
ReadLn(numero);
fin:= trunc(sqrt(numero));
divisor:= 2;
while (divisor <= fin) and (numero mod divisor <> 0) do
divisor:= divisor + 1;
3
if divisor <= fin then
WriteLn('El numero ',numero,' no es primo')
else
WriteLn('El numero ',numero,' es primo')
end.
var
factorial,i,numero : integer;
begin
ReadLn(numero);
factorial:= 1;
i:= 2;
while i<=numero do
begin
factorial:= factorial * i;
i:= i+1
end;
Esto se considera una mala prctica de programacin, la solucin apropiada es con for.
var
dividendo,
resto,
divisor, cociente : integer;
begin
ReadLn(dividendo,divisor);
resto:= dividendo;
cociente:= 0;
while resto >= divisor do
begin
resto:= resto - divisor;
cociente:= cociente + 1
end;
WriteLn('El resto es: ', resto);
WriteLn('El cociente es: ', cociente)
end.
var
numero,cantidadPares,anterior : integer;
repetidos : boolean;
begin
ReadLn(numero);
repetidos:= false;
4
cantidadPares:= 0;
while not repetidos and (numero > 0) do
begin
if numero mod 2 = 0 then
cantidadPares:= cantidadPares + 1;
anterior:= numero;
ReadLn(numero);
repetidos:= numero = anterior
end;
Write('Numero: ');
ReadLn(numLeido);
intentos:= intentos + 1
end;
2 La instruccin repeat
2.1 Sintaxis de la instruccin repeat.
sentencia_repeat = repeat instruccin { ; instruccin } until expresin
Donde
5
repeat
instruccin;
...
instruccin
until expresin
La instruccin:
repeat
S
until C
es equivalente a:
S;
while not C do
begin
S
end
const
ESPACIO = ' ';
FIN = '.';
var
caracter : Char;
cantidadPalabras : Integer;
6
begin
cantidadPalabras:= 0;
[avanzar comienzo de palabra]
while [quedan palabras] do
begin
[leer-palabra]
cantidadPalabras:= cantidadPalabras + 1
(* inicializar contadores *)
cantidadPalabras:= 0;
(* saltear espacios *)
repeat
read(caracter)
until caracter <> ESPACIO;