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# programming language , 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 "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
cases.
HTTP://EGLOBIOTRAINING.COM/
4. char key;
C# printf("Enter an arithmetic operatorn");
scanf("%c",&key);
In C# programming, every case block switch (key)
that contains any statements must have a {
case '+':
reachable end point, or triggers a add();
compilation error.In programming break;
usually, this is a break statement, but any
case '-':
jump statement can be used – such as subtract();
return, goto or throw – or the switch can break;
simply end with an infinite loop. Case
case '*':
fall-through is only permitted when there
multiply();
are no statements between one case break;
statement and the next. If fall-through is
otherwise desired, it must be made case '/':
divide();
explicit with the goto case construct. C# break;
programming also allows the use of
non-integer case values, such as Strings. default:
printf("invalid keyn");
break;
}
HTTP://EGLOBIOTRAINING.COM/
5. Select Case n
Case Is < -5
MsgBox("n is less than -5")
Visual Basic .NET Case -4 To -1
MsgBox("n is between -4 and -1")
Case 0
MsgBox("n is 0")
Case 2, 4, 6, 8
In MsgBox("n is even")
Case 1, 3, 5, 7, 9
programming, Visual MsgBox("n is odd")
Case Else
Basic .NET, the switch MsgBox("only single-digit numbers are allowed.", vbCritical)
End Select
statement is called Visual FoxPro
"Select Case Visual FoxPro:
programming", and Do Case
fall-through to later Case field_1 = "X"
Replace field_b With 1
blocks is not supported. Case field_1 = "Y"
Replace field_b With 2
However, ranges and Case field_1 = "Z"
Replace field_b With 3
various constructs from Endcase
If statements are both
supported
HTTP://EGLOBIOTRAINING.COM/
6. Haskell #include <iostream>
using namespace std;
int main(void)
{
char grade;
cout << "Enter your grade: ";
cin >> grade;
switch (grade)
Haskell's case {
case 'A':
construct, unlike C- cout << "Your average must be between 90 - 100"
<< endl;
influenced programming break;
case 'B':
languages, has no fall- cout << "Your average must be between 80 - 89"
<< endl;
break;
through behaviour. It is a case 'C':
cout << "Your average must be between 70 - 79"
programming expression << endl;
break;
which returns a value, and case 'D':
cout << "Your average must be between 60 - 69"
it can deconstruct values << endl;
break;
using pattern matching. default:
cout << "Your average must be below 60" << endl;
}
return 0;
}
HTTP://EGLOBIOTRAINING.COM/
7. #include<iostream>
using namespace std;
int main()
{
Pascal int a;
cin >> a;
if ( a <= 10 )
{
In Programming, Pascal does 10" << 'n';
cout << "Below
not allow “fall through”, but }
else
has ranges and comma {
separated literal lists. if ( a < 60 )
{
cout << "Below 60" << 'n';
}
}
return 0;
}
HTTP://EGLOBIOTRAINING.COM/
8. #include<iostream>
using namespace std;
int main()
{
char myinput;
cin >> myinput;
switch (myinput)
Perl
{
case 'a':
cout << "Run program 1n";
break;
case 'b':
Perl 5.10 has a cout << "Run program 2n";
{
powerful built in switch cout << "Please Waitn";
statement called
break;
}
default:
given, where the cout << "Invalid choicen";
programming cases are break;
}
called when:
return 0;
}
HTTP://EGLOBIOTRAINING.COM/
11. In programming, very often when you write code, you want the
same block of code to run a number of times. You can use
looping statements in your code to do this.
In programming, the JavaScript programming have the following
looping statements:
While programming- loops through a block of code while a
condition is true
Do...while programming- loops through a block of code
once, and then repeats the loop while a condition is true
For programming- run statements a specified number of times
HTTP://EGLOBIOTRAINING.COM/
12. #include <iostream>
using namespace std;
while
int main()
{
In programming while statement int x;
will execute a block of code while a
x = 0;
condition is true..
do {
// "Hello, world!" is printed at least one time
// even though the condition is false
cout<<"Hello, world!n";
} while ( x != 0 );
cin.get();
}
HTTP://EGLOBIOTRAINING.COM/
13. Do...while
/**
** This example contains a switch statement that performs
** the same statement for more than one case label.
In Programming the
**/
#include <stdio.h>
do...while statement will int main(void)
{
execute a block of code int month;
once, and then it will
/* Read in a month value */
printf("Enter month: ");
scanf("%d", &month);
repeat the loop while a /* Tell what season it falls into */
switch (month)
condition is true {
case 12:
case 1:
The Java programming case 2:
printf("month %d is a winter monthn", month);
break;
language also provides a case 3:
do-while
case 4:
case 5:
printf("month %d is a spring monthn", month);
statement, which can be break;
case 6:
expressed as follows: case 7:
case 8:
printf("month %d is a summer monthn", month);
break;
case 9:
case 10:
case 11:
printf("month %d is a fall monthn", month);
break;
HTTP://EGLOBIOTRAINING.COM/ case 66:
case 99:
default:
14. #include <iostream>
using namespace std; // So the program can see
cout and endl
int main()
for {
// The loop goes while x < 10, and x increases by
one every loop
In programming the “for” for ( int x = 0; x < 10; x++ ) {
statement will execute a block of // Keep in mind that the loop condition checks
// the conditional statement before it loops
code a specified number of times again.
// consequently, when x equals 10 the loop
breaks.
// x is updated before the condition is checked.
cout<< x <<endl;
}
cin.get();
}
HTTP://EGLOBIOTRAINING.COM/
15. The Infinite Loop:
A loop becomes infinite loop if a
condition never becomes false. The #include <iostream> using
for loop is traditionally used for this namespace std; int main () { for( ; ; )
purpose. Since none of the three { printf("This loop will run
expressions that form the for loop forever.n"); } return 0; }
are required, you can make an endless
loop by leaving the conditional
expression empty.
HTTP://EGLOBIOTRAINING.COM/
16. While Loop #include <iostream.h>
int main(void) {
int x = 0;
In programming, For int y = 0;
bool validNumber = false;
repeating C while (validNumber == false) {
programming cout << "Please enter an integer between 1
and 10: ";
statements whiles a
cin >> x;
cout << "You entered: " << x << endl <<
endl;
condition is true,the if ((x < 1) || (x > 10)) {
while provides a the x is not between 1 and 10!"
cout << "Your value for
<< endl;
necessary mechanis m. the number!" << endl << endl;
cout << "Please re-enter
}
else
validNumber = true;
}
cout << "Thank you for entering a valid number!" << endl;
return 0;
}
HTTP://EGLOBIOTRAINING.COM/
17. Input : In any programming language input means to feed some data into
program. Programming can be given in the form of file or from command
line. C programming language provides a set of built-in functions to read
given input and feed it to the program as per requirement.
Output : In any programming language output means to display some data
on screen, printer or in any file. C programming language provides a set of
built-in functions to output required data.
HTTP://EGLOBIOTRAINING.COM/