Vous êtes sur la page 1sur 11

ESTRUCTURAS DE CONTROL Y CONDICIONALES.

1. DIAGRAMAS DE FLUJO.

2. FLUJOS DE CONTROL.

CONDICIONAL (if).
if A ==0,
Sentencia 1
else
Sentencia 2

if A > B
Sentencia 1
else
Sentencia 2
if A<B
if A && B && C

SWITCH AND CASE.


switch (input)
case 0
Sentencia 1
case 1
Sentencia 2
case 2
Sentencia 3
case a
Sentencia 4
otherwise
variable no reconocida o error
end

FOR
for i=0:i=i+1:i<50 i=0;i=1;i=2;…;i=49
Sentencia
end
for j=100:j=j-1:j<1 j=100;j=99;j=98;…;j=2)
Sentencia

WHILE
while b = 0
Sentencia
end

while b+a < 100


Sentencia
end

BREAK
while a==10
B=C+D;
if E==1
break

INTRODUCCIÓN DE DATOS Y FUNCIONES.


Introducir números
n=input('INGRESE EL NÚMERO DE PUNTOS n: ');
Introducir funciones
f=input('INGRESE FUNCION: ','s');
EJEMPLO 1. Realizar un programa para generar la
secuencia 2𝑛 . n natural.
20 21 22 23 2𝑛−1 2𝑛
1 2 4 8 2𝑛−1 2𝑛

PROGRAMA
n=6;
resultado=2^n
EJEMPLO 2. Hallar la secuencia de números pares para que
estos sean almacenados si se introduce la letra p y hallar la
secuencia de números impares cuando se introduzca la
letra i.
PARES: 2,4,6,8,10,12,…,2*n
n=1, n
IMPARES: 1,3,5,7,9,…,(2*n)-1
n=1, n
n=input('INGRESE EL NÚMERO n: ');
p=input('INGRESE EL NÚMERO PAR=1 IMPAR=2: ');

if p==1
for i=1:n
M=2*i
end

else
for i=1:n
M=2*i-1
end
end

n=input('INGRESE EL NÚMERO n: ');


p=input('INGRESE EL NÚMERO PAR=1 IMPAR=2: ');

fprintf('La secuencia es \n')

if p==1
for i=1:n
M=2*i;
fprintf('%d \t',M)
end

else
for i=1:n
M=2*i-1
fprintf('%d \t',M)
end
end

EJEMPLO 3. Identificar el número mayor de 3 números.


a=input('INGRESE EL NÚMERO a: ');
b=input('INGRESE EL NÚMERO b: ');
c=input('INGRESE EL NÚMERO c: ')

fprintf('El número mayor es \n')

M=a;
N=M-b;
if N>0
M=a;
else
M=b;
end
P=M-c;
if P>0
M
else
M=c;
M
end

Vous aimerez peut-être aussi