3. SWITCH CASE
STATEMENT
0 In programming, a switch, case, select or inspect stateme
nt 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 Programming languages. Its
purpose is to allow the value of a variable or expression to
control the flow of program execution via a multiway
branch (or "go to", one of several labels). The main reasons
for using a switch 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.
T
O
P
I http://eglobiotraining.com NEXT
C
4. SWITCH CASE
STATEMENT
0 Switch case statements are 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). The basic format for using the switch case
in the programming is outlined below. 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.
0 The switch-case statement is a multi-way decision statement.
Unlike the multiple decision statement that can be created
using if-else, the switch statement evaluates the
conditional expression and tests it against numerous
constant values. The branch corresponding to the value that the
expression matches is taken during execution.
T
O
P
I BACK http://eglobiotraining.com NEXT
C
5. SWITCH CASE
STATEMENT
0 Switch is used to choose a fragment of template
depending on the value of an expression
0 This has a similar function as the If condition - but it is
more useful in situations when there is many possible
values for the variable. Switch will evaluate one of
several statements, depending on the value of a given
variable. If no given value matches the variable, the
default statement is executed.
0 The value of the expressions in a switch-case
statement must be an ordinal type
T i.e. integer, char, short, long, etc. Float and double are
O
P not allowed.
I BACK http://eglobiotraining.com NEXT
C
8. Example of switch case IN C
PROGRAMMING
#include <iostream> // switch statement based on the choice variable
#include <stdlib.h>
using namespace std; switch (choice) // notice no semicolon
void welcome(); {
char getChar(); case 'A': // choice was the letter A
void displayResponse(char choice);
int main(int argc, char *argv[]) case 'a': // choice was the letter a
{ cout << "your awesome dude.nn";
char choice; // declares the choice variable break; // this ends the statements for case A/a
welcome(); // This calls the welcome function
choice = getChar(); // calls getChar and returns the value for choice case 'B': // choice was the letter b
displayResponse(choice); // passes choice to displayResponse function case 'b': // choice was the letter b
cout << "you will find your lovelife.nn";
system("PAUSE");
return 0; break; // this ends the statements for case B/b
} // end main case 'C': // choice was the letter C
// welcome function displays an opening message to case 'c': // choice was the letter c
// explain the program to the user
void welcome() cout << "your will won the lottery.nn";
{ break; // this ends the statements for case C/c
cout << "This program displays different messages dependingn"; case 'D': // choice was the letter D
cout << "on which letter is entered by the user.n";
cout << "Pick a letter a, b, c or d to see whatn"; case 'd': // choice was the letter d
cout << "the program will say.nn"; cout << "your so ugly!!.nn";
} // end of welcome function break; // this ends the statements for case D/d
// getChar asks the user for a letter a, b or c.
// The character is returned to where the function was called. default: // used when choice falls out of the cases covered
char getChar() above
{ cout << "You didn't pick a letter a, b or c.nn";
char response; // declares variable called response
cout << "Please type a letter a, b, c and d: "; // prompt for letter
again = getChar(); // gives the user another try
cin >> response; // gets input from user and assigns it to response displayResponse(again); // recalls displayResponse with new
return response; // sends back the response value character
} // end getChar function break;
// displayResponse function takes the char variable and uses it
// to determine which set of tasks will be performed.
} // end of switch statement
void displayResponse(char choice) } // end displayResponse function
{
char again;
T
O
P
I BACK http://eglobiotraining.com NEXT
C
9. Running switch case IN C
PROGRAMMING
T
O
P
I http://eglobiotraining.com
C BACK
10. LOOPING
There may be a situation when you need to execute a block
of code several number of times. In general statements are
executed sequentially: The first statement in a function is
executed first, followed by the second, and so on.
Programming languages provide various control structures
that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group
of statements multiple times and following is the general
from of a loop statement in most of the programming
T languages.
O
P
I
LOOP http://eglobiotraining.com
C NEXT
11. LOOPING
C++ programming language provides
following types of loop to handle
looping requirements:
T
O
P
I
http://eglobiotraining.com
C
BACK
12. T
"FOR” LOOP
O
P
I
C
0 A for loop is a repetition control structure that allows
you to efficiently write a loop that needs to execute a
specific number of times.
0 The statements in the for loop repeat continuously for
aspecific number of times. The while and do-
while loops repeat until a certain condition is
met. The for loop repeats until a specific count is
met. Use a for loop when the number of repetition is
know, or can be supplied by the user.
http://eglobiotraining.com
LOOP BACK NEXT
13. T
O
P
I
C EXAMPLE OF FOR LOOPING IN
C PROGRAMMING
#include <iostream>
#include <cmath>
}
using namespace std;
cout <<"nSeconds
//prototype falling distancen";
int fallingdistance(); cout <<"---------------------------------------n";
for ( count = 1; count <= time; count++)
//main function
{
int main() distance = .5 * 9.8 *
{ pow(time, 2.0);
int count = 1 ; cout << count << "
int time; " << distance <<" meters"<< endl;
double distance ;
}
cout << "Please enter time in 1
system ("pause");
through 10 seconds.nn"; return 0;
}
time = fallingdistance(); // falling distance function for a return value in seconds
transfer to time
while ( time < 1 || time > 10) int fallingdistance ()
{
{ cout << "Must enter between 1 and
int seconds; NEXT
10 seconds, please re-enter.n"; cin >> seconds;
time = fallingdistance(); http://eglobiotraining.com
LOOP return seconds; BACK
}
14. T
O
P
I
C
RUNNING FOR LOOP IN IN
C PROGRAMMING
http://eglobiotraining.com
LOOP BACK
15. “WHILE” LOOP
T
O
P
I
C
0 The while loop allows programs to repeat a statement
or series of statements, over and over, as long as a
certain test condition is true.
0 The while loop can be used if you don’t know how
many times a loop must run.
0 A while loop statement repeatedly executes a target
statement as long as a given condition is true.
BACK http://eglobiotraining.com
LOOP NEXT
16. T
O
P
I
C
EXAMPLE OF WHILE LOOP
IN C PROGRAMMING
#include <iostream.h>
int main(void) {
int x = 0;
int y = 0;
bool validNumber = false;
while (validNumber == false) {
cout << "Please enter an integer between 1 and 10: ";
cin >> x;
cout << "You entered: " << x << endl << endl;
if ((x < 1) || (x > 10)) {
cout << "Your value for x is not between 1 and 10!"
<< endl;
cout << "Please re-enter the number!" << endl << endl;
}
else
validNumber = true;
}
cout << "Thank you for entering a valid number!" << endl;
return 0;
0 }
BACK http://eglobiotraining.com
LOOP NEXT
17. T
O
P
I
C RUNNING WHILE LOOP IN
C PROGRAMMING
LOOP http://eglobiotraining.com
BACK
18. T
O
P
“DO WHILE” LOOP
I
C
0 In most computer programming languages, a do while
loop, sometimes just called a while loop, is a control
flow statement that allows code to be executed once based
on a given Boolean condition.
0 The do while construct consists of a process symbol and a
condition. First, the code within the block is executed, and
then the condition is evaluated. If the condition is true the
code within the block is executed again. This repeats until
the condition becomes false. Because do while loops check
the condition after the block is executed, the control
structure is often also known as a post-test loop. Contrast
with the while loop, which tests the condition before the
code within the block is executed.
LOOP BACK http://eglobiotraining.com
NEXT
19. T
O
P
“DO WHILE” LOOP
I
C
0 Unlike for and while loops, which test the loop
condition at the top of the loop, the do...while loop
checks its condition at the bottom of the loop.
0 A do...while loop is similar to a while loop, except that
a do...while loop is guaranteed to execute at least one
time.
0 The do-while loop is similar to the while loop, except
that the test condition occurs at the end of the
loop. Having the test condition at the end, guarantees
that the body of the loop always executes at least one
time.
LOOP http://eglobiotraining.com
BACK NEXT
20. T
O
P
I
C
EXAMPLE OF A DO WHILE
LOOP IN C PROGRAMMING
0 #include <iostream>
0 using namespace std;
0 main()
0 { int num1, num2;
0 char again = 'y';
0 while (again == 'y' || again == 'Y') {
0 cout << "Enter a number: ";
0 cin >> num1;
0 cout << "Enter another number: ";
0 cin >> num2;
0 cout << "Their sum is " << (num1 + num2) << endl;
0 cout << "Do you want to do this again? ";
0 cin >> again; }
0 return 0;
0 }
LOOP BACK http://eglobiotraining.com
NEXT
21. T
O
P
I
C
RUNNING DO WHILE LOOP IN C
PROGRAMMING
http://eglobiotraining.com
LOOP BACK