2. What is a Switch Case?
0 Switch Case 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 multiway branch (or
"goto", 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 case.
3. Examples:
Example #1: break;
case 'D' :
#include <iostream> cout << "You passed" << endl;
using namespace std; break;
case 'F' :
int main () cout << "Better try again" << endl;
{ break;
// local variable declaration: default :
char grade = 'D'; cout << "Invalid grade" << endl;
}
switch(grade) cout << "Your grade is " << grade << endl;
{
case 'A' : System (“pause”);
cout << "Excellent!" << endl; return 0;
break; }
case 'B' :
case 'C' :
cout << "Well done" << endl;
4. Example #2: }
#include <iostream> cout << "Of course I had nothing to
using namespace std; do with the grade.";
cout << "nThe professor was really
int main () off the wall.n";
{
system ("pause");
char letGrad, ch; return 0;
cout << "Please enter a character: ";
cin >> letGrad; }
switch (letGrad)
{
case 'A': cout << "The numerical
grade is between 90 and 100"; break;
case 'B': cout << "The numerical
grade is between 80 and 89.9"; break;
case 'C': cout << "The numerical
grade is between 70 and 79.9"; break;
case 'D': cout << "How are you going
to explain this one?"; break;
5. Example #3: break;
#include <iostream> case 'F':
using namespace std; person = "female";
int main ( ) break;
{
string person ; case 'f':
char gender ; person ="female";
cout << "Please enter your gender !n"; break;
cin >> gender; default:
switch (gender) person = "unknown";
{ case 'm': }
person = "male" ; cout << person << endl;
break;
system ("pause");
case 'M': return 0;
person = "male"; }
6. Example #4: break;
default: cout << "Too large!n";
#include <iostream.h> break;
int main() }
{ cout << "nn";
unsigned short int number;
cout << "Enter a number between 1 and 5: "; system("pause");
cin >> number; return 0;
switch (number)
{ }
case 0: cout << "Too small, sorry!";
break;
case 5: cout << "Good job!n";
break;
case 4: cout << "Nice Pick!n";
break;
case 3: cout << "Excellent!n";
break;
case 2: cout << "Masterful!n";
break;
case 1: cout << "Incredible!n";
7.
8. What are Loops?
0 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 --
many programs or websites that produce extremely
complex output (such as a message board) are really only
executing a single task many times. (They may be executing
a small number of tasks, but in principle, to produce a list
of messages only requires repeating the operation of
reading in some data and displaying it.) Now, think about
what this means: a loop lets you write a very simple
statement to produce a significantly greater result simply
by repetition.