24
SWITCH CASE SAMPLE & LOOPING CASE SAMPLE HTTP://EGLOBIOTRAINING.COM/

Computer programming

Embed Size (px)

Citation preview

Page 1: Computer programming

SWITCH CASE SAMPLE &

LOOPING CASE SAMPLE

H T T P : / / E G L O B I O T R A I N I N G . C O M /

Page 2: Computer programming

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 languages in programming. Its purpose is to

allow the value of a variable or expression to control the flow of program

execution via a multiway branch. The main reasons for using a switch include

improving clarity, by reducing otherwise repetitive coding, and also offering the

potential for faster execution through easier compiler optimization in many

cases of programming.

http://eglobiotraining.com/

Page 3: Computer programming

In programming, the switch statement passes control to the statement following one of the labels or to the statement following the switch body. The value of the expression that precedes the switch body determines which statement receives control. This expression is called the switch expression.In the programming the value of the switch expression is compared with the value of the expression in each case label. If a matching value is found, control is passed to the statement following the case label that contains the matching value. If there is no matching value but there is a default label in the switch body, control passes to the default labeled statement. If no matching value is found, and there is no default label anywhere in the switch body, no part of the switch body is processed.When control passes in programming to a statement in the switch body, control only leaves the switch body when a break statement is encountered or the last statement in the switch body is processed.If necessary, in programming an integral promotion is performed on the controlling expression, and all expressions in the case statements are converted to the same type as the controlling expression. The switch expression can also be of class type if there is a single conversion to integral or enumeration type.Compiling with option -qinfo=gen finds case labels that fall through when they should not.

http://eglobiotraining.com/

Page 4: Computer programming

In programming, the if...else if...else Statement:

In programming, an if statement can be followed by an

optional else if...else statement, which is very useful to test various

conditions using single if...else if statement. When using if , else if ,

else statements in the programming languages there are few points

to keep in mind. An if can have zero or one else's and it must come

after any else if's. An if can have zero to many else if's and they

must come before the else. Once an else if succeeds, none of he

remaining else if's or else's will be tested.

http://eglobiotraining.com/

Page 5: Computer programming

In programming, If the value of expression is nonzero, statement1 is executed. If

the optional else is present, statement2 is executed if the value of expression is

zero. expression must be of arithmetic or pointer type, or it must be of a class type

that defines an unambiguous conversion to an arithmetic or pointer type. In both

forms of the if statement, expression, which can have any value except a

structure, is evaluated, including all side effects. In the programming control

passes from the if statement to the next statement in the program unless one of

the statements contains a break, continue, or goto. The else clause of

an if...else statement is associated with the closest previous if statement in the

same scope that does not have a corresponding else statement. For this sample

to be unambiguous about if...else pairing, uncomment the braces.

http://eglobiotraining.com/

Page 6: Computer programming

C++ Programming if else statement

If-else statement

It is similar to if statement i.e. It is also used to execute or ignore a set

of statements after testing a condition in programming language.

In if-else statement one condition and two blocks of statements are

given.

First blocks contain if statement with condition and its body part.

Second is else and it contain only body part.

http://eglobiotraining.com/

Page 7: Computer programming

Explanation working of if-else statement in programming languages

A condition is a logical or relational expression and it produces either true  or

false result.

If the condition is true the first block of if-else statement(which is 

if-statement) is executed and second is ignored and after executing the first

block , the control is transferred to nextstatement after if -else structure.

If the condition is false then the first blocks of statement is ignored and the

second block of statement is executed. After the executing the second block

of statement the control is transferred to next statement after if-else structure.

http://eglobiotraining.com/

Page 8: Computer programming

#include<iostream>

#include<stdlib.h>

using namespace std;

int main ()

{

int number_of_units, tuitionfee, paymentforunits;

cout<< "number of units you are to enroll:\n\n";

cin>>number_of_units;

paymentforunits=number_of_units*150;

cout<<"payment for units:\n\

n"<<paymentforunits;

tuitionfee=paymentforunits+15%+20;

cout<<"tuition fee:\n\n"<<tuitionfee;

system(“PAUSE");

return 0;

}

http://eglobiotraining.com/

Page 9: Computer programming

#include<stdlib.h>

#include<iostream>

using namespace std;

int main()

{

char gender;

cout<<"Enter your Gender.";

cin>>gender;

if (gender == 'M'||gender == 'm')

cout<<"Male";

else

cout<<"Female";

cout<<"\n\n\n";

system(“PAUSE");

return 0;

}http://eglobiotraining.com/

Page 10: Computer programming

#include<stdlib.h>

#include<iostream>

#include<stdio.h>

#include<conio.h>

using namespace std;

int main()

{

float total_purchase=0, purchase_discount = 0, net_bill=0;

cout<<"PLEASE INPUT TOTAL PURCHASE: ";

cin>>total_purchase;

if (total_purchase >=2000)

purchase_discount = total_purchase*0.10;

cout<<"THE DISCOUNT IS:";

cout<<purchase_discount;

net_bill = (total_purchase)-purchase_discount;

cout<<"\nYOUR NET BILL IS:";

cout<<net_bill;

system("PAUSE");

return 0;

}

http://eglobiotraining.com/

Page 11: Computer programming

#include<iostream>

#include<stdlib.h>

using namespace std;

int main ()

{

int speed;

cout<<"enter speed in kph:" ;

cin>>speed;

cin.ignore();

if (speed >= 80||speed <=99 )

{

cout<<"500";

}

else if (speed <= 100)

{

cout<<"250";

}

else

cout<<"0";

cin.get ();

system("PAUSE");

return 0;

}

http://eglobiotraining.com/

Page 12: Computer programming

#include<stdlib.h>

#include<iostream>

using namespace std;

int main()

{

int year;

cout<<"Enter student year; \n" ;

cin>> year ;

cin. ignore () ;

if (year == 1){

cout<< "Freshmen";

}

if (year == 2)

{

cout<< "Secondary or Sophomore";

}

if (year == 3)

{

cout<< "Tersiary or Junior";

}

if (year == 4)

{

cout<< "Old or Senior";

}

else if (year == 4)

{

cout<< "Error";

}

cin.get () ;

system ("PAUSE") ;

return 0;

}

http://eglobiotraining.com/

Page 13: Computer programming

LOOPING STATEMENTIn computer science a for loop is a programming language statement which allows code

to be repeatedly executed. A for loop is classified as an iteration statement. Unlike many

other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit 

loop counter or loop variable. This allows the body of the for loop to know about the

sequencing of each iteration. For loops are also typically used when the number of

iterations is known before entering the loop. In programming loops are the shorthand way to

make loops when the number of iterations is known, as a for loop can be written as a while

loop. The name for loop comes from the English word for, which is used as the keyword in

most programming languages to introduce a for loop. The loop body is executed "for" the

given values of the loop variable, though this is more explicit in the ALGOL version of the

statement, in which a list of possible values and/or increments can be specified. In 

FORTRAN and PL/I though, the keyword DO is used and it is called a do loop, but it is

otherwise identical to the for loop described here and is not to be confused with the 

Do while loop.

http://eglobiotraining.com/

Page 14: Computer programming

In computer programming, a loop is a sequence of instruction s that is continually

repeated until a certain condition is reached. Typically, a certain process is done,

such as getting an item of data and changing it, and then some condition is checked

such as whether a counter has reached a prescribed number. If it hasn't, the next

instruction in the sequence is an instruction to return to the first instruction in the

sequence and repeat the sequence. If the condition has been reached, the next

instruction "falls through" to the next sequential instruction or branches outside the

loop. A loop is a fundamental programming idea that is commonly used in writing

programs.

http://eglobiotraining.com/

Page 15: Computer programming

KINDS OF FOR LOOPS

A for loop statement is available in most imperative programming languages. Even ignoring

minor differences in syntax there are many differences in how these statements work and the

level of expressiveness they support. Generally, for loops fall into one of the following categories:

Iterator-based for loops

In programming, this type of for loop is a falsification of the numeric range type of for loop; as it

allows for the enumeration of sets of items other than number sequences. It is usually

characterized by the use of an implicit or explicit iterator, in which the loop variable takes on

each of the values in a sequence or other order able data collection.

http://eglobiotraining.com/

Page 16: Computer programming

Vectorised for loops

Some languages of programming offer a for loop that acts as if processing all

iterations in parallel, such as the for all keyword in FORTRAN 95 which has the

interpretation that all right-hand-side expressions are evaluated

before any assignments are made, as distinct from the explicit iteration form.

For example of the programming in the for loop in the following pseudo code

fragment, when calculating the new value for A(i), except for the first (with i = 2)

the reference to A(i - 1) will obtain the new value that had been placed there in

the previous step. In the for all version, however, each calculation refers only to

the original, unaltered A.

http://eglobiotraining.com/

Page 17: Computer programming

Compound for loops

In programming, A value is assigned to the loop variable i and only if

the while expression is true will the loop body be executed. If the

result were false the for-loop's execution stops short. Granted that for

programming the loop variable's value is defined after the termination

of the loop, then the above statement will find the first non-positive

element in array A (and if no such, its value will be N + 1), or, with

suitable variations, the first non-blank character in a string, and so on.

http://eglobiotraining.com/

Page 18: Computer programming

while ( expression ) statement

In programming a while loop, the expression is evaluated. If nonzero,

the statement executes, and the expression is evaluated again. This happens over and

over until the expression's value is zero. If the expression is zero the first time it is

evaluated, statement is not executed at all in the programming.

Do statement while ( expression);

In programming a do while loop is just like a plain while loop, except the statement

executes before the expression is evaluated. Thus, the statement in programming

languages will always be evaluated at least once.

for ( expression1; expression2; expression3 )statement

In programming a for loop, first expression1 is evaluated. Then expression2 is evaluated,

and if it is zero EEL leaves the loop and begins executing instructions after statement.

Otherwise the statement in programming languages is executed, expression3 is evaluated,

and expression2 is evaluated again, continuing until expression2 is zero. You can omit any

of the expressions. If you omit expression2, it is like expression2 is nonzero. while

(expression) is the same as for (; expression; ). The syntax for (;;) creates an endless loop

that must be exited using the break statement in the programming.

http://eglobiotraining.com/

Page 19: Computer programming

#include <iostream>

using namespace std;

int main(){

int a;

cout << "How many times do you want to eat? ";

cin >> a;

while (a){

cout << a << "\n";

--a;

}

system("PAUSE");

cin.get();

return 0;

}

http://eglobiotraining.com/

Page 20: Computer programming

#include<iostream>

#include<string>

int main()

{

using namespace std;

string password, finalsnamin;

finalsnamin="finalsnamin";

cout<<"Enter the correct password"<<endl;

do{

cout<<"Enter the correct password to obtain satisfaction:

";

cin>> password;

}while (password!=finalsnamin);

cout<<"You've got it!"<<endl;

system("pause");

return (0);

}

http://eglobiotraining.com

Page 21: Computer programming

#include <iostream>

#include <string>

using namespace std;

int main(){

string choice = "y";

while ( choice == "y" ){

cout << "You are in the loop" << endl;

cout << "Loop again?" << endl;

cin >> choice;

}

cout << "You exited the loop" << endl;

system("PAUSE");

cin.get();

}

http://eglobiotraining.com/

Page 22: Computer programming

#include<iostream>

#include<string>

int main()

{

using namespace std;

string password, love;

love="love";

cout<<"Enter the correct password"<<endl;

do{

cout<<"Enter the correct password to obtain

satisfaction: ";

cin>> password;

}while (password!=love);

cout<<"You've got it!"<<endl;

system("pause");

return (0);

}

http://eglobiotraining.com

Page 23: Computer programming

#include <iostream>

using namespace std;

int main(){

int a;

cout << "How many hours do we need to

sleep? ";cin >> a;

while (a){

cout << a << "\n";

--a;

}

system("PAUSE");

cin.get();

return 0;

}

http://eglobiotraining.com/

Page 24: Computer programming

THE FINAL REQUIREMENT FOR FUNDAMENTALS OF

PROGRAMMING

Submitted to : Prof. Erwin Globio

Sumbitted by : Xhyna Mea Delfin