2. SWITCH CASE STATEMENT
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
Programming languages. Its purpose is to allow the
value of a variable or expression to control the flow
of program execution via a multway branch (or "go
to", one of several labels).
http://eglobiotraining.com
3. SWITCH CASE STATEMENT
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.
http://eglobiotraining.com
4. SWITCH CASE STATEMENT
Switch is used to choose a fragment of template
depending on the value of an expression
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.
http://eglobiotraining.com
5. EXAMPLE OF SWITCH CASE IN C
PROGRAMMING cout << "This program displays different messages
dependingn";
cout << "on which letter is entered by the user.n";
#include <iostream>
cout << "Pick a letter a, b, c or d to see whatn";
#include <stdlib.h>
cout << "the program will say.nn";
using namespace std;
} // end of welcome function
void welcome();
// getChar asks the user for a letter a, b or c.
char getChar();
// The character is returned to where the function was called.
void displayResponse(char choice);
char getChar()
int main(int argc, char *argv[])
{
{
char response; // declares variable called response
char choice; // declares the choice variable
cout << "Please type a letter a, b, c and d: "; // prompt for
welcome(); // This calls the welcome function letter
choice = getChar(); // calls getChar and returns the value cin >> response; // gets input from user and assigns it to
for choice response
displayResponse(choice); // passes choice to return response; // sends back the response value
displayResponse function
} // end getChar function
// displayResponse function takes the char variable and uses
system("PAUSE"); it
return 0; // to determine which set of tasks will be performed.
} // end main void displayResponse(char choice)
// welcome function displays an opening message to {
// explain the program to the user char again;
void welcome()
{
http://eglobiotraining.com
6. // 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 << "your awesome dude.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 << "you will find your lovelife.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 << "your will won the lottery.nn";
break; // this ends the statements for case C/c
case 'D': // choice was the letter D
case 'd': // choice was the letter d
cout << "your so ugly!!.nn";
break; // this ends the statements for case D/d
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 http://eglobiotraining.com
7. LOOPING
There may be a situation when you need to execute a block
of code several number of times.
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 languages.
http://eglobiotraining.com
8. FOR LOOP
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.
The statements in the for loop repeat continuously
for a specific 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.
http://eglobiotraining.com
9. EXAMPLE OF FOR LOOPING
IN C PROGRAMMING
#include <iostream> }
#include <cmath>
using namespace std; cout <<"nSeconds
falling distancen";
//prototype cout <<"---------------------------------------n";
int fallingdistance();
for ( count = 1; count <= time; count++)
{
//main function distance = .5 * 9.8 *
int main() pow(time, 2.0);
{ cout << count << "
int count = 1 ; " << distance <<" meters"<< endl;
int time;
double distance ; }
cout << "Please enter time in 1 through 10 system ("pause");
seconds.nn"; return 0;
}
time = fallingdistance(); // falling distance function for a return value in seconds
transfer to time
int fallingdistance ()
while ( time < 1 || time > 10) {
{ cout << "Must enter between 1 and 10 int seconds;
seconds, please re-enter.n"; cin >> seconds;
time = fallingdistance(); return seconds;
}
http://eglobiotraining.com
10. WHILE LOOP
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.
The while loop can be used if you don’t know how
many times a loop must run.
http://eglobiotraining.com
11. EXAMPLE OF WHILE LOOP IN C
PROGRAMMING
#include <iostream.h> if ((x < 1) || (x > 10)) {
cout << "Your value for x is not between
1 and 10!"
int main(void) {
int x = 0; << endl;
cout << "Please re-enter the number!"
int y = 0;
<< endl << endl;
bool validNumber = false;
}
else
while (validNumber == false) {
validNumber = true;
cout << "Please enter an integer between 1
and 10: "; }
cin >> x;
cout << "Thank you for entering a valid
cout << "You entered: " << x << endl <<
number!" << endl;
endl;
return 0;
}
http://eglobiotraining.com
12. DO WHILE LOOP
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.
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.
http://eglobiotraining.com
13. DO WHILE LOOP
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.
A do...while loop is similar to a while loop, except
that a do...while loop is guaranteed to execute at
least one time.
http://eglobiotraining.com
14. EXAMPLE OF A DO WHILE
LOOP IN C PROGRAMMING
#include <iostream>
using namespace std;
main()
{ int num1, num2;
char again = 'y';
while (again == 'y' || again == 'Y') {
cout << "Enter a number: ";
cin >> num1;
cout << "Enter another number: ";
cin >> num2;
cout << "Their sum is " << (num1 + num2) << endl;
cout << "Do you want to do this again? ";
cin >> again; }
return 0;
}
http://eglobiotraining.com