SlideShare une entreprise Scribd logo
1  sur  32
CMSC 104, Version 8/06 L15Switch.ppt
Power Point Presentation
In
Loop Case Statement
.. http://eglobiotraining.com.
CMSC 104, Version 8/06 L15Switch.ppt
The Source of Looping Statement
in
Programming
• #include <iostream>
•
• int main()
• {
• using namespace std;
•
• // nSelection must be declared outside do/while loop
• int nSelection;
•
• do
• {
• cout << "Please make a selection: " << endl;
• cout << "1) Addition" << endl;
• cout << "2) Subtraction" << endl;
• cout << "3) Multiplication" << endl;
• cout << "4) Division" << endl;
• cin >> nSelection;
• } while (nSelection != 1 && nSelection != 2 &&
• nSelection != 3 && nSelection != 4);
•
• // do something with nSelection here
• // such as a switch statement
•
• return 0;
• }
http://eglobiotraining.com.
CMSC 104, Version 8/06 L15Switch.ppt
The Screen Shot on Looping Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 L15Switch.ppt
The Explanation of Looping Statement
in
Programming
• The statement in a do while loop always executes at least once. After the
statement has been executed, the do while loop checks the condition. If the
condition is true, the CPU jumps back to the top of the do while loop and
executes it again.
• Here is an example of using a do while loop to display a menu to the user and
wait for the user to make a valid choice:
• One interesting thing about the above example is that the nSelection variable
must be declared outside of the do block. Think about it for a moment and see
if you can figure out why that is.
• If the nSelection variable is declared inside the do block, it will be destroyed
when the do block terminates, which happens before the while conditional is
executed. But we need the variable to use in the while conditional —
consequently, the nSelection variable must be declared outside the do block.
• Generally it is good form to use a do while loop instead of a while loop when
you intentionally want the loop to execute at least once, as it makes this
assumption explicit — however, it’s not that big of a deal either way.
http://eglobiotraining.com.
CMSC 104, Version 8/06 L15Switch.ppt
The Source of Looping Statement
in
Programming
#include <iostream>
using namespace std;
enum Logical {False, True};
Logical acceptable(int age, int score);
/* START OF MAIN PROGRAM */
int main()
{
int candidate_age, candidate_score;
cout << "Enter the candidate's age: ";
cin >> candidate_age; cout << "Enter the candidate's
score: ";
cin >> candidate_score;
if (acceptable(candidate_age, candidate_score))
cout << "This candidate passed the test.n";
else
cout << "This candidate failed the test.n";
return 0;
}
http://eglobiotraining.com.
CMSC 104, Version 8/06 L15Switch.ppt
The Screen Shot on Looping Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 L15Switch.ppt
The Explanation of Looping Statement
in
Programming
Indeed, we can now use the identifier "Logical" in exactly the same way as we use the
identifiers "int", "char", etc. In particular, we can write functions which return a value of type
"Logical". The Following example program takes a candidate's age and test score, and
reports whether the candidate has passed the test. It uses the following criteria: candidates
between 0 and 14 years old have a pass mark of 50%, 15 and 16 year olds have a pass
mark of 55%, over 16's have a pass mark of 60%:
Defining our own data types (even if for the moment they're just sub-types of "int") brings us
another step closer to object-oriented programming, in which complex types of data structure
(or classes ofobjects) can be defined, each with their associated libraries of operations.
http://eglobiotraining.com.
CMSC 104, Version 8/06 L15Switch.ppt
The Source of Looping Statement
in
Programming
• #include <iostream>
• using namespace std;
• void main ()
• {
• int x;
• do
• {
• cout << "Input the number:";
• cin >> x;
• cout << "The number is:" << x << "n";
• } while (x != 5);
• cout << "End of program";
• }
http://eglobiotraining.com.
CMSC 104, Version 8/06 L15Switch.ppt
The Screen Shot on Looping Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 1L15Switch.ppt
The Explanation of Looping Statement
in
Programming
In this example, the input number is 5 and the while
loop is executed starting from 5 until the number
reaches a value <0.Whilethe value of x>0 the
statement inside the while block prints the value of x.
When this is completed, the numbers 5, 4, 3, 2, 1
are
printed. After the value of x reaches less than zero,
the control passes to outside the while block and
EXFORSYS is printed.
http://eglobiotraining.com.
CMSC 104, Version 8/06 1L15Switch.ppt
The Source of Looping Statement
in
Programming
• #include <iostream>
• using namespace std;
• void main ()
• {
• int x;
• do
• {
• cout << "Input the number:";
• cin >> x;
• cout << "The number is:" << x << "n";
• } while (x != 5);
• cout << "End of program";
• }
http://eglobiotraining.com.
CMSC 104, Version 8/06 1L15Switch.ppt
The Screen Shot on Looping Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 1L15Switch.ppt
The Explanation of Looping Statement
in
Programming
The do while loop functionality is similar to
while loop. The main difference in do-while
loop is that the condition is checked after the
statement is executed. In do-while loop, even
if the condition is not satisfied the statement
block is executed once.
http://eglobiotraining.com.
CMSC 104, Version 8/06 1L15Switch.ppt
The Source of Looping Statement
in
Programming
• /* END OF MAIN PROGRAM */ /*
• FUNCTION TO EVALUATE IF TEST SCORE IS ACCEPTABLE */
• Logical acceptable(int age, int score)
• {
• if (age <= 14 && score >= 50)
• return True;
• else if (age <= 16 && score >= 55)
• return True;
• else if (score >= 60)
• return True;
• else return False;
• }
• /*END OF FUNCTION */
http://eglobiotraining.com.
CMSC 104, Version 8/06 1L15Switch.ppt
The Screen Shot on Looping Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 1L15Switch.ppt
The Explanation of Looping Statement
in
Programming
There is a third kind of "loop" statement in C++
called a "do ... while" loop. This differs from "for" and
"while" loops in that the statement(s) inside
the {} braces are always executed once, before the
repetition condition is even checked. "Do ... while“
loops are useful, for example, to ensure that the
program user's keyboard input is of the correct
format:
http://eglobiotraining.com.
CMSC 104, Version 8/06 1L15Switch.ppt
Power Point Presentation
In
Switch Case Statement
http://eglobiotraining.com.
CMSC 104, Version 8/06 1L15Switch.ppt
The Source Code of Switch Case Statement
in
Programming
#include <iostream>
using namespace std;
int main ()
{
char permit;
cout << "Are you sure you want to quit? (y/n) : ";
cin >> permit;
switch (permit)
{
case 'y' :
cout << "Hope to see you again!" << endl;
break;
case 'n' :
cout << "Welcome back!" < < endl;
break;
default:
cout << "What? I don't get it!" << endl;
}
return 0;
}
http://eglobiotraining.com.
CMSC 104, Version 8/06 1L15Switch.ppt
The Screen Shot of Switch Case Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 2L15Switch.ppt
The Explanation of Switch Case Statement
in
Programming
The program that we create should be readable. To increase
the readability of the program we should use tools that is
simple to read and understand. When possible use switch
statement rather than if else statement, as it can be more
readable than if else statement. But switch statement has
limitation. It can't replace if else completely but can be helpful
at certain situation. It can't do everything thing that if else
statement can do. For example, switch statement can take
only int or char datatype in c++. The following programs will
help you to understand the switch statement.
http://eglobiotraining.com.
CMSC 104, Version 8/06 2L15Switch.ppt
The Source Code of Switch Case Statement
in
Programming
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
const int CHEESE_PIZZA = 11;
const int SPINACH_PIZZA = 13;
const int CHICKEN_PIZZA = 14;
cout << " *********** MENU ***********" << endl;
cout << setw (9) << "ITEM" << setw (20) << "PRICE" << endl;
cout << " (1) Cheese Pizza" << setw (8) << "$"
<< CHEESE_PIZZA << endl;
cout << " (2) Spinach Pizza" << setw (7) << "$"
<< SPINACH_PIZZA << endl;
cout << " (3) Chicken Pizza" << setw (7) << "$"
<< CHICKEN_PIZZA << endl;
cout << endl;
cout << "What do you want? ";
int option;
cin >> option;
cout << "How many? ";
int quantity;
cin >> quantity;
int price;
switch (option)
{
case 1:
price = CHEESE_PIZZA;
break;
case 2:
price = SPINACH_PIZZA;
break;
case 3:
price = CHICKEN_PIZZA;
break;
default:
cout << "Please select valid item from menu. " <<
endl;
return 1;
}
int amount = price * quantity;
cout << "Your Bill: $ " << amount << endl;
return 0;
}
http://eglobiotraining.com.
CMSC 104, Version 8/06 2L15Switch.ppt
The Screen Shot of Switch Case Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 2L15Switch.ppt
The Explanation of Switch Case Statement
in
Programming
In the above program we take an integer value from the user which is
stored in 'option' variable. We pass this value to switch statement. The
switch statement has 3 cases: case 1, case 2 and case 3. The case 1: is
similar to if (option == 1). This is the advantage of switch statement over if
else statement. You don't need to type the name of variable again and
again if you are doing selection operation on same variable. You just put
the variable name on switch statement and then just specify the value
after 'case'. One more thing to be noted is that it requires 'break‘
statement at the end of each 'case'. If you remove the break statement
then it will jump to the case that follows it. Try it and check by yourself.
The 'default' is same as else in if else statement.
http://eglobiotraining.com.
CMSC 104, Version 8/06 2L15Switch.ppt
The Source Code of Switch Case Statement
in
Programming
#include <iostream>
using namespace std;
void playgame()
{
cout << "Play game called";
}
void loadgame()
{
cout << "Load game called";
}
void playmultiplayer()
{
cout << "Play multiplayer game called";
}
int main()
{ int input;
cout<<"1. Play gamen";
cout<<"2. Load gamen";
cout<<"3. Play multiplayern";
cout<<"4. Exitn";
cout<<"Selection: ";
cin>> input;
switch ( input )
{
case 1: // Note the colon, not a
semicolon
playgame(); break;
case 2: // Note the colon, not a
semicolon
loadgame(); break;
case 3: // Note the colon, not a
semicolon
playmultiplayer(); break;
case 4: // Note the colon, not a
semicolon cout<<"Thank you for playing!n";
break;
default: // Note the colon, not a semicolon
cout<<"Error, bad input, quittingn"; break;
}
cin.get();
}
http://eglobiotraining.com.
CMSC 104, Version 8/06 2L15Switch.ppt
The Screen Shot on Looping Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 2L15Switch.ppt
The Explanation of Switch Case Statement
in
Programming
This program will compile, but cannot be run until the
undefined functions are given bodies, but it serves as a
model (albeit simple) for processing input. If you do not
understand this then try mentally putting in if statements for
the case statements. Default simply skips out of the switch
case construction and allows the program to terminate
naturally. If you do not like that, then you can make a loop
around the whole thing to have it wait for valid input. You
could easily make a few small functions if you wish to test
the code.
http://eglobiotraining.com.
CMSC 104, Version 8/06 2L15Switch.ppt
The Source Code of Switch Case Statement
in
Programming
// custom countdown using while
#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0) {
cout << n << ", ";
--n;
}
cout << "FIRE!n";
return 0;
}
http://eglobiotraining.com.
CMSC 104, Version 8/06 2L15Switch.ppt
The Screen Shot of Switch Case Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 2L15Switch.ppt
The Explanation of Switch Case Statement
in
Programming
• When the program starts the user is prompted to insert a starting number for the countdown. Then the while loop begins, if the
value entered by the user fulfills the condition n>0 (that n is greater than zero) the block that follows the condition will be
executed and repeated while the condition (n>0) remains being true.
The whole process of the previous program can be interpreted according to the following script (beginning in main):
User assigns a value to n
• The while condition is checked (n>0). At this point there are two possibilities:
* condition is true: statement is executed (to step 3)
* condition is false: ignore statement and continue after it (to step 5)
• Execute statement:
cout << n << ", ";
--n;
(prints the value of n on the screen and decreases n by 1)
• End of block. Return automatically to step 2
• Continue the program right after the block: print FIRE! and end program.
•
When creating a while-loop, we must always consider that it has to end at some point, therefore we must provide within the block
some method to force the condition to become false at some point, otherwise the loop will continue looping forever. In this case
we have included --n; that decreases the value of the variable that is being evaluated in the condition (n) by one - this will
eventually make the condition (n>0) to become false after a certain number of loop iterations: to be more specific,
when n becomes 0, that is where our while-loop and our countdown end.
Of course this is such a simple action for our computer that the whole countdown is performed instantly without any practical
delay between numbers.
http://eglobiotraining.com.
CMSC 104, Version 8/06 3L15Switch.ppt
The Source Code of Switch Case Statement
in
Programming
#include <stdlib.h>
#include <stdio.h>
int main(void) {
int n;
printf("Please enter a number: ");
scanf("%d", &n);
switch (n) {
case 1: {
printf("n is equal to 1!n");
break;
}
case 2: {
printf("n is equal to 2!n");
break;
}
case 3: {
printf("n is equal to 3!n");
break;
} default: {
printf("n isn't equal to 1, 2, or 3.n");
break;
}
}
system("PAUSE");
return 0;
}
http://eglobiotraining.com.
CMSC 104, Version 8/06 3L15Switch.ppt
The Screen Shot of Switch Case Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 3L15Switch.ppt
The Explanation of Switch Case Statement
in
Programming
The switch statement is used in C++ for testing if a
variable is equal to one of a set of values. The variable
must be an integer, i.e. integral or non-fractional. The
programmer can specify the actions taken for each
case of the possible values the variable can have. The
same operation can be performed by using a series of
if, else if, and else statements as was demonstrated
in Lesson 03: Else If Statements. Therefore, let's look
at an example using the switch statement that mimics
the functionality of the Else If Example from Lesson
03.
http://eglobiotraining.com.

Contenu connexe

Tendances

Unit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDUnit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDPaweł Michalik
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitMichelangelo van Dam
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
 
Checking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-StudioChecking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-StudioAndrey Karpov
 
Fundamentals of programming angeli
Fundamentals of programming angeliFundamentals of programming angeli
Fundamentals of programming angelibergonio11339481
 
Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programmingtrish_maxine
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitMichelangelo van Dam
 
Why Windows 8 drivers are buggy
Why Windows 8 drivers are buggyWhy Windows 8 drivers are buggy
Why Windows 8 drivers are buggyAndrey Karpov
 
Firefox Easily Analyzed by PVS-Studio Standalone
Firefox Easily Analyzed by PVS-Studio StandaloneFirefox Easily Analyzed by PVS-Studio Standalone
Firefox Easily Analyzed by PVS-Studio StandaloneAndrey Karpov
 
Date Processing Attracts Bugs or 77 Defects in Qt 6
Date Processing Attracts Bugs or 77 Defects in Qt 6Date Processing Attracts Bugs or 77 Defects in Qt 6
Date Processing Attracts Bugs or 77 Defects in Qt 6Andrey Karpov
 
Analyzing FreeCAD's Source Code and Its "Sick" Dependencies
Analyzing FreeCAD's Source Code and Its "Sick" DependenciesAnalyzing FreeCAD's Source Code and Its "Sick" Dependencies
Analyzing FreeCAD's Source Code and Its "Sick" DependenciesPVS-Studio
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project AnalyzedPVS-Studio
 
Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?PVS-Studio
 
Source code of WPF samples by Microsoft was checked
Source code of WPF samples by Microsoft was checkedSource code of WPF samples by Microsoft was checked
Source code of WPF samples by Microsoft was checkedPVS-Studio
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentationnicobn
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)jewelyngrace
 
Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...
Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...
Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...AuditMark
 
Why Windows 8 drivers are buggy
Why Windows 8 drivers are buggyWhy Windows 8 drivers are buggy
Why Windows 8 drivers are buggyPVS-Studio
 
Going On with the Check of Geant4
Going On with the Check of Geant4Going On with the Check of Geant4
Going On with the Check of Geant4Andrey Karpov
 

Tendances (20)

Unit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDUnit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDD
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
PHPUnit testing to Zend_Test
PHPUnit testing to Zend_TestPHPUnit testing to Zend_Test
PHPUnit testing to Zend_Test
 
Checking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-StudioChecking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-Studio
 
Fundamentals of programming angeli
Fundamentals of programming angeliFundamentals of programming angeli
Fundamentals of programming angeli
 
Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programming
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnit
 
Why Windows 8 drivers are buggy
Why Windows 8 drivers are buggyWhy Windows 8 drivers are buggy
Why Windows 8 drivers are buggy
 
Firefox Easily Analyzed by PVS-Studio Standalone
Firefox Easily Analyzed by PVS-Studio StandaloneFirefox Easily Analyzed by PVS-Studio Standalone
Firefox Easily Analyzed by PVS-Studio Standalone
 
Date Processing Attracts Bugs or 77 Defects in Qt 6
Date Processing Attracts Bugs or 77 Defects in Qt 6Date Processing Attracts Bugs or 77 Defects in Qt 6
Date Processing Attracts Bugs or 77 Defects in Qt 6
 
Analyzing FreeCAD's Source Code and Its "Sick" Dependencies
Analyzing FreeCAD's Source Code and Its "Sick" DependenciesAnalyzing FreeCAD's Source Code and Its "Sick" Dependencies
Analyzing FreeCAD's Source Code and Its "Sick" Dependencies
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project Analyzed
 
Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?
 
Source code of WPF samples by Microsoft was checked
Source code of WPF samples by Microsoft was checkedSource code of WPF samples by Microsoft was checked
Source code of WPF samples by Microsoft was checked
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...
Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...
Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...
 
Why Windows 8 drivers are buggy
Why Windows 8 drivers are buggyWhy Windows 8 drivers are buggy
Why Windows 8 drivers are buggy
 
Going On with the Check of Geant4
Going On with the Check of Geant4Going On with the Check of Geant4
Going On with the Check of Geant4
 

En vedette

Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structureJd Mercado
 
5 S Program Orientation Powerpoint Presentation
5 S Program Orientation Powerpoint Presentation5 S Program Orientation Powerpoint Presentation
5 S Program Orientation Powerpoint PresentationCobra143
 
How To Prepare A Basic Training Module
How To Prepare A Basic Training ModuleHow To Prepare A Basic Training Module
How To Prepare A Basic Training ModuleBruhad Buch
 

En vedette (9)

Hammerites
HammeritesHammerites
Hammerites
 
Dt table 11
Dt table 11Dt table 11
Dt table 11
 
Dt table 11
Dt table 11Dt table 11
Dt table 11
 
Dt table 11
Dt table 11Dt table 11
Dt table 11
 
Dt table 11
Dt table 11Dt table 11
Dt table 11
 
The Loops
The LoopsThe Loops
The Loops
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
 
5 S Program Orientation Powerpoint Presentation
5 S Program Orientation Powerpoint Presentation5 S Program Orientation Powerpoint Presentation
5 S Program Orientation Powerpoint Presentation
 
How To Prepare A Basic Training Module
How To Prepare A Basic Training ModuleHow To Prepare A Basic Training Module
How To Prepare A Basic Training Module
 

Similaire à neiljaysonching

Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping newaprilyyy
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.gerrell
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)aeden_brines
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!olracoatalub
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 
Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinarurumedina
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdfVcTrn1
 
Fundamentals of programming finals.ajang
Fundamentals of programming finals.ajangFundamentals of programming finals.ajang
Fundamentals of programming finals.ajangJaricka Angelyd Marquez
 
Capgemini oracle live-sql
Capgemini oracle live-sqlCapgemini oracle live-sql
Capgemini oracle live-sqlJohan Louwers
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++cpjcollege
 

Similaire à neiljaysonching (20)

C++ programming
C++ programmingC++ programming
C++ programming
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping new
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
 
Switch case and looping jam
Switch case and looping jamSwitch case and looping jam
Switch case and looping jam
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
My final requirement
My final requirementMy final requirement
My final requirement
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
 
Final requirement
Final requirementFinal requirement
Final requirement
 
Survelaine murillo ppt
Survelaine murillo pptSurvelaine murillo ppt
Survelaine murillo ppt
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medina
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdf
 
Fundamentals of programming finals.ajang
Fundamentals of programming finals.ajangFundamentals of programming finals.ajang
Fundamentals of programming finals.ajang
 
Capgemini oracle live-sql
Capgemini oracle live-sqlCapgemini oracle live-sql
Capgemini oracle live-sql
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
 

neiljaysonching

  • 1. CMSC 104, Version 8/06 L15Switch.ppt Power Point Presentation In Loop Case Statement .. http://eglobiotraining.com.
  • 2. CMSC 104, Version 8/06 L15Switch.ppt The Source of Looping Statement in Programming • #include <iostream> • • int main() • { • using namespace std; • • // nSelection must be declared outside do/while loop • int nSelection; • • do • { • cout << "Please make a selection: " << endl; • cout << "1) Addition" << endl; • cout << "2) Subtraction" << endl; • cout << "3) Multiplication" << endl; • cout << "4) Division" << endl; • cin >> nSelection; • } while (nSelection != 1 && nSelection != 2 && • nSelection != 3 && nSelection != 4); • • // do something with nSelection here • // such as a switch statement • • return 0; • } http://eglobiotraining.com.
  • 3. CMSC 104, Version 8/06 L15Switch.ppt The Screen Shot on Looping Statement in Programming http://eglobiotraining.com.
  • 4. CMSC 104, Version 8/06 L15Switch.ppt The Explanation of Looping Statement in Programming • The statement in a do while loop always executes at least once. After the statement has been executed, the do while loop checks the condition. If the condition is true, the CPU jumps back to the top of the do while loop and executes it again. • Here is an example of using a do while loop to display a menu to the user and wait for the user to make a valid choice: • One interesting thing about the above example is that the nSelection variable must be declared outside of the do block. Think about it for a moment and see if you can figure out why that is. • If the nSelection variable is declared inside the do block, it will be destroyed when the do block terminates, which happens before the while conditional is executed. But we need the variable to use in the while conditional — consequently, the nSelection variable must be declared outside the do block. • Generally it is good form to use a do while loop instead of a while loop when you intentionally want the loop to execute at least once, as it makes this assumption explicit — however, it’s not that big of a deal either way. http://eglobiotraining.com.
  • 5. CMSC 104, Version 8/06 L15Switch.ppt The Source of Looping Statement in Programming #include <iostream> using namespace std; enum Logical {False, True}; Logical acceptable(int age, int score); /* START OF MAIN PROGRAM */ int main() { int candidate_age, candidate_score; cout << "Enter the candidate's age: "; cin >> candidate_age; cout << "Enter the candidate's score: "; cin >> candidate_score; if (acceptable(candidate_age, candidate_score)) cout << "This candidate passed the test.n"; else cout << "This candidate failed the test.n"; return 0; } http://eglobiotraining.com.
  • 6. CMSC 104, Version 8/06 L15Switch.ppt The Screen Shot on Looping Statement in Programming http://eglobiotraining.com.
  • 7. CMSC 104, Version 8/06 L15Switch.ppt The Explanation of Looping Statement in Programming Indeed, we can now use the identifier "Logical" in exactly the same way as we use the identifiers "int", "char", etc. In particular, we can write functions which return a value of type "Logical". The Following example program takes a candidate's age and test score, and reports whether the candidate has passed the test. It uses the following criteria: candidates between 0 and 14 years old have a pass mark of 50%, 15 and 16 year olds have a pass mark of 55%, over 16's have a pass mark of 60%: Defining our own data types (even if for the moment they're just sub-types of "int") brings us another step closer to object-oriented programming, in which complex types of data structure (or classes ofobjects) can be defined, each with their associated libraries of operations. http://eglobiotraining.com.
  • 8. CMSC 104, Version 8/06 L15Switch.ppt The Source of Looping Statement in Programming • #include <iostream> • using namespace std; • void main () • { • int x; • do • { • cout << "Input the number:"; • cin >> x; • cout << "The number is:" << x << "n"; • } while (x != 5); • cout << "End of program"; • } http://eglobiotraining.com.
  • 9. CMSC 104, Version 8/06 L15Switch.ppt The Screen Shot on Looping Statement in Programming http://eglobiotraining.com.
  • 10. CMSC 104, Version 8/06 1L15Switch.ppt The Explanation of Looping Statement in Programming In this example, the input number is 5 and the while loop is executed starting from 5 until the number reaches a value <0.Whilethe value of x>0 the statement inside the while block prints the value of x. When this is completed, the numbers 5, 4, 3, 2, 1 are printed. After the value of x reaches less than zero, the control passes to outside the while block and EXFORSYS is printed. http://eglobiotraining.com.
  • 11. CMSC 104, Version 8/06 1L15Switch.ppt The Source of Looping Statement in Programming • #include <iostream> • using namespace std; • void main () • { • int x; • do • { • cout << "Input the number:"; • cin >> x; • cout << "The number is:" << x << "n"; • } while (x != 5); • cout << "End of program"; • } http://eglobiotraining.com.
  • 12. CMSC 104, Version 8/06 1L15Switch.ppt The Screen Shot on Looping Statement in Programming http://eglobiotraining.com.
  • 13. CMSC 104, Version 8/06 1L15Switch.ppt The Explanation of Looping Statement in Programming The do while loop functionality is similar to while loop. The main difference in do-while loop is that the condition is checked after the statement is executed. In do-while loop, even if the condition is not satisfied the statement block is executed once. http://eglobiotraining.com.
  • 14. CMSC 104, Version 8/06 1L15Switch.ppt The Source of Looping Statement in Programming • /* END OF MAIN PROGRAM */ /* • FUNCTION TO EVALUATE IF TEST SCORE IS ACCEPTABLE */ • Logical acceptable(int age, int score) • { • if (age <= 14 && score >= 50) • return True; • else if (age <= 16 && score >= 55) • return True; • else if (score >= 60) • return True; • else return False; • } • /*END OF FUNCTION */ http://eglobiotraining.com.
  • 15. CMSC 104, Version 8/06 1L15Switch.ppt The Screen Shot on Looping Statement in Programming http://eglobiotraining.com.
  • 16. CMSC 104, Version 8/06 1L15Switch.ppt The Explanation of Looping Statement in Programming There is a third kind of "loop" statement in C++ called a "do ... while" loop. This differs from "for" and "while" loops in that the statement(s) inside the {} braces are always executed once, before the repetition condition is even checked. "Do ... while“ loops are useful, for example, to ensure that the program user's keyboard input is of the correct format: http://eglobiotraining.com.
  • 17. CMSC 104, Version 8/06 1L15Switch.ppt Power Point Presentation In Switch Case Statement http://eglobiotraining.com.
  • 18. CMSC 104, Version 8/06 1L15Switch.ppt The Source Code of Switch Case Statement in Programming #include <iostream> using namespace std; int main () { char permit; cout << "Are you sure you want to quit? (y/n) : "; cin >> permit; switch (permit) { case 'y' : cout << "Hope to see you again!" << endl; break; case 'n' : cout << "Welcome back!" < < endl; break; default: cout << "What? I don't get it!" << endl; } return 0; } http://eglobiotraining.com.
  • 19. CMSC 104, Version 8/06 1L15Switch.ppt The Screen Shot of Switch Case Statement in Programming http://eglobiotraining.com.
  • 20. CMSC 104, Version 8/06 2L15Switch.ppt The Explanation of Switch Case Statement in Programming The program that we create should be readable. To increase the readability of the program we should use tools that is simple to read and understand. When possible use switch statement rather than if else statement, as it can be more readable than if else statement. But switch statement has limitation. It can't replace if else completely but can be helpful at certain situation. It can't do everything thing that if else statement can do. For example, switch statement can take only int or char datatype in c++. The following programs will help you to understand the switch statement. http://eglobiotraining.com.
  • 21. CMSC 104, Version 8/06 2L15Switch.ppt The Source Code of Switch Case Statement in Programming #include <iostream> #include <iomanip> using namespace std; int main () { const int CHEESE_PIZZA = 11; const int SPINACH_PIZZA = 13; const int CHICKEN_PIZZA = 14; cout << " *********** MENU ***********" << endl; cout << setw (9) << "ITEM" << setw (20) << "PRICE" << endl; cout << " (1) Cheese Pizza" << setw (8) << "$" << CHEESE_PIZZA << endl; cout << " (2) Spinach Pizza" << setw (7) << "$" << SPINACH_PIZZA << endl; cout << " (3) Chicken Pizza" << setw (7) << "$" << CHICKEN_PIZZA << endl; cout << endl; cout << "What do you want? "; int option; cin >> option; cout << "How many? "; int quantity; cin >> quantity; int price; switch (option) { case 1: price = CHEESE_PIZZA; break; case 2: price = SPINACH_PIZZA; break; case 3: price = CHICKEN_PIZZA; break; default: cout << "Please select valid item from menu. " << endl; return 1; } int amount = price * quantity; cout << "Your Bill: $ " << amount << endl; return 0; } http://eglobiotraining.com.
  • 22. CMSC 104, Version 8/06 2L15Switch.ppt The Screen Shot of Switch Case Statement in Programming http://eglobiotraining.com.
  • 23. CMSC 104, Version 8/06 2L15Switch.ppt The Explanation of Switch Case Statement in Programming In the above program we take an integer value from the user which is stored in 'option' variable. We pass this value to switch statement. The switch statement has 3 cases: case 1, case 2 and case 3. The case 1: is similar to if (option == 1). This is the advantage of switch statement over if else statement. You don't need to type the name of variable again and again if you are doing selection operation on same variable. You just put the variable name on switch statement and then just specify the value after 'case'. One more thing to be noted is that it requires 'break‘ statement at the end of each 'case'. If you remove the break statement then it will jump to the case that follows it. Try it and check by yourself. The 'default' is same as else in if else statement. http://eglobiotraining.com.
  • 24. CMSC 104, Version 8/06 2L15Switch.ppt The Source Code of Switch Case Statement in Programming #include <iostream> using namespace std; void playgame() { cout << "Play game called"; } void loadgame() { cout << "Load game called"; } void playmultiplayer() { cout << "Play multiplayer game called"; } int main() { int input; cout<<"1. Play gamen"; cout<<"2. Load gamen"; cout<<"3. Play multiplayern"; cout<<"4. Exitn"; cout<<"Selection: "; cin>> input; switch ( input ) { case 1: // Note the colon, not a semicolon playgame(); break; case 2: // Note the colon, not a semicolon loadgame(); break; case 3: // Note the colon, not a semicolon playmultiplayer(); break; case 4: // Note the colon, not a semicolon cout<<"Thank you for playing!n"; break; default: // Note the colon, not a semicolon cout<<"Error, bad input, quittingn"; break; } cin.get(); } http://eglobiotraining.com.
  • 25. CMSC 104, Version 8/06 2L15Switch.ppt The Screen Shot on Looping Statement in Programming http://eglobiotraining.com.
  • 26. CMSC 104, Version 8/06 2L15Switch.ppt The Explanation of Switch Case Statement in Programming This program will compile, but cannot be run until the undefined functions are given bodies, but it serves as a model (albeit simple) for processing input. If you do not understand this then try mentally putting in if statements for the case statements. Default simply skips out of the switch case construction and allows the program to terminate naturally. If you do not like that, then you can make a loop around the whole thing to have it wait for valid input. You could easily make a few small functions if you wish to test the code. http://eglobiotraining.com.
  • 27. CMSC 104, Version 8/06 2L15Switch.ppt The Source Code of Switch Case Statement in Programming // custom countdown using while #include <iostream> using namespace std; int main () { int n; cout << "Enter the starting number > "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!n"; return 0; } http://eglobiotraining.com.
  • 28. CMSC 104, Version 8/06 2L15Switch.ppt The Screen Shot of Switch Case Statement in Programming http://eglobiotraining.com.
  • 29. CMSC 104, Version 8/06 2L15Switch.ppt The Explanation of Switch Case Statement in Programming • When the program starts the user is prompted to insert a starting number for the countdown. Then the while loop begins, if the value entered by the user fulfills the condition n>0 (that n is greater than zero) the block that follows the condition will be executed and repeated while the condition (n>0) remains being true. The whole process of the previous program can be interpreted according to the following script (beginning in main): User assigns a value to n • The while condition is checked (n>0). At this point there are two possibilities: * condition is true: statement is executed (to step 3) * condition is false: ignore statement and continue after it (to step 5) • Execute statement: cout << n << ", "; --n; (prints the value of n on the screen and decreases n by 1) • End of block. Return automatically to step 2 • Continue the program right after the block: print FIRE! and end program. • When creating a while-loop, we must always consider that it has to end at some point, therefore we must provide within the block some method to force the condition to become false at some point, otherwise the loop will continue looping forever. In this case we have included --n; that decreases the value of the variable that is being evaluated in the condition (n) by one - this will eventually make the condition (n>0) to become false after a certain number of loop iterations: to be more specific, when n becomes 0, that is where our while-loop and our countdown end. Of course this is such a simple action for our computer that the whole countdown is performed instantly without any practical delay between numbers. http://eglobiotraining.com.
  • 30. CMSC 104, Version 8/06 3L15Switch.ppt The Source Code of Switch Case Statement in Programming #include <stdlib.h> #include <stdio.h> int main(void) { int n; printf("Please enter a number: "); scanf("%d", &n); switch (n) { case 1: { printf("n is equal to 1!n"); break; } case 2: { printf("n is equal to 2!n"); break; } case 3: { printf("n is equal to 3!n"); break; } default: { printf("n isn't equal to 1, 2, or 3.n"); break; } } system("PAUSE"); return 0; } http://eglobiotraining.com.
  • 31. CMSC 104, Version 8/06 3L15Switch.ppt The Screen Shot of Switch Case Statement in Programming http://eglobiotraining.com.
  • 32. CMSC 104, Version 8/06 3L15Switch.ppt The Explanation of Switch Case Statement in Programming The switch statement is used in C++ for testing if a variable is equal to one of a set of values. The variable must be an integer, i.e. integral or non-fractional. The programmer can specify the actions taken for each case of the possible values the variable can have. The same operation can be performed by using a series of if, else if, and else statements as was demonstrated in Lesson 03: Else If Statements. Therefore, let's look at an example using the switch statement that mimics the functionality of the Else If Example from Lesson 03. http://eglobiotraining.com.