SlideShare a Scribd company logo
1 of 32
CMSC 104, Version 8/06 1
L15Switch.ppt
Power Point Presentation
In
Loop Case Statement
.
. http://eglobiotraining.com.
CMSC 104, Version 8/06 2
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 3
L15Switch.ppt
The Screen Shot on Looping Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 4
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 5
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 6
L15Switch.ppt
The Screen Shot on Looping Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 7
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 8
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 9
L15Switch.ppt
The Screen Shot on Looping Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 10
L15Switch.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 11
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 12
L15Switch.ppt
The Screen Shot on Looping Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 13
L15Switch.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 14
L15Switch.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 15
L15Switch.ppt
The Screen Shot on Looping Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 16
L15Switch.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 17
L15Switch.ppt
Power Point Presentation
In
Switch Case Statement
http://eglobiotraining.com.
CMSC 104, Version 8/06 18
L15Switch.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 19
L15Switch.ppt
The Screen Shot of Switch Case Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 20
L15Switch.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 21
L15Switch.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 22
L15Switch.ppt
The Screen Shot of Switch Case Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 23
L15Switch.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 24
L15Switch.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 25
L15Switch.ppt
The Screen Shot on Looping Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 26
L15Switch.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 27
L15Switch.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 28
L15Switch.ppt
The Screen Shot of Switch Case Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 29
L15Switch.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 30
L15Switch.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 31
L15Switch.ppt
The Screen Shot of Switch Case Statement
in
Programming
http://eglobiotraining.com.
CMSC 104, Version 8/06 32
L15Switch.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.

More Related Content

What's hot

Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programmingtrish_maxine
 
JavaScript Control Statements II
JavaScript Control Statements IIJavaScript Control Statements II
JavaScript Control Statements IIReem Alattas
 
Checking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-StudioChecking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-StudioAndrey Karpov
 
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
 
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
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project AnalyzedPVS-Studio
 
4. programing 101
4. programing 1014. programing 101
4. programing 101IEEE MIU SB
 
Why Windows 8 drivers are buggy
Why Windows 8 drivers are buggyWhy Windows 8 drivers are buggy
Why Windows 8 drivers are buggyAndrey 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
 
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...Andrey Karpov
 
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...PVS-Studio
 
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
 
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
 
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
 
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
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentationnicobn
 
Analyzing the Blender project with PVS-Studio
Analyzing the Blender project with PVS-StudioAnalyzing the Blender project with PVS-Studio
Analyzing the Blender project with PVS-StudioPVS-Studio
 
Linux version of PVS-Studio couldn't help checking CodeLite
Linux version of PVS-Studio couldn't help checking CodeLiteLinux version of PVS-Studio couldn't help checking CodeLite
Linux version of PVS-Studio couldn't help checking CodeLitePVS-Studio
 
Python and Ruby implementations compared by the error density
Python and Ruby implementations compared by the error densityPython and Ruby implementations compared by the error density
Python and Ruby implementations compared by the error densityPVS-Studio
 

What's hot (20)

Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programming
 
JavaScript Control Statements II
JavaScript Control Statements IIJavaScript Control Statements II
JavaScript Control Statements II
 
Checking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-StudioChecking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-Studio
 
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
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project Analyzed
 
4. programing 101
4. programing 1014. programing 101
4. programing 101
 
Why Windows 8 drivers are buggy
Why Windows 8 drivers are buggyWhy Windows 8 drivers are buggy
Why Windows 8 drivers are buggy
 
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
 
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
 
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnit
 
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
 
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
 
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?
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
 
Analyzing the Blender project with PVS-Studio
Analyzing the Blender project with PVS-StudioAnalyzing the Blender project with PVS-Studio
Analyzing the Blender project with PVS-Studio
 
Linux version of PVS-Studio couldn't help checking CodeLite
Linux version of PVS-Studio couldn't help checking CodeLiteLinux version of PVS-Studio couldn't help checking CodeLite
Linux version of PVS-Studio couldn't help checking CodeLite
 
Python and Ruby implementations compared by the error density
Python and Ruby implementations compared by the error densityPython and Ruby implementations compared by the error density
Python and Ruby implementations compared by the error density
 

Viewers also liked

Audiovisuales cartel de hombre
Audiovisuales cartel de hombreAudiovisuales cartel de hombre
Audiovisuales cartel de hombreivanblazquezz
 
Chap 7 1_temperament_test
Chap 7 1_temperament_testChap 7 1_temperament_test
Chap 7 1_temperament_testAl Alcantara
 
Cultura audioviual trabajo del color
Cultura audioviual trabajo del colorCultura audioviual trabajo del color
Cultura audioviual trabajo del colorivanblazquezz
 
Configuration cellvizio100 2012
Configuration cellvizio100 2012Configuration cellvizio100 2012
Configuration cellvizio100 2012kurdi87
 
Audiovisuales cartel de mujer
Audiovisuales cartel de mujerAudiovisuales cartel de mujer
Audiovisuales cartel de mujerivanblazquezz
 
Animals motion
Animals motionAnimals motion
Animals motionjaslaycano
 
Tarea 12 de cultura audiovisual
Tarea 12 de cultura audiovisualTarea 12 de cultura audiovisual
Tarea 12 de cultura audiovisualivanblazquezz
 
Animals motion
Animals motionAnimals motion
Animals motionjaslaycano
 
Cultura audiovisual trabajo de texturas
Cultura audiovisual trabajo de texturasCultura audiovisual trabajo de texturas
Cultura audiovisual trabajo de texturasivanblazquezz
 
I+will+sing+forever
I+will+sing+foreverI+will+sing+forever
I+will+sing+foreverAl Alcantara
 
Ordföljd i huvudsats och bisats
Ordföljd i huvudsats och bisatsOrdföljd i huvudsats och bisats
Ordföljd i huvudsats och bisatsLärare Åsa Häll
 
[BTL] Cảm biến nhiệt độ
[BTL] Cảm biến nhiệt độ[BTL] Cảm biến nhiệt độ
[BTL] Cảm biến nhiệt độHoàng Phạm
 
Biomass mngt & utilization
Biomass mngt & utilizationBiomass mngt & utilization
Biomass mngt & utilizationAl Alcantara
 
[BTL] Cảm biến đo độ ẩm
[BTL] Cảm biến đo độ ẩm[BTL] Cảm biến đo độ ẩm
[BTL] Cảm biến đo độ ẩmHoàng Phạm
 
Throttling, Choking and environmental asphyxia
Throttling, Choking and environmental asphyxia Throttling, Choking and environmental asphyxia
Throttling, Choking and environmental asphyxia hafsahassan16
 
Hệ thống bôi trơn và hệ thống làm mát
Hệ thống bôi trơn và hệ thống làm mátHệ thống bôi trơn và hệ thống làm mát
Hệ thống bôi trơn và hệ thống làm mátHoàng Phạm
 

Viewers also liked (19)

Audiovisuales cartel de hombre
Audiovisuales cartel de hombreAudiovisuales cartel de hombre
Audiovisuales cartel de hombre
 
Trabajo de la linea
Trabajo de la lineaTrabajo de la linea
Trabajo de la linea
 
Chap 7 1_temperament_test
Chap 7 1_temperament_testChap 7 1_temperament_test
Chap 7 1_temperament_test
 
Cultura audioviual trabajo del color
Cultura audioviual trabajo del colorCultura audioviual trabajo del color
Cultura audioviual trabajo del color
 
Configuration cellvizio100 2012
Configuration cellvizio100 2012Configuration cellvizio100 2012
Configuration cellvizio100 2012
 
Audiovisuales cartel de mujer
Audiovisuales cartel de mujerAudiovisuales cartel de mujer
Audiovisuales cartel de mujer
 
Animals motion
Animals motionAnimals motion
Animals motion
 
Tarea 12 de cultura audiovisual
Tarea 12 de cultura audiovisualTarea 12 de cultura audiovisual
Tarea 12 de cultura audiovisual
 
Animals motion
Animals motionAnimals motion
Animals motion
 
Cultura audiovisual trabajo de texturas
Cultura audiovisual trabajo de texturasCultura audiovisual trabajo de texturas
Cultura audiovisual trabajo de texturas
 
I+will+sing+forever
I+will+sing+foreverI+will+sing+forever
I+will+sing+forever
 
neiljaysonching
neiljaysonchingneiljaysonching
neiljaysonching
 
Bicol Mass ppt.
Bicol Mass ppt.Bicol Mass ppt.
Bicol Mass ppt.
 
Ordföljd i huvudsats och bisats
Ordföljd i huvudsats och bisatsOrdföljd i huvudsats och bisats
Ordföljd i huvudsats och bisats
 
[BTL] Cảm biến nhiệt độ
[BTL] Cảm biến nhiệt độ[BTL] Cảm biến nhiệt độ
[BTL] Cảm biến nhiệt độ
 
Biomass mngt & utilization
Biomass mngt & utilizationBiomass mngt & utilization
Biomass mngt & utilization
 
[BTL] Cảm biến đo độ ẩm
[BTL] Cảm biến đo độ ẩm[BTL] Cảm biến đo độ ẩm
[BTL] Cảm biến đo độ ẩm
 
Throttling, Choking and environmental asphyxia
Throttling, Choking and environmental asphyxia Throttling, Choking and environmental asphyxia
Throttling, Choking and environmental asphyxia
 
Hệ thống bôi trơn và hệ thống làm mát
Hệ thống bôi trơn và hệ thống làm mátHệ thống bôi trơn và hệ thống làm mát
Hệ thống bôi trơn và hệ thống làm mát
 

Similar to neiljaysonching

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
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping newaprilyyy
 
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
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdfVcTrn1
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++cpjcollege
 
Loops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptLoops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptKamranAli649587
 
Capgemini oracle live-sql
Capgemini oracle live-sqlCapgemini oracle live-sql
Capgemini oracle live-sqlJohan Louwers
 

Similar to neiljaysonching (20)

C++ programming
C++ programmingC++ programming
C++ programming
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping new
 
Switch case and looping jam
Switch case and looping jamSwitch case and looping jam
Switch case and looping jam
 
My final requirement
My final requirementMy final requirement
My final requirement
 
Survelaine murillo ppt
Survelaine murillo pptSurvelaine murillo ppt
Survelaine murillo ppt
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
 
Final requirement
Final requirementFinal requirement
Final requirement
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdf
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
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++
 
Loops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptLoops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.ppt
 
Capgemini oracle live-sql
Capgemini oracle live-sqlCapgemini oracle live-sql
Capgemini oracle live-sql
 
Project in programming
Project in programmingProject in programming
Project in programming
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 

neiljaysonching

  • 1. CMSC 104, Version 8/06 1 L15Switch.ppt Power Point Presentation In Loop Case Statement . . http://eglobiotraining.com.
  • 2. CMSC 104, Version 8/06 2 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 3 L15Switch.ppt The Screen Shot on Looping Statement in Programming http://eglobiotraining.com.
  • 4. CMSC 104, Version 8/06 4 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 5 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 6 L15Switch.ppt The Screen Shot on Looping Statement in Programming http://eglobiotraining.com.
  • 7. CMSC 104, Version 8/06 7 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 8 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 9 L15Switch.ppt The Screen Shot on Looping Statement in Programming http://eglobiotraining.com.
  • 10. CMSC 104, Version 8/06 10 L15Switch.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 11 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.
  • 12. CMSC 104, Version 8/06 12 L15Switch.ppt The Screen Shot on Looping Statement in Programming http://eglobiotraining.com.
  • 13. CMSC 104, Version 8/06 13 L15Switch.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 14 L15Switch.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 15 L15Switch.ppt The Screen Shot on Looping Statement in Programming http://eglobiotraining.com.
  • 16. CMSC 104, Version 8/06 16 L15Switch.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 17 L15Switch.ppt Power Point Presentation In Switch Case Statement http://eglobiotraining.com.
  • 18. CMSC 104, Version 8/06 18 L15Switch.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 19 L15Switch.ppt The Screen Shot of Switch Case Statement in Programming http://eglobiotraining.com.
  • 20. CMSC 104, Version 8/06 20 L15Switch.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 21 L15Switch.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 22 L15Switch.ppt The Screen Shot of Switch Case Statement in Programming http://eglobiotraining.com.
  • 23. CMSC 104, Version 8/06 23 L15Switch.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 24 L15Switch.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 25 L15Switch.ppt The Screen Shot on Looping Statement in Programming http://eglobiotraining.com.
  • 26. CMSC 104, Version 8/06 26 L15Switch.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 27 L15Switch.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 28 L15Switch.ppt The Screen Shot of Switch Case Statement in Programming http://eglobiotraining.com.
  • 29. CMSC 104, Version 8/06 29 L15Switch.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 30 L15Switch.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 31 L15Switch.ppt The Screen Shot of Switch Case Statement in Programming http://eglobiotraining.com.
  • 32. CMSC 104, Version 8/06 32 L15Switch.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.