Powerpoint presentation final requirement in fnd prg

26
Final Requirement in Programming Castro, Alyssa S. FM09205 http://eglobiotraining.com

Transcript of Powerpoint presentation final requirement in fnd prg

Page 1: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Final Requirement in Programming

Castro, Alyssa S.FM09205

Page 2: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Switch Case and Looping StatementsFundamentals In Programming

Page 3: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

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. Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multi way branch (or "goto", one of several labels).

The main reasons for using a switch statement in programming is to 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.

It is 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).

In computer programming, 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.

Page 4: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Looping Statement

A loop (in programming) is a sequence of statements which is specified once but which may be carried out several times in succession. The code "inside" the loop (the body of the loop, shown below as xxx) is obeyed a specified number of times, or once for each of a collection of items, or until some condition is met, or indefinitely.

In functional programming languages, such as Haskell and Scheme, loops can be expressed by using recursion or fixed point iteration rather than explicit looping constructs. Tail recursion is a special case of recursion which can be easily transformed to iteration.

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

Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming.

Page 5: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Source Code of Switch Case Statement 1

#include <iostream>

using namespace std;

void playgame(){ cout << "Play game called";}void loadgame(){ cout << "Load game called";}void playmultiplayer(){ cout << "Play multiplayer game called";}

int main(){ int input; cout<<"1. Play game\n"; cout<<"2. Load game\n"; cout<<"3. Play multiplayer\n"; cout<<"4. Exit\n"; cout<<"Selection: "; cin>> input; switch ( input ) { case 1: // Note the colon, not a semicolon playgame(); break; case 2: // Note the colon, not a semicolon loadgame(); break; case 3: // Note the colon, not a semicolon playmultiplayer(); break; case 4: // Note the colon, not a semicolon cout<<"Thank you for playing!\n"; break; default: // Note the colon, not a semicolon cout<<"Error, bad input, quitting\n"; break; } cin.get();}

Screen Shot of Example 1

Page 6: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Explanation of Example 1

The first example of switch case statement in programming allows the user to input a number that states the functions in a game. If the user types the number before each chosen function [e.g 1 (play game)], the running program will show the words “thank you for playing”.

Page 7: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Source Code of Switch Case Statement 2

#include <iostream> #include <stdlib.h>

using namespace std; void welcome(); int getInteger(); void displayResponse(int choice);

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

Screen Shot of Example 2

Page 8: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Explanation of Example 2 The second example of switch case

statement in programming displays different messages depending on which number is entered by the user. Pick a number between 1 and 6 and see what the program will say.

Page 9: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Source Code of Switch Case Statement 3

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

Screen Shot of Example 3

Page 10: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Explanation of Example 3

The third example of switch case statement in programming displays different messages depending on which letter is chosen by the user. Pick a, b or c and see what the program will say.

Page 11: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Source Code of Switch Case Statement 4

// // Demonstrates switch statement

#include <iostream.h>

int main() { unsigned short int number; cout << "Enter a number between 1 and 5: "; cin >> number; switch (number) { case 0: cout << "Too small, sorry!"; break; case 5: cout << "Good job!\n"; // fall through case 4: cout << "Nice Pick!\n"; // fall through case 3: cout << "Excellent!\n"; // fall through case 2: cout << "Masterful!\n"; // fall through case 1: cout << "Incredible!\n"; break; default: cout << "Too large!\n"; break; } cout << "\n\n"; return 0; }

Screen Shot of Example 4

Page 12: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Explanation of Example 4 This fourth example of switch case

statement in programming asks the user to pick a number ranging from 1 to 5. Switch Case in programming is effective to any program especially in multiple choices.

Page 13: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Source Code of Switch Case Statement 5

#include <iostream>

using namespace std;

int main ()

{

int score;

cout << "What was your score?";

cin >> score;

if (score <= 25)

{

cout << "\nOuch, less than 25...!";

}

else if (score <= 50)

{

cout << "\nYou score aint great mate..";

}

else if (score <= 75)

{

cout << "\nYour pretty good, wel done man!";

}

else if (score <= 100)

{

cout << "\nYou got to the top!!!";

}

else

{

cout << "\nYou cant score higher than 100!!! Cheater!!!!";

}

cin.ignore();

cin.get();

return 0;

}

Screen Shot of Example 5

Page 14: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Explanation of Example 5

This fifth example of switch case statement in programming allows the user to input a score from 0 to 100. This programming sample is like having a feedback on the score you got (let’s say, from an exam).

Page 15: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Source Code of Looping Statement 1

#include <iostream>

using namespace std; // So the program can see cout and endl

int main(){ // 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();}

Screen Shot of Example 1

Page 16: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Explanation of Example 1 This first example of looping in

computer programming is an example of FOR loop.

For loop is said to be the most useful type in programming.

Page 17: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Source Code of Looping Statement 2

#include <iostream>

using namespace std; // So we can see cout and endl

int main(){ int x = 0; // Don't forget to declare variables while ( x < 10 ) { // While x is less than 10 cout<< x <<endl; x++; // Update x so the condition can be met eventually } cin.get();}

Screen Shot of Example 2

Page 18: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Explanation of Example 2

This second example of looping in programming is just another simple loop program.

A loop is a type in computer programming that is usually known through being a program that checks the whole process by reaching the end then jumping back again to the beginning.

Page 19: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Source Code of Looping Statement 3

#include <iostream>

using namespace std;

int main(){ 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();}

Screen Shot of Example 3

Page 20: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Explanation of Example 3 This third example of looping in

programming shows at least on print of “Hello World!” even if the condition is false.

Page 21: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Source Code of Looping Statement 4

#include <iostream>

int main(){ using namespace std;

// nSelection must be declared outside do/while loop int nSelection;

do { cout << "Please make a selection: " << endl; cout << "1) Addition" << endl; cout << "2) Subtraction" << endl; cout << "3) Multiplication" << endl; cout << "4) Division" << endl; cin >> nSelection; } while (nSelection != 1 && nSelection != 2 && nSelection != 3 && nSelection != 4);

// do something with nSelection here // such as a switch statement

return 0;}

Screen Shot of Example 4

Page 22: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Explanation of Example 4

This fourth example of looping in programming is a given sample of a DO WHILE loop.

These type of loops are useful in computer programming.

Page 23: Powerpoint presentation final requirement in fnd prg

Source Code of Looping Statement 5

#include <iostream>using namespace std;int main (){int x;cout << "Input the number:";cin >> x;while (x>0) {cout << x << "; ";--x;}cout << "EXFORSYS";}

http://eglobiotraining.com

Screen Shot of Example 5

Page 24: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Explanation of Example 5 This fifth example of looping in

programming tells the user to input a number. And once the user clicked enter, the word “exforsys” appears.

Page 25: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

URL:

Page 26: Powerpoint presentation final requirement in fnd prg

http://eglobiotraining.com

Prof. Erwin M. Globio

http://eglobiotraining.com/

Submitted to: