Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Requirement in fundamentals of programming

1.
Switch Case and Looping Statements http://eglobiotraining.com

2.
 In a the world of programming, switch case statement is a multi-way decision statement. It 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. 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 value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char, short, long, etc. in programming. Float and double are not allowed in this type of programming. http://eglobiotraining.com

3.
#include <iostream>#include <stdlib.h> // to determine which set of tasks will be performed.using namespace std; void displayResponse(int choice)void welcome(); {int getInteger(); int again;void displayResponse(int choice); // switch statement based on the choice variableint main(int argc, char *argv[]) switch (choice) // notice no semicolon{ {int choice; // declares the choice variable case 1: // choice was the number 1welcome(); // This calls the welcome function cout << "One is a lonely number and very useful in math.nn";choice = getInteger(); // calls getInteger and receives the value for choice break; // this ends the statements for case 1displayResponse(choice); // passes choice to displayResponse function case 2: // choice was the number 2system("PAUSE"); cout << "Two is the only even prime number.nn";return 0; break; // this ends the statements for case 2} // end main case 3: // choice was the number 3// welcome function displays an opening message to cout << "Three is a crowd and also a prime number.nn";// explain the program to the user break; // this ends the statements for case 3void welcome() case 4: // choice was the number 4{ cout << "Four square is a fun game to play, but four squared is ";cout << "This program displays different messages dependingn"; cout << 4 * 4 << ".nn";cout << "on which number is entered by the user.n"; break; // this ends the statements for case 4cout << "Pick a number between 1 and 6 to see whatn"; case 5: // choice was the number 5cout << "the program will say.nn"; cout << "Counting by fives is fun. Five, Ten, Fifteen, Twenty...nn";} // end of welcome function break; // this ends the statements for case 5// getInteger asks the user for a number between 1 and 6. case 6: // choice was the number 6// The integer is returned to where the function was called. cout << "Six is divisible by two and three.nn";int getInteger() break; // this ends the statements for case 6{ default: // used when choice falls out of the cases covered aboveint response; // declares variable called response cout << "You didnt pick a number between 1 and 6.nn";cout << "Please type a number between 1 and 6: "; // prompt for number again = getInteger(); // gives the user another trycin >> response; // gets input from user and assigns it to response displayResponse(again); // recalls displayResponse with new numberreturn response; // sends back the response value break;} // end getInteger function } // end of switch statement// displayResponse function takes the int variable and uses it } // end displayResponse function http://eglobiotraining.com

5.
 This program displays different messages depending on which number is entered by the user. Pick a number between 1 to 6 to see what the program will say. This is executed in the C++ programming software. http://eglobiotraining.com/

6.
#include <iostream> return response; // sends back the response value#include <stdlib.h> } // end getChar functionusing namespace std; // displayResponse function takes the char variable and uses itvoid welcome(); // to determine which set of tasks will be performed.char getChar(); void displayResponse(char choice)void displayResponse(char choice); {int main(int argc, char *argv[]) char again;{ // switch statement based on the choice variablechar choice; // declares the choice variable switch (choice) // notice no semicolonwelcome(); // This calls the welcome function {choice = getChar(); // calls getChar and returns the value for choice case A: // choice was the letter AdisplayResponse(choice); // passes choice to displayResponse function case a: // choice was the letter asystem("PAUSE"); cout << "A is for apple.nn";return 0; break; // this ends the statements for case A/a} // end main case B: // choice was the letter b// welcome function displays an opening message to case b: // choice was the letter b// explain the program to the user cout << "B is for baseball.nn";void welcome() break; // this ends the statements for case B/b{ case C: // choice was the letter Ccout << "This program displays different messages dependingn"; case c: // choice was the letter ccout << "on which letter is entered by the user.n"; cout << "C is for cat.nn";cout << "Pick a letter a, b or c to see whatn"; break; // this ends the statements for case C/ccout << "the program will say.nn"; default: // used when choice falls out of the cases covered above} // end of welcome function cout << "You didnt pick a letter a, b or c.nn";// getChar asks the user for a letter a, b or c. again = getChar(); // gives the user another try// The character is returned to where the function was called. displayResponse(again); // recalls displayResponse with new characterchar getChar() break;{ } // end of switch statement } // end displayResponse functionchar response; // declares variable called responsecout << "Please type a letter a, b or c: "; // prompt for lettercin >> response; // gets input from user and assigns it to response http://eglobiotraining.com

8.
 This program displays different messages depending on which letter is entered by the user. Pick a letter: a, b or c to see what the program will say. This is executed in the C++ programming software. http://eglobiotraining.com/

11.
 This program will compile, but cannot be run until the undefined functions are given bodies, but it serves as a model in computer programming (albeit simple) for processing input. If you do not understand this then try mentally putting in if statements for the case statements. Default simply skips out of the switch case programming construction and allows the program to terminate naturally. If you do not like that kind of programming scheme, then you can make a loop around the whole thing to have it wait for valid input. You could easily make a few small functions if you wish to test the code. http://eglobiotraining.com/

14.
 In this program, the user will be asked to type number 1 or 2. When the user types in 1 or 2, the program will display either of the two numbers. This is executed in the C++ programming software. http://eglobiotraining.com/

17.
 In programming, the user is prompted for a number. That number is given to the switch statement. If the number is 0, the case statement on line 13 matches, the message Too small, sorry! is printed, and the break statement ends the switch. If the value is 5, execution switches to line 15 where a message is printed, and then falls through to line 16, another message is printed, and so forth until hitting the break on line 20. The net effect of these statements in this programming is that for a number between 1 and 5, that many messages are printed. If the value of number is not 0-5, it is assumed to be too large, and the default statement in programming is invoked on line 21. http://eglobiotraining.com

18.
 while ( expression ) statement In a while loop of computer programming, the expression is evaluated. If nonzero, the statement executes, and the expression is evaluated again. This happens over and over until the expressions value is zero. If the expression is zero the first time it is evaluated, statement is not executed at all. do statement while ( expression); A do while loop is just like a plain while loop, except the statement executes before the expression is evaluated. Thus, the statement will always be evaluated at least once in programming. http://eglobiotraining.com

19.
 for ( expression1; expression2; expression3 ) statement In a for loop in computer programming, 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 is executed, expression3 is evaluated, and expression2 is evaluated again, continuing until expression2 is zero. You can omit any of the expressions in programming. 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 http://eglobiotraining.com

22.
 In computer programming language, his code takes a value from the user and runs a while loop that many times. The conditions used for the while loop are the same as the if- then-else statements, same goes for every loop. In this programming language, since I only put "a" the program will read "While a is true execute this block" and as long as a is a positive integer it is considered to be true. http://eglobiotraining.com

25.
 In programming, the user of is prompted for a starting value on line 10. This starting value is stored in the integer variable counter. The value of counter is tested on line 12, and decremented in the body of the while loop. The first time through counter was set to 2, and so the body of the while loop ran twice. The second time through, however, the user typed in 0. The value of counter was tested on line 12 and the condition was false; counter was not greater than 0. The entire body of the while loop in programming was skipped, and Hello was never printed. http://eglobiotraining.com

28.
 Here the first for loop of programming defines i as an integer with a value of 0. Since the array is 10x10 and 0 counts when counting the elements of an array, we will run the loop of computer programming until i is equal or greater than 9. "++i" means "add 1 to i", it can be used with any numeric data type. Since the array is two dimensional we will need a second for the loop to get the second index number. This is setup the exact same way, except I used a t instead of an i. So now every time the first for loop runs in the C++ programming, the second will run 10 times, and then return to the first until the first has been run 10 times thus covering every element in the array. http://eglobiotraining.com

31.
 The user is prompted for a starting value on line 9, which is stored in the integer variable counter. In the do...while loop of computer programming, the body of the loop is entered before the condition is tested, and therefore the body of the loop is guaranteed to run at least once. On line 13 the message is printed, on line 14 the counter is decremented, and on line 15 the condition is tested. If the condition of the made programming language evaluates TRUE, execution jumps to the top of the loop on line 13; otherwise, it falls through to line 16. The continue and break statements in computer programming work in the do...while loop exactly as they do in the while loop. The only difference between a while loop and a do...while loop in computer programming is when the condition is tested. http://eglobiotraining.com

34.
 The do statement in programming is similar to the while statement except that its termination condition is at the end of the body of the loop only. Thus, you want to use a do statement in programming, if you want to perform the body of the loop at least once, regardless of the condition. http://eglobiotraining.com/