informÁtica industrial - uc3mocw.uc3m.es/ingenieria-de-sistemas-y-automatica/inform... · 2018. 2....
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/23.jpg)
“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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/24.jpg)
“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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/28.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/29.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022063013/5fcc909c8e047944a20c53ec/html5/thumbnails/30.jpg)
M.Abderrahim,A.Castro,J.C.Cas3lloDepartamentodeIngenieríadeSistemasyAutomá3ca
PROGRAMACIÓNBÁSICAC++(II)
INFORMÁTICAINDUSTRIAL