informÁtica industrial - uc3mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2....

30
M. Abderrahim, A. Castro, J. C. Cas3llo Departamento de Ingeniería de Sistemas y Automá3ca PROGRAMACIÓN BÁSICA C++ (II) INFORMÁTICA INDUSTRIAL

Upload: others

Post on 21-Aug-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

M.Abderrahim,A.Castro,J.C.Cas3lloDepartamentodeIngenieríadeSistemasyAutomá3ca

PROGRAMACIÓNBÁSICAC++(II)

INFORMÁTICAINDUSTRIAL

Page 2: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

6.FlujodeControl

Page 3: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

FlujodeControl

Paracadaestudiante

Hacer

Mientraslaasignaturanoestéaprobada

Estudiar

Hacerelexamen

Sinoestásdeacuerdoconlacalificación ➭"

Irarevisión

Page 4: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

Operadoresparaelflujodecontrol

•  OperadoresdeRelación<><=>=

•  OperadoresdeIgualdad/Desigualdad==(==noes=,queesasignación)!=

•  Operadoreslógicos•  &&||!•  Todosdevuelven:

–  0silacondiciónesfalsa–  1cuandolacondiciónesválida

•  Engeneral,cualquiernúmerodiferentedecerosignifica“true”(salida“1”)

Page 5: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

if if(expresión) sentencia1; siguientessentencias;

–  Sisecumple“expresión”(salidalógicadiferentedecero),seejecuta“sentencia1”(yluegolassiguientessentencias).

–  Sinosecumple“expresión”(salidalógicaescero),seejecutandirectamentelassiguientessentencias,perono“sentencia1”.

Page 6: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

ifvoidmain(){

inti=0;if(i=1) cout<<"erroreneloperadordeigualdad";

}

Page 7: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

ifvoidmain(){

inti=0;if(i==1) cout<<"erroreneloperadordeigualdad";

}

Page 8: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

if-else

if(expresión)sentencia1;elsesentencia2;siguientessentencias;

Page 9: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

if-else

Si“expresión”esverdadero,seejecuta“sentencia1”.

Sino,seejecutasentencia2.Ysiempredepués,seejecutanlassiguentessentencias.

Page 10: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

if-else#include<iostream>#include<math.h>usingnamespacestd;intmain(){

doublenumero;cin>>numero;if(numero<0)cout<<"Numeronega3vo"<<endl;else{cout<<"Numeroposi3vo"<<endl;cout<<"Laraízcuadradaes"<<sqrt(numero)<<endl;}return0;

}

Page 11: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

If-elseanidado#include<iostream>usingnamespacestd;intmain(){

inti,j;i=3;j=-3;if(i<0)if(j<0) cout<<"ijmenoresque0"<<endl;else cout<<"inoesmenorquecero"<<endl;return0;

}

#include <iostream> using namespace std; int main() {

int i,j; i=3; j=-3; if(i<0){ if(j<0) cout<<"i j menores que 0"<<endl; }else cout<<"i no es menor que cero"<<endl; retun 0;

}

Page 12: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

If-elseanidadoif(exp1){if(exp2){sentencia1;}else{sentencia2;}}else{sentencia3;}

Page 13: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

If-elseanidado#include<iostream>usingnamespacestd;intmain(){

inti,j;i=3;j=-3;if(i<0){if(j<0)cout<<"ijmenoresque0"<<endl;elsecout<<"jnoesmenorquecero,aunqueisílosea"<<endl;}elsecout<<"inoesmenorquecero"<<endl;return0;

}

Page 14: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

If-elseanidadoif(exp1){sentencia1;}if(exp2){sentencia2;}else{

sentencia3;}

Lasentencia“else”estáasociadaconel“if”máscercano

Page 15: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

while

•  Siqueremosejecutarsentenciasquedependendeunacondición

while(expresión){

sentencia1;}

Page 16: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

while

Page 17: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

while

Page 18: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

for(exp1;exp2;exp3){statement1;

}

expresión1;while(expresión2){sentencia1;expresión3;

}

Seejecutadesdeelprincipio

Encadaiteración,severificaparadecidirejecutaronounanuevaiteración

Seejecutaencadaiteración

for

Page 19: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

for

•  Elusocomúnde“for”esenbuclesconunnúmeroconocidodeiteracionesinta[10];for(i=0;i<10;++i){ cout<<“elemento”<<““<<i<<“es”<<““<<a[i]<<endl;

}

sentencias

test

Page 20: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

for#include<iostream>usingnamespacestd;intmain(){

inti,j;for(i=0,j=0;i<5&&j<20;i++,j+=2)cout<<"ies"<<i<<"jes"<<j<<endl;return0;

}

Page 21: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

do-while•  Essimilara“while”,peroseu3lizacuandonecesitamosejecutarunconjuntodeinstruccionesporlomenosunavez(inclusocuandolaexpresiónesfalsa

do{sentencia1;}while(expresión);

Page 22: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

do-while#include<iostream>usingnamespacestd;intmain(){

inti=0;do{i+=1;}while(i<0);cout<<"Elvalorfinaldeies"<<i<<endl;return0;

}

Page 23: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

“break”y“con3nue”

•  “break”causalasalidadeunbucleanidado(interno)

while(1){cin>>x;if(x<0.0)break;elsecout<<"raizcuadrada"<<sqrt(x)<<endl;

}

Page 24: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

“break”y“con3nue”

•  con+nueinterrumpelaiteraciónactualdelbuclealasiguienteiteración

for(i=0;i<1000;i++){c=getchar();if('0'<=c&&c<='9') con3nue;

}

•  Soloseu3lizaconfor,whileydo-while

Page 25: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

breakandcon3nue

#include<iostream>usingnamespacestd;intmain(){

inti,j=0;for(i=0;i<5;i++){cout<<"ies"<<i<<"jes"<<j<<endl;if(j>1) break;j++;//j+=1;oj=j+1;}return0;

}

Page 26: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

breakandcon3nue

#include<iostream>usingnamespacestd;intmain(){

inti,j=0;for(i=0;i<5;i++){cout<<"ies"<<i<<"jes"<<j<<endl;if(j>1) con3nue;j++;//j+=1;oj=j+1;}return0;

}

Page 27: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

switch

•  Generalizaelusodemúl3plessentencias“if-else”

•  Normalmenteseu3lizacuandoexistenvariasopciones

•  Verificaychequeaelvalordeunavariableintroducida,permi3endoademásvalorespordefecto.

Page 28: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

switchswitch(c){case'a':

ints1;break;

case'b':ints2;

case'c':ints3;

break;default:ints4;

}

switch(c){case´a´: ints1; break;case´b´: ints2;case´c´: ints3; break;default: ints4;}

Page 29: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

switch

•  Es|picoponer“break”alfinaldecadacasoparaevitarentrarenelsiguiente.

•  Esmuyu3lparamenúsconselección(“Escribe1paracalcularlasuma,2paracalcularlaresta,…”).

Page 30: INFORMÁTICA INDUSTRIAL - UC3Mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2. 19. · – Si se cumple “expresión” (salida lógica diferente de cero), se

M.Abderrahim,A.Castro,J.C.Cas3lloDepartamentodeIngenieríadeSistemasyAutomá3ca

PROGRAMACIÓNBÁSICAC++(II)

INFORMÁTICAINDUSTRIAL