48
FINAL REQUIREMENT IN PROGRAMMING Del Rosario, Trisha Maxine A. h t t p : / / e g l o b i o t r a i n i n g . c o m

Final requirement in programming

Embed Size (px)

Citation preview

Page 1: Final requirement in programming

http

://eglo

bio

train

ing.co

mFINAL REQUIREMENT IN PROGRAMMINGDel Rosario, Trisha Maxine A.

Page 2: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SWITCH CASE AND LOOPING

Page 3: Final requirement in programming

http

://eglo

bio

train

ing.co

m

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, 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 multiway branch (or "goto", one of several labels).

Page 4: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SWITCH CASE The main reasons for using a switch in

programming 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 5: Final requirement in programming

http

://eglo

bio

train

ing.co

m

THE BASIC FORMAT FOR USING SWITCH CASE IS OUTLINED BELOW.

switch ( <variable> ) {case this-value: Code to execute if <variable> == this-value

break;case that-value: Code to execute if <variable> == that-value

break;...default: Code to execute if <variable> does not equal the value

following any of the cases break;}

Page 6: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SWITCH CASE In computer programming, the condition of a switch

statement is a value. The case says that if it has the value of whatever is after that case then do whatever follows the colon. The break is used to break out of the case statements. Break as one of the language used in programming is a keyword that breaks out of the code block, usually surrounded by braces, which it is in.

In this case, break prevents the program from falling through and executing the code in all the other case statements. An important thing to note about the switch statement is that the case values may only be constant integral expressions.

It can be useful to put some kind of output to alert you to the code entering the default case if you don't expect it to. Switch statements serve as a simple way to write long if statements when the requirements of programming are met. Often it can be used to process input from a user. 

Page 7: Final requirement in programming

http

://eglo

bio

train

ing.co

m

ACTUAL SOURCE CODE OF SWITCH CASE#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();}

Page 8: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SCREEN SHOTS OF OUTPUT PROGRAM 1

Page 9: Final requirement in programming

http

://eglo

bio

train

ing.co

m

EXPLANATION

In this program, the user will select if he wants to play, load, play multiplayer or close the game based on the number indicated in the output program of the programming software.

Page 10: Final requirement in programming

http

://eglo

bio

train

ing.co

m

ACTUAL SOURCE CODE OF PROGRAM 2#include <stdlib.h>#include <stdio.h>

int main(void) { int n; printf("Please enter a number: "); scanf("%d", &n); switch (n) { case 1: { printf("n is equal to 1!\n"); break; } case 2: {printf("n is equal to 2!\n");

break; }

case 3: {

printf("n is equal to 3!\n"); break; } default:

{ printf("n isn't equal to 1, 2,

or 3.\n"); break; } } system("PAUSE"); return 0;}

Page 11: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SCREEN SHOTS OF OUTPUT PROGRAM 2

Page 12: Final requirement in programming

http

://eglo

bio

train

ing.co

m

EXPLANATION #include <iostream>

- This tells the compiler to include files in using dev c++ of programming.

#include <stdlib.h> - This tells the compiler to include files.

int main(int argc, char *argv[]) - This starts the main function use in programming.

This 2nd example program that I did for the requirement in programming will ask the user to select a number. After entering the number, the programming software which is the dev c++ program will print if the entered number is equal to 1, 2 or 3. It will print different things on the screen depending on which number the user chose.

Page 13: Final requirement in programming

http

://eglo

bio

train

ing.co

m

ACTUAL SOURCE CODE OF PROGRAM 3 #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; }

Page 14: Final requirement in programming

http

://eglo

bio

train

ing.co

m

OUTPUT PROGRAM 3

Page 15: Final requirement in programming

http

://eglo

bio

train

ing.co

m

EXPLANATION

This 3rd program of programming will ask the user to select a number between 1 and 5. Then the program will print different things on the screen depending on which number the user chose. The switch statement can be very helpful in handling multiple choices in programming.

Page 16: Final requirement in programming

http

://eglo

bio

train

ing.co

m

ACTUAL SOURCE CODE OF PROGRAM 4#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)

Page 17: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SOURCE CODE{ 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

Page 18: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SCREEN SHOTS OF OUTPUT PROGRAM 4

Page 19: Final requirement in programming

http

://eglo

bio

train

ing.co

m

EXPLANATION

This is the 4th example that I included in my final requirement in programming. This program displays different messages depending on which number is entered by the user. The user will be asked to pick a number between 1 and 6 to see what the program will say. Then, after the user enter the number, the programming software will print if it is an even or odd number.

Page 20: Final requirement in programming

http

://eglo

bio

train

ing.co

m

ACTUAL SOURCE CODE OF PROGRAM 5#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)

Page 21: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SOURCE CODE{ 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

Page 22: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SCREENSHOTS OF OUTPUT PROGRAM 5

Page 23: Final requirement in programming

http

://eglo

bio

train

ing.co

m

EXPLANATION

The final program of the requirement in programming project displays different messages depending on which letter is entered by the user. The user will be asked to pick a letter a, b or c to see what the program will say. Then, after the user enter the number, the programming software which is the dev c++ will print if it is an even or odd number.

Page 24: Final requirement in programming

http

://eglo

bio

train

ing.co

m

LOOPING STATEMENT

Page 25: Final requirement in programming

http

://eglo

bio

train

ing.co

m

LOOPING STATEMENT

In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is reached.

Typically, a certain process in programming 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 used in programming 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.

Page 26: Final requirement in programming

http

://eglo

bio

train

ing.co

m A loop is a fundamental programming idea that is

commonly used in writing programs. In object-oriented programming language,

whenever a block of statements has to be repeated a certain number of times or repeated until a condition becomes satisfied, the concept of looping is used.

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.

One Caveat: before going further, you should understand the concept of C++'s true and false, because it will be necessary when working with loops (the conditions are the same as with if statements). There are three types of loops: for, while, and do..while. Each of them has their specific uses.

Page 27: Final requirement in programming

http

://eglo

bio

train

ing.co

m

The following commands used in C++ for achieving looping: for loop while loop do-while loop

Page 28: Final requirement in programming

http

://eglo

bio

train

ing.co

m

FOR LOOPFOR - for loops are the most useful type in programming. The syntax for a for loop is 

for ( variable initialization; condition; variable update ) { Code to execute while the condition is true}

The variable initialization allows you to either declare a variable and give it a value or give a value to an already existing variable. Second, the condition tells the program that while the conditional expression is true the loop should continue to repeat itself. The variable update section is the easiest way for a for loop to handle changing of the variable. It is possible to do things like x++, x = x + 10, or even x = random ( 5 ), and if you really wanted to, you could call other functions that do nothing to the variable but still have a useful effect on the code.

Notice that a semicolon separates each of these sections, that is important. Also note that every single one of the sections may be empty, though the semicolons still have to be there. If the condition is empty, it is evaluated as true and the loop will repeat until something else stops it. This is one of the important factors of a programming language.

Page 29: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SOURCE CODE

#include <stdio.h>

int main(){ int x; /* The loop goes while x < 10, and x increases by one every

loop*/ for ( 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. */ printf( "%d\n", x ); } getchar();}

Page 30: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SCREENSHOTS

Page 31: Final requirement in programming

http

://eglo

bio

train

ing.co

m

EXPLANATION The variable initialization used in programming allows

you to either declare a variable and give it a value or give a value to an already existing variable. Second, the condition tells the program that while the conditional expression is true the loop should continue to repeat itself. The variable update section is the easiest way for a for loop to handle changing of the variable. It is possible to do things like x++, x = x + 10, or even x = random ( 5 ), and if you really wanted to, you could call other functions that do nothing to the variable but still have a useful effect on the code. Notice that a semicolon separates each of these sections, that is important. Also note that every single one of the sections may be empty, though the semicolons still have to be there. If the condition in programming is empty, it is evaluated as true and the loop will repeat until something else stops it. 

Page 32: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SOURCE CODE

#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();}

Page 33: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SCREENSHOTS

Page 34: Final requirement in programming

http

://eglo

bio

train

ing.co

m

EXPLANATION

This program is a very simple example of a for loop. x is set to zero, while x is less than 10 it calls cout<< x <<endl; and it adds 1 to x until the condition is met. Keep in mind also that the variable of a programming language is incremented after the code in the loop is run for the first time.

Page 35: Final requirement in programming

http

://eglo

bio

train

ing.co

m

WHILE LOOP WHILE - WHILE loops are very simple. The basic

structure is 

while ( condition ) { Code to execute while the condition is true } The true represents a boolean expression which could be x == 1 or while ( x != 7 ) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x ==5 || v == 7) which says execute the code while x equals five or while v equals 7.

Notice that a while loop is the same as a for loop without the initialization and update sections.

However, an empty condition is not legal for a while loop as it is with a for loop. 

Page 36: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SOURCE CODE

#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();}

Page 37: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SCREENSHOTS

Page 38: Final requirement in programming

http

://eglo

bio

train

ing.co

m

EXPLANATION

This was another simple example, but it is longer than the above FOR loop. The easiest way to think of the loop is that when it reaches the brace at the end it jumps back up to the beginning of the loop, which checks the condition again and decides whether to repeat the block another time, or stop and move to the next statement after the block. 

Page 39: Final requirement in programming

http

://eglo

bio

train

ing.co

m

DO-WHILE LOOP

DO..WHILE - DO..WHILE loops are useful for things in programming that want to loop once. The structure is

do {} while ( condition );

Notice that the condition is tested at the end of the block instead of the beginning, so the block will be executed at least once. If the condition is true, we jump back to the beginning of the block and execute it again. A do..while loop is basically a reversed while loop. A while loop says "Loop while the condition is true, and execute this block of code", a do..while loop says "Execute this block of code, and loop while the condition is true". 

Page 40: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SOURCE CODE

#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();}

Page 41: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SCREEN SHOTS

Page 42: Final requirement in programming

http

://eglo

bio

train

ing.co

m

EXPLANATION

In this example, once you compile and run the source codes you did, the programming software will print “Hello, world!” even though the condition is false.

Page 43: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SOURCE CODE

#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();}

Page 44: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SCREEN SHOTS

Page 45: Final requirement in programming

http

://eglo

bio

train

ing.co

m

EXPLANATION

Keep in mind that you must include a trailing semi-colon after the while in the above example. A common error in programming is to forget that a do..while loop must be terminated with a semicolon (the other loops should not be terminated with a semicolon, adding to the confusion). Notice that this loop will execute once, because it automatically executes before checking the condition. 

Page 46: Final requirement in programming

http

://eglo

bio

train

ing.co

m

URL :http://www.slideshare.net/upload

Page 47: Final requirement in programming

http

://eglo

bio

train

ing.co

m

SUBMITTED TO: PROF. ERWIN M GLOBIOhttp://eglobiotraining.com/

Page 48: Final requirement in programming

http

://eglo

bio

train

ing.co

m

THANK YOU!