Está en la página 1de 36

ESTRUCTURAS GENERALES DE PROGRAMACIÓN

Lenguaje de Programación

Introducción

Concepto de Programa

Un programa de computador es un conjunto de


instrucciones, órdenes dadas a la máquina, que
producirán la ejecución de una determinada tarea.

En esencia, un programa es
un medio para conseguir un fin.

¿Cuál es el fin?
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Instrucción de asignación numérica

Diagrama de flujo

Código Matlab
a pi/2.54 a=pi/2.54;
u 45.76a u=45.76*a;
t 4a/pi t=4*a/pi;
y 54.9 y=54.9;
C 10*cos(a)+(u+5t)/(3y) C=10*cos(a)+(u+5*t)/(3*y)
y y+1 y=y+1
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Instrucción de asignación alfanumérica

Diagrama de flujo

Código Matlab
nombre Pedro nombre=‘Pedro’;
Q1 nombre Q1=nombre;
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Instrucciones de Lectura

Diagrama de flujo
Código Matlab

Numérica
Leer Radio=input(‘Ingresar Radio= ‘);
Radio, Ang Ang=input(‘Ingresar Angulo= ‘);

Alfanumérica Leer
animal=input(‘Ingresar Animal= ‘,’s’);
animal

Alfanumérica Leer Color=input(‘Ingresar Color= ‘,’s’);


y numérica
Color, precio precio=input(‘Ingresar Precio= ‘);
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Instrucciones de Escritura

Diagrama de flujo
Código Matlab

IMPRIMIR fprintf(1,’\n La suma es: %f’,Sum);


“La suma es:”,Sum

Diagrama de flujo

Código Matlab
IMPRIMIR
“El color es:”,P fprintf(1,’\n El color es: %s’,P);
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura Secuencial

clear all
INICIO %
% Lectura de datos
% ----------------------
LEER a=input(‘Ingresar valor 1: ’);
a,b b=input(‘Ingresar valor 2: ’);
%
% Cálculos
% ------------
S a+b S=a+b;
P a*b P=a*b;
%
% Impresión de resultados
IMPRIMIR % --------------------------------
“La suma es:”,S fprintf(1,’\n La suma es: %f’,S);
“El producto es:”,P fprintf(1,’\n El producto es: %f’,P)
%
close all
FIN
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructuras selectivas

Las estructuras selectivas se utilizan


para que el programa
tome decisiones a partir de la evaluación
de expresiones lógicas

Las estructuras selectivas básicas son:

- Simples

- Dobles

- Múltiples
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: simple

Diagrama de flujo

Código Matlab

Expresión F %
if (Expresión lógica)
lógica
-----
-----
V -----
end
%
----------
----------
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: simple

PROBLEMA

Diseñar un programa utilizando diagrama de flujo, que lea por


consola tres números, si el primero es negativo, debe imprimir
el producto de los tres y si no lo es, debe imprimir la suma.
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: simple

INICIO
LEER
a,b,c
clear all
Res=a+b+c a=input(‘Ingrese numero:’);
b=input(‘Ingrese numero:’);
c=input(‘Ingrese numero:’);
a<0 F
Res=a+b+c;
if(a<0)
V Res=a*b*c;
Res=a*b*c end
fprintf(1,’\nEl resultado
es: %f’,Res);
close all
IMPRIMIR
“El resultado es”, Res

FIN
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: doble

Diagrama de flujo
Código Matlab

%
if (Expresión lógica)
V Expresión F
Sentencias 1
lógica
else

Sentencias 2
Sentencias 1 Sentencias 2
end
%
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: doble

PROBLEMA

Diseñar un algoritmo que calcule el área de un triángulo


a partir de los largos: a, b, c.
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: doble

INICIO
clear all
LEER a=input(‘Ingrese largo 1:’);
a,b,c b=input(‘Ingrese largo 2:’);
c=input(‘Ingrese largo 3:’);
P=(a+b+c)/2;
a+b+c if(((P>a)&(P>b))&(P>c))
P= Area=sqrt(P*(P-a)*(P-b)*(P-c));
2
fprintf(1,’\nEl area
es: %f’,Area);
else
F fprintf(1,’\nNo es un
((P>a)&(P>b))&(P>c) triangulo’);
end
close all
V
Area = P ( P − a )( P − b)( P − c)
IMPRIMIR
“No es un triangulo”
IMPRIMIR
“El area es:”, Area

FIN
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: múltiple


Diagrama de flujo

F
Exp 1 Código Matlab
V
%
Sentencias 1
if (Exp 1)
Sentencias 1
elseif (Exp 2)
Exp 2 F Sentencias 2
elseif (Exp n)
V Sentencias n
Sentencias 2 else
Sentencias m
end
%
Exp n F

V
Sentencias n Sentencias m
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: múltiple

PROBLEMA

Diseñar un algoritmo que lea una calificación


en escala numérica de 1 a 10 y la transforme
al equivalente en escala conceptual.

Tabla de equivalencia
1 ≤ Nota < 5LL L Insuficiente
5 ≤ Nota < 6LL L Suficiente
6 ≤ Nota < 8LL L Bueno
8 ≤ Nota ≤ 10 L LL Sobresaliente
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: múltiple

INICIO
a b c
Leer
Nota 6 ≤ Nota < 8 F
I mprimir
Concep
V
1 ≤ Nota < 5 F
Concep =' Bueno'
FIN
V
Concep =' Insuficiente' F
8 ≤ Nota ≤ 10

V
5 ≤ Nota < 6 F
Concep =' Sobresaliente'

V
Concep =' Suficiente'
Concep =' La nota no es válida '

a b
c
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: múltiple

clear all
nota=input(‘Ingrese nota:’);
if((nota>=0)&(nota<5))
Concep=‘Insuficiente’;
elseif ((nota>=5)&(nota<6))
Concep=‘Suficiente’;
elseif ((nota>=6)&(nota<8))
Concep=‘Bueno’;
elseif ((nota>=8)&(nota<=10))
Concep=‘Sobresaliente’;
else
Concep=‘La nota no es valida’)
end
fprintf(1,’\nConcepto: %s’,Concep);
close all
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: múltiple

Diagrama de flujo Código Matlab


%
switch (Expr. arit.)
case V1
Expresión Sentencias V1
Aritmética case V2
Sentencias V2
case Vn
V1 Sentencias Vn
Otro
V2 Vn otherwise
Sentencias Otro
Sentencias Sentencias Sentencias Sentencias end
V1 V2 Vn Otro %
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: múltiple

PROBLEMA

Diseñar un algoritmo que escriba el nombre


del día de la semana en función del valor ingresado por teclado.

Valor = 1 corresponde al lunes


Valor = 2 corresponde al martes
…….
…….
Valor = 7 corresponde al domingo
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura selectiva: múltiple

INICIO

Leer
posdia

posdia

1 2 3 4 5 6 7 Otro

d =' Lunes ' d =' Martes' d =' Miércoles' d =' Jueves' d ='Viernes' d =' Sabado' d =' Domingo' d =' No válido'

I mprimir
d

FIN
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructuras selectivas: anidadas


Diagrama de flujo

F
Exp. 1
Código Matlab
V
Sentencias 1 %
if (Exp. 1)
Sentencias 1
F if (Exp. 2)
Exp. 2
if (Exp. 3)
V Sentencias 3
F V else
Exp. 3 Sentencias 2
end
Sentencias 2 Sentencias 3
end
Sentencias 4
end
%

Sentencias 4
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructuras repetitivas

Los computadores están especialmente diseñados para realizar


aplicaciones en que una operación debe repetirse muchas veces.

Las estructuras que repiten una secuencia de instrucciones un número


determinado de veces se denominan bucles.

La repetición de una secuencia de acciones se llama ITERACIÓN

La estructura de repetición requiere de una CONDICIÓN DE SALIDA

Las estructuras repetitivas básicas son:

- Mientras
- Repetir
- Para
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura repetitiva: Repetir

La secuencia se repite mientras la expresión lógica sea FALSA

Diagrama de flujo

------------
------------

F Expresión
Lógica

V
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura repetitiva: Mientras

La secuencia se repite mientras la expresión lógica sea VERDADERA

Diagrama de flujo
Código Matlab

Expresión F %
Lógica while (Expresión Lógica)
V -------
-------
----------- -------
----------- end
%
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura repetitiva: Mientras

PROBLEMA

Diseñar un algoritmo que encuentre el entero positivo


mayor (num) para el cual la suma 1 + 2 + 3 +…….+ num sea menor
o igual que un valor ingresado por teclado (limit)
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura repetitiva: Mientras

INICIO

Leer
limit

S =0 clear all
Num = 0 limit=input(‘Ingrese limite:’);
S=0;
Num=0;
S ≤ limit while (S<=limit)
Num=Num+1;
S=S+Num;
Num = Num + 1
end
S = S + Num Num=Num-1;
fprintf(1,’\n Numero = %f’,Num);
close all

Num = Num − 1

I mprimir
Num

FIN
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura repetitiva: Para

La secuencia se repite un número predeterminado de veces

Diagrama de flujo Código Matlab

%
for a=Vi:Inc:Vf
a +Inc > Vf
a=Vi, Inc, Vf
---------
---------
---------
--------------- end
--------------- %
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura repetitiva: Para

PROBLEMA

Diseñar un algoritmo que permita determinar la


suma de los 20 números enteros siguientes a un valor
ingresado por teclado.
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructura repetitiva: Para

INICIO

Leer
val

S =0 clear all
ValI = val + 1 val=input(‘Ingrese valor:’);
S=0;
ValF = val + 20
ValI=val+1;
ValF=val+20;
for K=ValI:1:ValF
K = ValI , ValF , 1 S=S+K;
end
fprintf(1,’\n Suma = %f’,S);
S =S+K close all

I mprimir
S

FIN
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructuras repetitivas: anidadas

Diagrama de flujo
Código Matlab
a +Inc > Vf
a=Vi, Inc, Vf
%
for a=Vi:Inc:Vf

Sentencias 1 Sentencias 1

while (Exp. Lóg)


Exp. Lóg. F
Sentencias 2
V end
Sentencias 2 end
%
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Estructuras repetitivas: aborto

Diagrama de flujo
Código Matlab
a +Inc > Vf
a=Vi, Inc, Vf %
for a=Vi:Inc:Vf

Sentencias 1 Sentencias 1

If (Exp. Lóg)
Exp. Lóg. F
Sentencias 2

V Abortar else
break
Sentencias 2 end
end
%
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Elemento Contador
Variable cuyo valor se incrementa o decrementa
una cantidad constante en cada iteración

Diagrama de flujo
Código Matlab
Cont 0
%
Cont=0
F
Cont < K while (Cont < K)

V Sentencias
Sentencias
Cont=Cont+p
end
Cont Cont + p %
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Elemento Acumulador
Variable que totaliza el resultado
Diagrama de flujo de sumas sucesivas

N 0
S 0 Código Matlab
%
F N=0
N<K S=0
while (N < K)
V
-------------- -------

N=N+1
N N+1 S=S+R
S S+R end
%
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Elemento interruptor
Variable que toma valores fijos
Ejemplo 0 y 1
Código Matlab

%
Diagrama de flujo for i=a:b
if (Sw==1)
i +1 > b Sw=0
i=a,b
Sentencias 2
F V
Sw=1 else
Sw=1 Sw=0 Sw=1

Sentencias 1 Sentencias 2 Sentencias 1

end
end
%
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Elemento interruptor

PROBLEMA

Diseñar un algoritmo que obtenga la suma de los números


pares y la suma de los número impares
comprendidos entre 1 y 100, utilizando una sola interación.
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación

Elemento interruptor

INICIO
clear all
sw = 0 sw=0
sp=0;
sp = 0
simp=0;
simp = 0 for n=1:100
if(sw==0)
simp=simp+n;
n = 1, 100 sw=1;
else
sp=sp+n
V F sw=0;
sw = 0 end
simp = simp + n sp = sp + n end
fprintf(1,’\n Suma par = %f’,sp);
sw = 1 sw = 0 fprintf(1,’\n Suma impar = %f’,simp);
close all

I mprimir
sp, simp

FIN

También podría gustarte