3. In programming, a switch, case, select or inspect statement is a type of selection
control mechanism that exists in most imperative programming languages such
as Pascal, Ada, C/C++, C#, Java, and so on. It is also included in several other types of
languages. Its purpose is to allow the value of a variable or expression to control the
flow of program execution via a multi way branch (or "goto", one of several labels).
The main reasons for using a switch statement in programming is to include
improving clarity, by reducing otherwise repetitive coding, and (if
the heuristics permit) also offering the potential for faster execution through
easier compiler optimization in many cases.
It is a substitute for long if statements that compare a variable to several "integral"
values ("integral" values are simply values that can be expressed as an integer, such as
the value of a char).
In computer programming, the value of the variable given into switch is compared to
the value following each of the cases, and when one value matches the value of the
variable, the computer continues executing the program from that point.
http://eglobiotraining.com
4. A loop (in programming) is a sequence of statements which is specified once but
which may be carried out several times in succession. The code "inside" the loop
(the body of the loop, shown below as xxx) is obeyed a specified number of
times, or once for each of a collection of items, or until some condition is
met, or indefinitely.
In functional programming languages, such as Haskell and Scheme, loops can be
expressed by using recursion or fixed point iteration rather than explicit looping
constructs. Tail recursion is a special case of recursion which can be easily
transformed to iteration.
A loop is a fundamental programming idea that is commonly used in
writing programs.
Loops are used to repeat a block of code. Being able to have your
program repeatedly execute a block of code is one of the most basic but
useful tasks in programming.
http://eglobiotraining.com
5. Source Code of Switch Screen Shot of Example 1
Case Statement 1
http://eglobiotraining.com
#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();
}
6. The first example of switch case statement in
programming allows the user to input a number
that states the functions in a game. If the user types
the number before each chosen function [e.g 1 (play
game)], the running program will show the words
“thank you for playing”.
http://eglobiotraining.com
7. Source Code of Switch
Screen Shot of Example 2
Case Statement 2
http://eglobiotraining.com
#include <iostream>
#include <stdlib.h>
using namespace std;
void welcome();
int getInteger();
void displayResponse(int choice);
int main(int argc, char *argv[])
{
int choice; // declares the choice variable
welcome(); // This calls the welcome function
choice = getInteger(); // calls getInteger and receives the value for choice
displayResponse(choice); // passes choice to displayResponse function
system("PAUSE");
return 0;
} // end main
// welcome function displays an opening message to
// explain the program to the user
void welcome()
{
cout << "This program displays different messages dependingn";
cout << "on which number is entered by the user.n";
cout << "Pick a number between 1 and 6 to see whatn";
cout << "the program will say.nn";
} // end of welcome function
// getInteger asks the user for a number between 1 and 6.
// The integer is returned to where the function was called.
int getInteger()
{
int response; // declares variable called response
cout << "Please type a number between 1 and 6: "; // prompt for number
cin >> response; // gets input from user and assigns it to response
return response; // sends back the response value
} // end getInteger function
// displayResponse function takes the int variable and uses it
// to determine which set of tasks will be performed.
void displayResponse(int choice)
{
int again;
// switch statement based on the choice variable
switch (choice) // notice no semicolon
{
case 1: // choice was the number 1
cout << "One is a lonely number and very useful in math.nn";
break; // this ends the statements for case 1
case 2: // choice was the number 2
cout << "Two is the only even prime number.nn";
break; // this ends the statements for case 2
case 3: // choice was the number 3
cout << "Three is a crowd and also a prime number.nn";
break; // this ends the statements for case 3
case 4: // choice was the number 4
cout << "Four square is a fun game to play, but four squared is ";
cout << 4 * 4 << ".nn";
break; // this ends the statements for case 4
case 5: // choice was the number 5
cout << "Counting by fives is fun. Five, Ten, Fifteen, Twenty...nn";
break; // this ends the statements for case 5
case 6: // choice was the number 6
cout << "Six is divisible by two and three.nn";
break; // this ends the statements for case 6
default: // used when choice falls out of the cases covered above
cout << "You didn't pick a number between 1 and 6.nn";
again = getInteger(); // gives the user another try
displayResponse(again); // recalls displayResponse with new number
break;
} // end of switch statement
} // end displayResponse function
8. The second example of switch case
statement in programming displays different
messages depending on which number is
entered by the user. Pick a number between 1
and 6 and see what the program will say.
http://eglobiotraining.com
9. Source Code of Switch
Screen Shot of Example 3
Case Statement 3
http://eglobiotraining.com
#include <iostream>
#include <stdlib.h>
using namespace std;
void welcome();
char getChar();
void displayResponse(char choice);
int main(int argc, char *argv[])
{
char choice; // declares the choice variable
welcome(); // This calls the welcome function
choice = getChar(); // calls getChar and returns the value for choice
displayResponse(choice); // passes choice to displayResponse function
system("PAUSE");
return 0;
} // end main
// welcome function displays an opening message to
// explain the program to the user
void welcome()
{
cout << "This program displays different messages dependingn";
cout << "on which letter is entered by the user.n";
cout << "Pick a letter a, b or c to see whatn";
cout << "the program will say.nn";
} // end of welcome function
// getChar asks the user for a letter a, b or c.
// The character is returned to where the function was called.
char getChar()
{
char response; // declares variable called response
cout << "Please type a letter a, b or c: "; // prompt for letter
cin >> response; // gets input from user and assigns it to response
return response; // sends back the response value
} // end getChar function
// displayResponse function takes the char variable and uses it
// to determine which set of tasks will be performed.
void displayResponse(char choice)
{
char again;
// switch statement based on the choice variable
switch (choice) // notice no semicolon
{
case 'A': // choice was the letter A
case 'a': // choice was the letter a
cout << "A is for apple.nn";
break; // this ends the statements for case A/a
case 'B': // choice was the letter b
case 'b': // choice was the letter b
cout << "B is for baseball.nn";
break; // this ends the statements for case B/b
case 'C': // choice was the letter C
case 'c': // choice was the letter c
cout << "C is for cat.nn";
break; // this ends the statements for case C/c
default: // used when choice falls out of the cases covered above
cout << "You didn't pick a letter a, b or c.nn";
again = getChar(); // gives the user another try
displayResponse(again); // recalls displayResponse with new character
break;
} // end of switch statement
} // end displayResponse function
10. The third example of switch case statement
in programming displays different messages
depending on which letter is chosen by the
user. Pick a, b or c and see what the program
will say.
http://eglobiotraining.com
11. Source Code of Switch Screen Shot of Example 4
Case Statement 4
http://eglobiotraining.com
//
// Demonstrates switch statement
#include <iostream.h>
int main()
{
unsigned short int number;
cout << "Enter a number between 1
and 5: ";
cin >> number;
switch (number)
{
case 0: cout << "Too small, sorry!";
break;
case 5: cout << "Good job!n"; // fall
through
case 4: cout << "Nice Pick!n"; // fall
through
case 3: cout << "Excellent!n"; // fall
through
case 2: cout << "Masterful!n"; // fall
through
case 1: cout << "Incredible!n";
break;
default: cout << "Too large!n";
break;
}
cout << "nn";
return 0;
}
12. This fourth example of switch case statement
in programming asks the user to pick a
number ranging from 1 to 5. Switch Case in
programming is effective to any program
especially in multiple choices.
http://eglobiotraining.com
13. Source Code of Switch Screen Shot of Example 5
Case Statement 5
http://eglobiotraining.com
#include <iostream>
using namespace std;
int main ()
{
int score;
cout << "What was your score?";
cin >> score;
if (score <= 25)
{
cout << "nOuch, less than 25...!";
}
else if (score <= 50)
{
cout << "nYou score aint great mate..";
}
else if (score <= 75)
{
cout << "nYour pretty good, wel done man!";
}
else if (score <= 100)
{
cout << "nYou got to the top!!!";
}
else
{
cout << "nYou cant score higher than 100!!! Cheater!!!!";
}
cin.ignore();
cin.get();
return 0;
}
14. This fifth example of switch case statement in
programming allows the user to input a score
from 0 to 100. This programming sample is
like having a feedback on the score you got
(let’s say, from an exam).
http://eglobiotraining.com
15. Source Code of Screen Shot of Example 1
Looping Statement 1
http://eglobiotraining.com
#include <iostream>
using namespace std; // So the
program can see cout and endl
int main()
{
// The loop goes while x <
10, and x increases by one
every loop
for ( int x = 0; x < 10; x++ ) {
// Keep in mind that the loop
condition checks
// the conditional statement
before it loops again.
// consequently, when x
equals 10 the loop breaks.
// x is updated before the
condition is checked.
cout<< x <<endl;
}
cin.get();
}
16. This first example of looping in computer
programming is an example of FOR loop.
For loop is said to be the most useful type in
programming.
http://eglobiotraining.com
17. Source Code of Screen Shot of Example 2
Looping Statement 2
http://eglobiotraining.com
#include <iostream>
using namespace std; // So we
can see cout and endl
int main()
{
int x = 0; // Don't forget to
declare variables
while ( x < 10 ) { // While x is
less than 10
cout<< x <<endl;
x++; // Update x so the
condition can be met
eventually
}
cin.get();
}
18. This second example of looping in
programming is just another simple loop
program.
A loop is a type in computer programming
that is usually known through being a
program that checks the whole process by
reaching the end then jumping back again to
the beginning.
http://eglobiotraining.com
19. Source Code of Screen Shot of Example 3
Looping Statement 3
http://eglobiotraining.com
#include <iostream>
using namespace std;
int main()
{
int x;
x = 0;
do {
// "Hello, world!" is printed at
least one time
// even though the condition
is false
cout<<"Hello, world!n";
} while ( x != 0 );
cin.get();
}
20. This third example of looping in
programming shows at least on print of
“Hello World!” even if the condition is false.
http://eglobiotraining.com
21. Source Code of Screen Shot of Example 4
Looping Statement 4
http://eglobiotraining.com
#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;
}
22. This fourth example of looping in
programming is a given sample of a DO
WHILE loop.
These type of loops are useful in computer
programming.
http://eglobiotraining.com
23. Source Code of Screen Shot of Example 5
Looping Statement 5
http://eglobiotraining.com
#include <iostream>
using namespace std;
int main ()
{
int x;
cout << "Input the number:";
cin >> x;
while (x>0)
{
cout << x << "; ";
--x;
}
cout << "EXFORSYS";
}
24. This fifth example of looping in programming
tells the user to input a number. And once the
user clicked enter, the word “exforsys”
appears.
http://eglobiotraining.com