Your SlideShare is downloading. ×
0
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Computer programming
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Computer programming

283

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
283
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. SWITCH CASE SAMPLE &LOOPING CASE SAMPLE HTTP://EGLOBIOTRAINING.COM/
  • 2. SWITCH CASE STATEMENT In programming, a switch, case, select or inspect statement is a type ofselection control mechanism that exists in most imperativeprogramming languages such as Pascal, Ada, C/C++, C#, Java, and so on. Itis also included in several other types of languages in programming. Itspurpose is to allow the value of a variable or expression to control the flow ofprogram execution via a multiway branch. The main reasons for using a switchinclude improving clarity, by reducing otherwise repetitive coding, and alsooffering the potential for faster execution through easier compileroptimization in many cases of programming. http://eglobiotraining.com/
  • 3. In programming, the switch statement passes control to the statement followingone of the labels or to the statement following the switch body. The value of theexpression that precedes the switch body determines which statement receivescontrol. This expression is called the switch expression.In the programming the value of the switch expression is compared with the valueof the expression in each case label. If a matching value is found, control ispassed 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, controlpasses to the default labeled statement. If no matching value is found, and thereis no default label anywhere in the switch body, no part of the switch body isprocessed.When control passes in programming to a statement in the switch body, controlonly leaves the switch body when a break statement is encountered or the laststatement in the switch body is processed.If necessary, in programming an integral promotion is performed on the controllingexpression, and all expressions in the case statements are converted to the sametype as the controlling expression. The switch expression can also be of classtype if there is a single conversion to integral or enumeration type.Compiling with option -qinfo=gen finds case labels that fall through when theyshould not. http://eglobiotraining.com/
  • 4.  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 testvarious conditions using single if...else if statement. When usingif , else if , else statements in the programming languages thereare few points to keep in mind. An if can have zero or one elsesand it must come after any else ifs. An if can have zero to many else ifs and they must come before the else. Once an else ifsucceeds, none of he remaining else ifs or elses will be tested. http://eglobiotraining.com/
  • 5.  In programming, If the value of expression is nonzero, statement1 isexecuted. If the optional else is present, statement2 is executed if the valueof expression is zero. expression must be of arithmetic or pointer type, or itmust be of a class type that defines an unambiguous conversion to anarithmetic or pointer type. In both forms of the if statement, expression, whichcan have any value except a structure, is evaluated, including all side effects.In the programming control passes from the if statement to the next statementin 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 closestprevious if statement in the same scope that does not have acorresponding else statement. For this sample to be unambiguousabout if...else pairing, uncomment the braces. http://eglobiotraining.com/
  • 6. C++ Programming if else statement If-else statementIt is similar to if statement i.e. It is also used to execute or ignore a set ofstatements after testing a condition in programming language. In if-else statement one condition and two blocks of statements aregiven. First blocks contain if statement with condition and its body part. Second is else and it contain only body part. http://eglobiotraining.com/
  • 7. Explanation working of if-else statement in programming languages A condition is a logical or relational expression and it produces eithertrue 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 thesecond block of statement is executed. After the executing the second blockof statement the control is transferred to next statement after if-else structure. http://eglobiotraining.com/
  • 8. #include<iostream> #include<stdlib.h> using namespace std; int main () {int number_of_units, tuitionfee, paymentforunits;cout<< "number of units you are to enroll:nn"; cin>>number_of_units; paymentforunits=number_of_units*150; cout<<"payment for units:nn"<<paymentforunits; tuitionfee=paymentforunits+15%+20; cout<<"tuition fee:nn"<<tuitionfee; system(“PAUSE"); return 0; } http://eglobiotraining.com/
  • 9. #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<<"nnn"; system(“PAUSE"); return 0; } http://eglobiotraining.com/
  • 10. #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/
  • 11. #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/
  • 12. #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/
  • 13. LOOPING STATEMENT In computer science a for loop is a programming language statement which allowscode to be repeatedly executed. A for loop is classified as an iteration statement. Unlikemany other kinds of loops, such as the while loop, the for loop is often distinguished by anexplicit loop counter or loop variable. This allows the body of the for loop to know about thesequencing of each iteration. For loops are also typically used when the number of iterationsis known before entering the loop. In programming loops are the shorthand way to makeloops 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 mostprogramming languages to introduce a for loop. The loop body is executed "for" the givenvalues of the loop variable, though this is more explicit in the ALGOL version of thestatement, 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 isotherwise identical to the for loop described here and is not to be confused with the Do whileloop. http://eglobiotraining.com/
  • 14.  In computer programming, a loop is a sequence of instruction s that iscontinually repeated until a certain condition is reached. Typically, a certainprocess is done, such as getting an item of data and changing it, and thensome condition is checked such as whether a counter has reached aprescribed number. If it hasnt, the next instruction in the sequence is aninstruction to return to the first instruction in the sequence and repeat thesequence. If the condition has been reached, the next instruction "falls through"to the next sequential instruction or branches outside the loop. A loop is afundamental programming idea that is commonly used in writing programs. http://eglobiotraining.com/
  • 15. KINDS OF FOR LOOPS A for loop statement is available in most imperative programming languages. Evenignoring minor differences in syntax there are many differences in how these statementswork and the level of expressiveness they support. Generally, for loops fall into one of thefollowing categories:Iterator-based for loopsIn 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 isusually 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/
  • 16.  Vectorised for loops Some languages of programming offer a for loop that acts as if processing alliterations 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 codefragment, 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 inthe previous step. In the for all version, however, each calculation refers only to the original, unaltered A. http://eglobiotraining.com/
  • 17.  Compound for loopsIn 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-loops execution stops short. Granted that for programming the loop variables value is defined after the termination of the loop, then the above statement will find the firstnon-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/
  • 18.  while ( expression ) statementIn programming a while loop, the expression is evaluated. If nonzero,the statement executes, and the expression is evaluated again. This happens over and overuntil the expressions value is zero. If the expression is zero the first time it isevaluated, 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 statementexecutes before the expression is evaluated. Thus, the statement in programminglanguages will always be evaluated at least once. for ( expression1; expression2; expression3 )statementIn 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 anyof 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 loopthat must be exited using the break statement in the programming. http://eglobiotraining.com/
  • 19. #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/
  • 20. #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<<"Youve got it!"<<endl; system("pause"); return (0); } http://eglobiotraining.com
  • 21. #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/
  • 22. #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<<"Youve got it!"<<endl; system("pause"); return (0); } http://eglobiotraining.com
  • 23. #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/
  • 24. THE FINAL REQUIREMENT FOR FUNDAMENTALS OF PROGRAMMINGSubmitted to : Prof. Erwin Globio Sumbitted by : Xhyna Mea Delfin

×