TOPICS IN PROGRAMMING      http://eglobiotraining.com
SWITCH CASE                 STATEMENT    0 In programming, a switch, case, select or inspect stateme     nt is a type of s...
SWITCH CASE                   STATEMENT    0 Switch case statements are a substitute for long if statements      that comp...
SWITCH CASE                 STATEMENT    0 Switch is used to choose a fragment of template      depending on the value of ...
TO                                        NEXTP           http://eglobiotraining.comI   BACKC
TOPI   BACK   http://eglobiotraining.com   NEXTC
Example of switch case IN C        PROGRAMMING    #include <iostream>                                                     ...
Running switch case IN C       PROGRAMMINGTOPI           http://eglobiotraining.comC                                      ...
LOOPING    There may be a situation when you need to execute a block    of code several number of times. In general statem...
LOOPING    C++ programming language provides    following types of loop to handle    looping requirements:TOPI            ...
T             "FOR” LOOPOPIC       0 A for loop is a repetition control structure that allows         you to efficiently w...
TOPIC      EXAMPLE OF FOR LOOPING IN           C PROGRAMMING #include <iostream> #include <cmath>                         ...
TOPIC    RUNNING FOR LOOP IN IN       C PROGRAMMING           http://eglobiotraining.comLOOP                              ...
“WHILE” LOOPTOPIC       0 The while loop allows programs to repeat a statement         or series of statements, over and o...
TOPIC    EXAMPLE OF WHILE LOOP      IN C PROGRAMMING       #include <iostream.h>       int main(void) {           int x = ...
TOPIC   RUNNING WHILE LOOP IN       C PROGRAMMINGLOOP       http://eglobiotraining.com                                    ...
TOP               “DO WHILE” LOOPIC       0 In most computer programming languages, a do while         loop, sometimes jus...
TOP               “DO WHILE” LOOPIC       0 Unlike for and while loops, which test the loop         condition at the top o...
TOPIC        EXAMPLE OF A DO WHILE       LOOP IN C PROGRAMMING       0   #include <iostream>       0   using namespace std...
TOPIC    RUNNING DO WHILE LOOP IN C          PROGRAMMING             http://eglobiotraining.comLOOP                       ...
http://www.slideshare.net/ni        colecastro21          http://eglobiotraining.com
Final requirement
Final requirement
Upcoming SlideShare
Loading in …5
×

Final requirement

218 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
218
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Final requirement

  1. 1. TOPICS IN PROGRAMMING http://eglobiotraining.com
  2. 2. SWITCH CASE STATEMENT 0 In programming, a switch, case, select or inspect stateme nt 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 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 "go to", 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.TOPI http://eglobiotraining.com NEXTC
  3. 3. SWITCH CASE STATEMENT 0 Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using the switch case in the programming is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point. 0 The switch-case statement is a multi-way decision statement. Unlike the multiple decision statement that can be created using if-else, the switch statement evaluates the conditional expression and tests it against numerous constant values. The branch corresponding to the value that the expression matches is taken during execution.TOPI BACK http://eglobiotraining.com NEXTC
  4. 4. SWITCH CASE STATEMENT 0 Switch is used to choose a fragment of template depending on the value of an expression 0 This has a similar function as the If condition - but it is more useful in situations when there is many possible values for the variable. Switch will evaluate one of several statements, depending on the value of a given variable. If no given value matches the variable, the default statement is executed. 0 The value of the expressions in a switch-case statement must be an ordinal typeT i.e. integer, char, short, long, etc. Float and double areOP not allowed.I BACK http://eglobiotraining.com NEXTC
  5. 5. TO NEXTP http://eglobiotraining.comI BACKC
  6. 6. TOPI BACK http://eglobiotraining.com NEXTC
  7. 7. Example of switch case IN C PROGRAMMING #include <iostream> // switch statement based on the choice variable #include <stdlib.h> using namespace std; switch (choice) // notice no semicolon void welcome(); { char getChar(); case A: // choice was the letter A void displayResponse(char choice); int main(int argc, char *argv[]) case a: // choice was the letter a { cout << "your awesome dude.nn"; char choice; // declares the choice variable break; // this ends the statements for case A/a welcome(); // This calls the welcome function choice = getChar(); // calls getChar and returns the value for choice case B: // choice was the letter b displayResponse(choice); // passes choice to displayResponse function case b: // choice was the letter b cout << "you will find your lovelife.nn"; system("PAUSE"); return 0; break; // this ends the statements for case B/b } // end main case C: // choice was the letter C // welcome function displays an opening message to case c: // choice was the letter c // explain the program to the user void welcome() cout << "your will won the lottery.nn"; { break; // this ends the statements for case C/c cout << "This program displays different messages dependingn"; case D: // choice was the letter D cout << "on which letter is entered by the user.n"; cout << "Pick a letter a, b, c or d to see whatn"; case d: // choice was the letter d cout << "the program will say.nn"; cout << "your so ugly!!.nn"; } // end of welcome function break; // this ends the statements for case D/d // getChar asks the user for a letter a, b or c. // The character is returned to where the function was called. default: // used when choice falls out of the cases covered char getChar() above { cout << "You didnt pick a letter a, b or c.nn"; char response; // declares variable called response cout << "Please type a letter a, b, c and d: "; // prompt for letter again = getChar(); // gives the user another try cin >> response; // gets input from user and assigns it to response displayResponse(again); // recalls displayResponse with new return response; // sends back the response value character } // end getChar function break; // displayResponse function takes the char variable and uses it // to determine which set of tasks will be performed. } // end of switch statement void displayResponse(char choice) } // end displayResponse function { char again;TOPI BACK http://eglobiotraining.com NEXTC
  8. 8. Running switch case IN C PROGRAMMINGTOPI http://eglobiotraining.comC BACK
  9. 9. LOOPING There may be a situation when you need to execute a block of code several number of times. In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programmingT languages.OPI LOOP http://eglobiotraining.comC NEXT
  10. 10. LOOPING C++ programming language provides following types of loop to handle looping requirements:TOPI http://eglobiotraining.comC BACK
  11. 11. T "FOR” LOOPOPIC 0 A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. 0 The statements in the for loop repeat continuously for aspecific number of times. The while and do- while loops repeat until a certain condition is met. The for loop repeats until a specific count is met. Use a for loop when the number of repetition is know, or can be supplied by the user. http://eglobiotraining.comLOOP BACK NEXT
  12. 12. TOPIC EXAMPLE OF FOR LOOPING IN C PROGRAMMING #include <iostream> #include <cmath> } using namespace std; cout <<"nSeconds //prototype falling distancen"; int fallingdistance(); cout <<"---------------------------------------n"; for ( count = 1; count <= time; count++) //main function { int main() distance = .5 * 9.8 * { pow(time, 2.0); int count = 1 ; cout << count << " int time; " << distance <<" meters"<< endl; double distance ; } cout << "Please enter time in 1 system ("pause"); through 10 seconds.nn"; return 0; } time = fallingdistance(); // falling distance function for a return value in seconds transfer to time while ( time < 1 || time > 10) int fallingdistance () { { cout << "Must enter between 1 and int seconds; NEXT 10 seconds, please re-enter.n"; cin >> seconds; time = fallingdistance(); http://eglobiotraining.comLOOP return seconds; BACK }
  13. 13. TOPIC RUNNING FOR LOOP IN IN C PROGRAMMING http://eglobiotraining.comLOOP BACK
  14. 14. “WHILE” LOOPTOPIC 0 The while loop allows programs to repeat a statement or series of statements, over and over, as long as a certain test condition is true. 0 The while loop can be used if you don’t know how many times a loop must run. 0 A while loop statement repeatedly executes a target statement as long as a given condition is true. BACK http://eglobiotraining.comLOOP NEXT
  15. 15. TOPIC EXAMPLE OF WHILE LOOP IN C PROGRAMMING #include <iostream.h> int main(void) { int x = 0; int y = 0; bool validNumber = false; while (validNumber == false) { cout << "Please enter an integer between 1 and 10: "; cin >> x; cout << "You entered: " << x << endl << endl; if ((x < 1) || (x > 10)) { cout << "Your value for x is not between 1 and 10!" << endl; cout << "Please re-enter the number!" << endl << endl; } else validNumber = true; } cout << "Thank you for entering a valid number!" << endl; return 0; 0 } BACK http://eglobiotraining.comLOOP NEXT
  16. 16. TOPIC RUNNING WHILE LOOP IN C PROGRAMMINGLOOP http://eglobiotraining.com BACK
  17. 17. TOP “DO WHILE” LOOPIC 0 In most computer programming languages, a do while loop, sometimes just called a while loop, is a control flow statement that allows code to be executed once based on a given Boolean condition. 0 The do while construct consists of a process symbol and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false. Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the while loop, which tests the condition before the code within the block is executed.LOOP BACK http://eglobiotraining.com NEXT
  18. 18. TOP “DO WHILE” LOOPIC 0 Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop. 0 A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time. 0 The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. Having the test condition at the end, guarantees that the body of the loop always executes at least one time.LOOP http://eglobiotraining.com BACK NEXT
  19. 19. TOPIC EXAMPLE OF A DO WHILE LOOP IN C PROGRAMMING 0 #include <iostream> 0 using namespace std; 0 main() 0 { int num1, num2; 0 char again = y; 0 while (again == y || again == Y) { 0 cout << "Enter a number: "; 0 cin >> num1; 0 cout << "Enter another number: "; 0 cin >> num2; 0 cout << "Their sum is " << (num1 + num2) << endl; 0 cout << "Do you want to do this again? "; 0 cin >> again; } 0 return 0; 0 }LOOP BACK http://eglobiotraining.com NEXT
  20. 20. TOPIC RUNNING DO WHILE LOOP IN C PROGRAMMING http://eglobiotraining.comLOOP BACK
  21. 21. http://www.slideshare.net/ni colecastro21 http://eglobiotraining.com

×