Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Lenguaje de Programación
Introducción
Concepto de Programa
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
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
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
Instrucciones de Escritura
Diagrama de flujo
Código Matlab
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
- Simples
- Dobles
- Múltiples
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación
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
PROBLEMA
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
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
PROBLEMA
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
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
PROBLEMA
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
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
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
PROBLEMA
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
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
- Mientras
- Repetir
- Para
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación
Diagrama de flujo
------------
------------
F Expresión
Lógica
V
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación
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
PROBLEMA
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
%
for a=Vi:Inc:Vf
a +Inc > Vf
a=Vi, Inc, Vf
---------
---------
---------
--------------- end
--------------- %
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación
PROBLEMA
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
Diagrama de flujo
Código Matlab
a +Inc > Vf
a=Vi, Inc, Vf
%
for a=Vi:Inc:Vf
Sentencias 1 Sentencias 1
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
end
end
%
ESTRUCTURAS GENERALES DE PROGRAMACIÓN
Lenguaje de Programación
Elemento interruptor
PROBLEMA
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