1. FUNDAMENTALS OF PROGRAMMING
Final Examination
Submitted to:
Prof. Erwin Globio
Submitted by:
Janine Rachel F. Ramirez
2011367621
For more on C++ Programs go to:
http://eglobiotraining.com
2. SWITCH CASE
The switch statement body consists of a series
of case labels and an optional default label. No two
http://eglobiotraining.com
For more on C++ Programs go to:
constant expressions in case statements can
evaluate to the same value. The default label can
appear only once. The labeled statements are not
syntactic requirements, but the switch statement is
meaningless without them. The default statement
need not come at the end; it can appear anywhere
in the body of the switch statement. A case or
default label can only appear inside a switch
statement.
3. C++ PROGRAMMING: SWITCH CASE
EXAMPLE 1
char ticket;
cout<<"Please choose your ticket:n"<<endl<<"Ticket [A] - Manila to Intramuros."<<endl
<<"Ticket [B] - Manila to City Hall."<<endl<<"Ticket [C] - Manila to Mall of Asia.n"<<endl;
http://eglobiotraining.com
For more on C++ Programs go to:
cin>>ticket;
switch (ticket)
{
case 'a':
cout<<"nThe price of Ticket [A] is P 15.00. Please proceed to the cashier.n"<<endl;
break;
case 'b':
cout<<"nThe price of Ticket [B] is P 12.00. Please proceed to the cashier.n"<<endl;
break;
case 'c':
cout<<"nThe price of Ticket [C] is P 25.00. Please proceed to the cashier.n"<<endl;
break;
default:
cout<<"You must choose a ticket to proceed."<<endl;
}
4. For more on C++ Programs go to:
http://eglobiotraining.com
SWITCH CASE EXAMPLE 1 OUTPUT
5. C++ PROGRAMMING: SWITCH CASE
EXAMPLE 2
char name;
cout<<"Please choose a horse to bet on: Abe, Chuck or Steve."<<endl
<<"Please enter a for Abe, b for Chuck and c for Steve.n"<<endl;
http://eglobiotraining.com
For more on C++ Programs go to:
cin>>name;
switch (name)
{
case 'a':
cout<<"nYou choose Abe. He has a 45% chance of winning.n"<<endl;
break;
case 'b':
cout<<"nYou choose Chuck. He has a 50% chance of winning.n"<<endl;
break;
case 'c':
cout<<"nYou choose Steve. He has a 75% chance of winnng. n"<<endl;
break;
default:
cout<<"You must choose a horse to proceed."<<endl;
}
6. For more on C++ Programs go to:
http://eglobiotraining.com
SWITCH CASE EXAMPLE 2 OUTPUT
7. C++ PROGRAMMING: SWITCH CASE EXAMPLE 3
int equation;
int x, y, z;
cout<<"Please choose which arithmetic operation to use. 1 for
addition, 2 for subtraction, 3 for multiplication or 4 for division.n"<<endl;
cin>>equation;
http://eglobiotraining.com
For more on C++ Programs go to:
switch (equation)
{
case 1:
cout<<"nPlease enter two integers: "<<endl;
cin>>x>>y;
z=x+y;
cout<<"nTheir sum is: "<<z<<endl<<endl;
break;
case 2:
cout<<"nPlease enter two integers: "<<endl;
cin>>x>>y;
z=x-y;
cout<<"Their difference is: "<<z<<endl<<endl;
break;
8. SWITCH CASE EXAMPLE 3 CONTINUATION
case 3:
cout<<"nPlease enter two integers: "<<endl;
cin>>x>>y;
z=x*y;
cout<<"Their product is: "<<z<<endl<<endl;
http://eglobiotraining.com
For more on C++ Programs go to:
break;
case 4:
cout<<"nPlease enter two integers: "<<endl;
cin>>x>>y;
z=x/y;
cout<<"Their quotient is: "<<z<<endl<<endl;
break;
default:
cout<<"You must choose an operation to
proceed."<<endl;
}
9. For more on C++ Programs go to:
http://eglobiotraining.com
SWITCH CASE EXAMPLE 3 OUTPUT
10. C++ PROGRAMMING: SWITCH CASE
EXAMPLE 4
int food;
cout<<"Please choose your dessert.n"<<endl<<"Menu:n"<<"[1] Chocolate Caken"
<<"[2] Strawberry Caken"<<"[3] Mango Caken"<<endl;
http://eglobiotraining.com
For more on C++ Programs go to:
cin>>food;
switch (food)
{
case 1:
cout<<"nThe Chocolate Cake is P210.00. Please proceed to the cashier.n"<<endl;
break;
case 2:
cout<<"nThe Strawberry Case is P150.00. Please proceed to the cashier.n"<<endl;
break;
case 3:
cout<<"nThe Mango Cake is P110.00. Please proceed to the cashier.n"<<endl;
break;
default:
cout<<"Please choose a cake!n"<<endl;
}
11. For more on C++ Programs go to:
http://eglobiotraining.com
SWITCH CASE EXAMPLE 4 OUTPUT
12. C++ PROGRAMMING: SWITCH CASE
EXAMPLE 5
int dinner;
cout<<"Please choose your dinner.n"<<endl<<"Menu:n"<<"[1] Sinigang na baboyn"
<<"[2] Beef Steakn"<<"[3] Chicken Curryn"<<endl;
http://eglobiotraining.com
For more on C++ Programs go to:
cin>>dinner;
switch (dinner)
{
case 1:
cout<<"nThe Sinigang na baboy is P120.00. Please proceed to the cashier.n"<<endl;
break;
case 2:
cout<<"nThe Beef Steak is P150.00. Please proceed to the counter.n"<<endl;
break;
case 3:
cout<<"nThe Chicken Curry is P110.00. Please proceed to the counter.n"<<endl;
break;
default:
cout<<"Please choose a cake!n"<<endl;
}
13. For more on C++ Programs go to:
http://eglobiotraining.com
SWITCH CASE EXAMPLE 5 OUTPUT
14. LOOPING STATEMENT
a loop is a way of repeating a statement a number
of times until some way of ending the loop occurs. It
http://eglobiotraining.com
For more on C++ Programs go to:
might be run for a preset number of times, typically
in a for loop, repeated as long as an expression is
true (a while loop) or repeated until an expression
becomes false in a do while loop.
15. C++ PROGRAMMING: LOOPING STATEMENT
EXAMPLE 1: FOR
int main()
{
http://eglobiotraining.com
For more on C++ Programs go to:
int width, space, alignleft, count=16;
for(width=1;width<=8;width++)
{
for (alignleft=width ; alignleft>=0 ; alignleft--)
{
cout<<" ";
}
for (space=1 ; space<count ; space++)
{
cout<<"*";
}
cout<<endl;
count=count-2;
}
16. For more on C++ Programs go to:
http://eglobiotraining.com
LOOPING STATEMENT EXAMPLE 1 OUTPUT
17. C++ PROGRAMMING: LOOPING STATEMENT
EXAMPLE 2: FOR
int x;
http://eglobiotraining.com
For more on C++ Programs go to:
cout<<"Please enter an integer and I will countdown
beginning from the"
<<endl<<"integer you input.n"<<endl;
cin>>x;
cout<<endl;
for (x=16; x>-1; x--)
{
cout<<x<<", ";
}
cout<<"nThis concludes my countdown.n"<<endl;
18. For more on C++ Programs go to:
http://eglobiotraining.com
LOOPING STATEMENT EXAMPLE 2 OUTPUT
19. C++ PROGRAMMING: LOOPING STATEMENT
EXAMPLE 3: WHILE
int x;
cout<<"Please enter an integer and I will countdown
http://eglobiotraining.com
For more on C++ Programs go to:
beginning from the"
<<endl<<"integer you input.n"<<endl;
cin>>x;
cout<<endl;
while (x>0){
cout<<x<<", ";
x--;
}
cout<<endl<<"nThis concludes my countdown.n"<<endl;
20. For more on C++ Programs go to:
http://eglobiotraining.com
LOOPING STATEMENT EXAMPLE 3 OUTPUT
21. C++ PROGRAMMING: LOOPING STATEMENT
EXAMPLE 4: WHILE
int x=4, y=3;
http://eglobiotraining.com
For more on C++ Programs go to:
while (x>0){
cout<<"This sentence is repeated 4
times."<<endl;
x--;
}
while (y>0){
cout<<"While this sentence is repeated only
3 times."<<endl;
y--;
}
22. For more on C++ Programs go to:
http://eglobiotraining.com
LOOPING STATEMENT EXAMPLE 4 OUTPUT
23. C++ PROGRAMMING: LOOPING STATEMENT
EXAMPLE 5: DO WHILE
int x, y, z;
char answer;
http://eglobiotraining.com
For more on C++ Programs go to:
do
{
cout<<"Please enter two integers and I will add them."<<endl;
cin>>x>>y;
z=x+y;
cout<<"nThe sum of "<<x<<" and "<<y<<" is "<<z<<endl;
cout<<"nWould you like me to add another two integers? Please answer with y
for yes and n for no."<<endl;
cin>>answer;
cout<<endl;
}
while (answer=='y');
cout<<"nGood bye!"<<endl;
24. For more on C++ Programs go to:
http://eglobiotraining.com
LOOPING STATEMENT EXAMPLE 5 OUTPUT
25. http://eglobiotraining.com
For more on C++ Programs go to:
YOU CAN FIND THIS PRESENTATION AT MY
SLIDESHARE ACCOUNT AT:
http://www.slideshare.net/janine_ramirez16
26. This Power Point presentation is created by:
Janine Rachel F. Ramirez
The Power Point presentation is to be submitted to:
http://eglobiotraining.com
For more on C++ Programs go to:
Prof. Erwin Globio
The Official Website is:
http://eglobiotraining.com/