You are on page 1of 16

PROGRAMACIN DE INGENIERA

S-06: Estructura Selectiva Mltiple


Ing. Jorge A. Martel Torres
Ingenieras

Contenido
Estructuras de Programacin
Secuencial, Decisin, Repetitivas
Anidaciones

Estructura de un Programa/Algoritmo
Entorno, Edicin, grabacin, otros

Elementos y estructura de un programa en C/C++


Tipos de datos.
Operadores, Tipos

Instrucciones de I/O
Sentencias: ;, { }, //, /* */
Sentencias cin >>, cout <<, endl
Funciones: printf(), scanf(), clrscr(), gotoxy(), getch()

UNTELS

JORGE A. MARTEL TORRES

Estructuras de Programacin

INICIO

DIAGRAMA DE FLUJO
SI SIMPLE

PROCESO

SI DOBLE

MIENTRAS
F

V
PROCESO

SALIDA
POR
PANTALLA

PROCESO

PROCESO

PROCESO
HACER MIENTRAS

FIN
SELECTOR DE CASOS
V

PROCESO

INICIO
F

INGRESO POR
EL TECLADO
1

OTRO

DESDE PARA

PROCESO

ACCIONES
S1

ACCIONES
S2

ACCIONES
S3

SALIDA
POR
PANTALLA

UNTELS
FIN

VI, VF, INCR

ACCIONES
OTROS
V

JORGE A. MARTEL TORRES

PROCESO

DIAGRAMA NASSI-SCHNEIDERMAN (D-NS)


CONDICION

Nombre del Algoritmo;


Inicio
Accin 1;
Accin 2;
Accin 1;
...
Accin n;
Fin.

MIENTRAS CONDICION

SI
Acciones que se
ejecutaran si la
CONDICION es
VERDAD

Acciones
por SI o
Verdad

CONDICION
SI

NO

Acciones
por SI o
Verdad

Acciones que se
ejecutaran mientras
QUE LACONDICION
es VERDAD

Acciones
por No o
Falsedad

MIENTRAS CONDICION
variable

OTRO

PARA VI, VF, INCREMENTO


ACCIONES

ACCIONES ACCIONES ACCIONES ACCIONES


S1
S2
S3
OTROS
FIN_PARA

UNTELS

JORGE A. MARTEL TORRES

PSEUDOCODIGO
SELECCION DOBLE
SECUENCIAL
Nombre del Algoritmo;
Inicio
Accin
Accin
Accin
...
Accin
Fin.

1;
2;
1;
n;

Si ( Condicin )
Acciones Si
la condicin
es verdad
Si_No
Acciones Si
la condicin
es Falso
Fin_Si

MIENTRAS
V

HACER
V

SELECCION SIMPLE

SELECTOR DE MULTIPLE

Si ( Condicin )

Seleccionar ( Condicin )
CASO n1:
Acciones
CASO n2:
Acciones
CASO n3:
Acciones
Fin_Seleccionar

Acciones Solo Si

la condicin
es Verdad
Fin_Si

UNTELS

JORGE A. MARTEL TORRES

Mientras ( Condicin )
Acciones Solo Si
la condicin es Verdad
Fin_Mientras

Hacer
Acciones Solo Si
la condicin es Verdad
Mientras ( Condicin )

PARA /DESDE/ HASTA


V

Para ( Vari; VIni; Vfin; Incre )


Acciones Que Solo Si
la Vari >= Vfin

Fin ( Condicin )
6

?
Estructuras Selectivas
!!Alguien tiene que ser!!

?
V

?
F

a
V

?
c

Estructuras Selectivas de Decisin Mltiple


SELECTOR DE CASOS

Lunes

( variable )
1

OTRO

Martes
ACCIONES ACCIONES ACCIONES ACCIONES
I
II
III
IV
Miercoles

C=2

SELECTOR DE CASOS

SELECTOR DE CASOS

(C)

Seleccionar ( variable )
CASO cons1 :
Instrucciones I ;
cons1

cons2

cons3

CASO cons2 :

DeLoContra

Instrucciones II ;

CASO cons3 :
ACCIONES
I

ACCIONES
II

ACCIONES
III

ACCIONES
IV

Instrucciones III ;
NingunoAnterior :
Instrucciones IV ;

Fin_Seleccionar

UNTELS

JORGE A. MARTEL TORRES

romper

Estructuras Selectivas de Decisin Mltiple


Pseudocodigo

( variable )

con1

con2

con3

OTRO

romper

Seleccionar ( variable )
CASO cons1 :
Instrucciones I ;

CASO cons2 :

ACCIONES ACCIONES ACCIONES ACCIONES


program
fisica
calculo
FACE

Instrucciones II ;

CASO cons3 :
Instrucciones III ;
NingunoAnterior :
Instrucciones IV ;

Fin_Seleccionar

(C)
break

cons1

cons2

cons3

DeLoContra

c/ c++
switch ( variable )
{
case cons1 :
Instrucciones I ;

case cons2 :
ACCIONES
I

ACCIONES
II

ACCIONES
III

Instrucciones II ;

ACCIONES
IV

case cons3 :
Instrucciones III ;
default :
Instrucciones IV ;

UNTELS

JORGE A. MARTEL TORRES

Lunes

Representar como
Dia

Programacin

Ejemplo

Letra

Numero

Imprime Curso

Lunes

"programacion"

Martes

"fisica"

Miercoles

"calculo"

char
dia

"feriado"

switch ( dia )
{
case L :
cout << Programacin\n ;
break ;
case M :
cout << Fsica\n;
break;
case C :
cout << Clculo\n ;
break;
default :
cout << Face - tota\n ;
}

otros
dia = L;

program

fisica

calculo

DeLoContra

face

cout

UNTELS

que semana

Martes

fsica

Miercoles

Se ingresa un
identificador de da(letra),
mostrar que curso me toca
por dia seleccionado.

( dia )

Dia

JORGE A. MARTEL TORRES

Clculo

dia ;
= L ;

<< que semana\n ;

10

Lunes

Representar como
Dia

Letra

Numero

Imprime Curso

Lunes

"programacion"

Martes

"fisica"

Miercoles

"calculo"

otros

Ejemplo

int
Dn

Se ingresa un identificador
de da(numero), mostrar
que curso me toca por dia
seleccionado.

( Dn )

program

fisica

calculo

Dia

que semana

fsica

DeLoContra

face

Clculo

Dn ;
= 2;

switch ( Dn )
{
case 1 :
cout << Programacin\n ;
break ;
case 2 :
cout << Fsica\n;
break;
case 3 :
cout << Clculo\n ;
break;
default :
cout << Face - tota\n ;
}
cout

UNTELS

Martes

Miercoles

"feriado"

Dn = 2;

Programacin

JORGE A. MARTEL TORRES

<< que semana\n ;

11

Estructura de un Programa en C++


Se ingresa desde el teclado un
identificador (opcion) de
carrera para mostrar el nombre
de la carrera por la pantalla
Representar como
carrera

Letra

Numero

Imprime

administracion

administracion"

ambiental

ambiental"

electronica

electronica"
no hay"

otros

UNTELS

char
chrCarrera;
main() {
cout << "Ingresar un Caracter : ;
cin >> chrCarrera;
switch ( chrCarrera ) {
case A :
cout << administracion;
break;
case B:
cout << ambiental;
break;
case E :
cout << Electronica;
break ;
default :
cout << No hay ;
}
}

JORGE A. MARTEL TORRES

12

Estructura de un Programa en C++


using namespace std ;
int
intN ;
main() {
cout << "Ingresar Nmero Entero: ;
cin >> intN;
switch ( intN )
{
case 1 :
cout << uno ;
break ;
case 2 :
cout << dos ;
break;
case 3 :
cout << tres ;
break;
default :
cout << no hay ;
}
}

UNTELS

Se ingresa desde el teclado un


identificador (opcion) numero
entero, mostrar el numero en letras
por la pantalla

Representar como
Numero

Letra

Numero

Imprime Curso

uno"

dos"

tres"

otros

JORGE A. MARTEL TORRES

no hay"

13

Selectiva mltiple con if - else anidados


if ( expresionLogica1 )
{
Instruccioenes1 ;
}
else if ( expresionLogica2 )
{
Instruccioenes2 ;
}
else if ( expresionLogica3 )
{
Instruccioenes3 ;
}
else if ( expresionLogica4 )
{
Instruccioenes4 ;
}
else
{
Instruccioenes5 ;
}

UNTELS

Para Trabajar con


rango (intervalos)
de datos.
Voltios (V)

mensaje

< 0;

1.2 ]

apagado

< 1.2; 2.5 ]

amarillo

< 2.5; 5.5 ]

rojo

< 5.5, 12.0 ]

verde

< 12 ; >

peligro?

JORGE A. MARTEL TORRES

2.7 ;

if ( ( v > 0 ) && ( v <= 1.2 ) ) {


cout << apagado << endl;
}
else if ( ( v > 1.2 ) && ( v <= 2.5 ) ) {
cout << amarillo << endl ;
}
else if ( ( v > 2.5 ) && ( v <= 5.5 ) ) {
cout << rojo << endl ;
}
else if ( ( v > 5.5 ) && ( v <= 12 ) ) {
cout << verde << endl ;
}
else {
cout << peligroso << endl ;
}

14

Selectiva mltiple con if - else anidados


cin >> v ;

Se ingresa un valor
que representa los
estados en voltios de
un semforo. Segn
el cuadro adjunto,
mostrar un mensaje
en funcin a los
valores ingresados a
un monitor de
estados.

UNTELS

Voltios (V)

mensaje

< 0;

1.2 ]

apagado

< 1.2; 2.5 ]

amarillo

< 2.5; 5.5 ]

rojo

< 5.5, 12.0 ]

verde

< 12 ; >

peligro?

JORGE A. MARTEL TORRES

if ( ( v > 0 ) && ( v <= 1.2 ) ) {


cout << apagado << endl;
}
else if ( ( v > 1.2 ) && ( v <= 2.5 ) ) {
cout << amarillo << endl ;
}
else if ( ( v > 2.5 ) && ( v <= 5.5 ) ) {
cout << rojo << endl ;
}
else if ( ( v > 5.5 ) && ( v <= 12 ) ) {
cout << verde << endl ;
}
else {
cout << peligroso << endl ;
}

15

Contadores
OP

+=
-=
=
*=
/=
%=
OP

++
--

UNTELS

Operacin
suma ms asignacion
resta ms asignacion
Asignacion simple
multiplicacion ms asignacion
division ms asignacion
modulo ms asignacion
Si

a ++;
++ b;
c --;
-- d;

Ejem plo Equivalente

c += 7; c = c + 7;
d -= 4; d = d - 4;
a = b;
e *= 5; e = e * 5
f /= 3; f = f / 3
g %= 9; g = g % 3

Operacin
Utiliza el valor actual, despus suma 1
suma 1, despus utiliza el valor actual
Utiliza el valor actual, despus quita 1
quita 1, despus utiliza el valor actual
JORGE A. MARTEL TORRES

16

You might also like