Académique Documents
Professionnel Documents
Culture Documents
Variable
Selector Otro
Caso
Sentencia(s)
Sentencia(s)
Sentencia(s)
Sentencia(s)
Sentencia(s)
Sentencia(s)
Sentencia(s)
Sentencia(s)
Sentencia(s)
Variable
Selector
. .
Otro
Caso
switch(<variable selector>)
{ case <constante 1>:sentencia1(s);[break;]
case <constante 2>:sentencia2(s);[break;]
case <constante 3>:sentencia3(s);[break;]
..
..
default:
sentencia(s);
}
Analisis
Datos entrada
Datos salida
#include<iostream>
using namespace std;
//Uso de la sentencia switch
//Funcion principal
int main()
{ int dia;
cout<<endl<<"Ingrese numero de dia ?:";
cin>>dia; cout<<endl;
Datos entrada
Datos salida
while(<condicion>)
Sentencia(s);
No <Condicion>
Condicin
Si
Sentencia(s)
Sentencia(s)
Pseudocodigo
Mientras(<condicion>)
Sentencias(s)
FinMientras
Donde:
<condicion>: Es la condicin que debe probarse, si es
verdadera se ejecuta las sentencia que viene.
Luego alcanzado el fin de mientras, regresa a probar la
condicin y el proceso se repite.
Si condicin es falso, el bucle termina.
Fin
C50
Mientras(C<=100) No
C<=100 Fin
Escribir(C, 1.8*C + 32 Si
CC + 10
Escribir(C, 1.8*C + 32)
Fin de mientras
Fin
CC+10
#include <iostream>
using namespace std;
//Codificacion usando while
int main()
{ float C=50;
cout<<"gC\tgF\n";
cout<<"**************"<<endl;
cout<<endl;
while (C<=100)
{ cout<<C<<"\t"<<(1.8*C+32)<<endl;
C= C+10;
}
cout<<endl;
}
Escribir(Ingrese numero:)
Leer(N)
aN
No
Escribir(a,tiene,k,digitos
N>0 )
Si
Fin
NN/10
kk+1
ING. FELIX HUARI 34
EJEMPLO DE USO SENTENCIA WHILE
CARTA N-S Inicio
Fin 35
EJEMPLO DE USO SENTENCIA WHILE
#include<iostream>
using namespace std;
//Uso del while
//Funcion principal
int main()
{ int N, k, a;
k=0;
cout<<"Ingrese Numero:";
cin>>N;
a=N;
while(N>0)
{ N=N/10;
k=k+1;
}
cout<<endl
cout<<a<<" Tiene "<<k<<" digitos\n";
}
36
PROBLEMA PROPUESTO
a) Suppose that the input is:
58 23 46 75 98 150 12 176 145 -999
What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{ int num;
cin >> num;
while (num != -999)
{ cout << num % 25 << " ";
cin >> num;
}
cout << endl;
return 0;
} 37
PROBLEMA PROPUESTO
b) The following program is designed to input two numbers and output
their sum. It asks the user if he/she would like to run the program. If the
answer is Y or y, it prompts the user to enter two numbers. After adding
the numbers and displaying the results, it again asks the user if he/she
would like to add more numbers. However, the program fails to do so.
Correct the program so that it works properly.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ char response;
double num1, num2;
cout << "This program adds two numbers." << endl;
cout << "Would you like to run the program: (Y/y) ";
cin >> response; cout << endl;
38
PROBLEMA PROPUESTO
cout << fixed << showpoint << setprecision(2);
while (response == 'Y' && response == 'y')
{ cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << endl;
cout << num1 << " + " << num2 << " = " << (num1+ num2)<< endl;
cout << "Would you like to add again: (Y/y) ";
cin >> response;
cout << endl;
}
return 0;
}
39