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 • {
• case '+':
have a reachable end point, or • add();
triggers a compilation error.In • break;
programming usually, this is a break • case '-':
statement, but any jump statement • subtract();
can be used – such as return, goto or • break;
throw – or the switch can simply end • case '*':
with an infinite loop. Case fall- • multiply();
through is only permitted when there • break;
are no statements between one case • case '/':
statement and the next. If fall- • divide();
• break;
through is otherwise desired, it must
be made explicit with the goto case • default:
construct. C# programming also • printf("invalid keyn");
• break;
allows the use of non-integer case • }
values, such as Strings.
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
• In programming, Visual Basic • MsgBox("n is 0")
• Case 2, 4, 6, 8
.NET, the switch statement is • MsgBox("n is even")
called "Select Case • Case 1, 3, 5, 7, 9
programming", and fall-through • MsgBox("n is odd")
• Case Else
to later blocks is not supported. • MsgBox("only single-digit numbers are allowed.",
However, ranges and various vbCritical)
• End Select
constructs from If statements are •
both supported • Visual FoxPro
•
• Visual FoxPro:
•
• Do Case
• Case field_1 = "X"
• Replace field_b With 1
• Case field_1 = "Y"
• Replace field_b With 2
• Case field_1 = "Z"
• Replace field_b With 3
• Endcase
http://eglobiotraining.com/
6. • #include <iostream>
using namespace std;
int main(void)
• Haskell {
char grade;
cout << "Enter your grade: ";
cin >> grade;
switch (grade)
• Haskell's case construct, unlike C-influenced {
programming languages, has no fall-through case 'A':
behaviour. It is a programming expression which cout << "Your average must be between 90 - 100"
<< endl;
returns a value, and it can deconstruct values break;
using pattern matching. case 'B':
cout << "Your average must be between 80 - 89"
<< endl;
break;
case 'C':
cout << "Your average must be between 70 - 79"
<< endl;
break;
case 'D':
cout << "Your average must be between 60 - 69"
<< endl;
break;
default:
cout << "Your average must be below 60" << endl;
}
return 0;
}
http://eglobiotraining.com/
7. • #include<iostream>
• using namespace std;
• Pascal • int main()
• {
• int a;
• In Programming, Pascal does not allow • cin >> a;
“fall through”, but has ranges and • if ( a <= 10 )
comma separated literal lists. • {
• cout << "Below
10" << 'n';
• }
• else
• {
• if ( a < 60 )
• {
•
cout << "Below 60" << 'n';
• }
• }
• return 0;
• }
http://eglobiotraining.com/
8. • #include<iostream>
• using namespace std;
• int main()
• {
• char myinput;
• cin >> myinput;
• Perl
• • switch (myinput)
• Perl 5.10 has a powerful built in switch • {
statement called given, where the programming • case 'a':
cases are called when: •
cout << "Run program 1n";
•
break;
• case 'b':
• {
•
cout << "Run program 2n";
•
cout << "Please Waitn";
•
break;
• }
• default:
•
cout << "Invalid choicen";
•
break;
• }
• 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>
• while
• • using namespace std;
• In programming while statement will
execute a block of code while a condition is • int main()
true.. • {
• 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();
• }
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>
• int main(void)
do...while statement •
•
{
int month;
will execute a block of •
•
•
/* Read in a month value */
printf("Enter month: ");
scanf("%d", &month);
code once, and then it •
•
•
/* Tell what season it falls into */
switch (month)
{
will repeat the loop •
•
•
case 12:
case 1:
case 2:
• printf("month %d is a winter monthn", month);
while a condition is •
•
break;
case 3:
true •
•
•
case 4:
case 5:
printf("month %d is a spring monthn", month);
• break;
• The Java programming •
•
case 6:
case 7:
• case 8:
language also provides •
•
printf("month %d is a summer monthn", month);
break;
a do-while statement, •
•
•
case 9:
case 10:
case 11:
•
which can be
printf("month %d is a fall monthn", month);
• break;
• case 66:
expressed as follows: •
•
•
case 99:
default:
printf("month %d is not a valid monthn", month);
• }
• return(0);
• }
http://eglobiotraining.com/
14. • #include <iostream>
• using namespace std; // So the program can
see cout and endl
• for
•
• int main()
• In programming the “for” statement will
execute a block of code a specified number • {
of times • // 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();
• }
http://eglobiotraining.com/
15. • The Infinite Loop: • #include <iostream>
using namespace std;
• A loop becomes infinite int main () { for( ; ; ) {
loop if a condition never printf("This loop will
becomes false. The for run forever.n"); }
loop is traditionally return 0; }
used for this purpose.
Since none of the three
expressions that form
the for loop 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) {
• In programming, For • int x = 0;
• int y = 0;
• bool validNumber = false;
repeating C programming •
• while (validNumber == false) {
statements whiles a •
and 10: ";
cout << "Please enter an integer between 1
condition is true,the •
•
endl;
cin >> x;
cout << "You entered: " << x << endl <<
while provides a the •
• if ((x < 1) || (x > 10)) {
necessary mechanis
m. •
x is not between 1 and 10!"
cout << "Your value for
• << endl;
• cout << "Please re-
enter the number!" << endl << endl;
• }
• 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/