My programming final proj. (1)

on

  • 145 views

 

Statistics

Views

Total Views
145
Views on SlideShare
145
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

My programming final proj. (1) My programming final proj. (1) Presentation Transcript

  • Switch Case and Looping Statement Briñes, Aden Mae S. BM10203http://eglobiotraining.com
  • What is Programming?“programming”, it is a computer language programmers use to develop applications, scripts, or other set of instructions for a computer to execute. programming is instructing a computer to do something for you with the help of a programming language. The role of a programming language can be described in two ways: Technical: It is a means for instructing a Computer to perform Tasks Conceptual: It is a framework within which we organize our ideas about things and processes.• http://eglobiotraining.com
  • As a student, I was a bit curious about my subject but as time pass by and we had new lessons I have learned that programming is very broad because it composes many applications, codes and can be used to run a program.A programming language both provide means to describe primitive data and procedures and means to combine and abstract those into more complex ones. http://eglobiotraining.com
  • At first, I had a hard time understanding what programming is because you have so much to read and analyze about codes that will enable to run a program. Programming is a creative process done by programmers to instruct a computer on how to do a task.• http://eglobiotraining.com
  • Switch Casestatements that 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 switch case is outlined below.• http://eglobiotraining.com
  • To further understand switch casehere is the format: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.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;}• http://eglobiotraining.com
  • Condition 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 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.• http://eglobiotraining.com
  • BreakIt 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. http://eglobiotraining.com
  • The break is used to break out of the case statements. An important thing to note about the switch statement is that the case values may only be• constant integral expressions. http://eglobiotraining.com
  • Looping(Loops). They 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 -- many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times.• http://eglobiotraining.com
  • a loop lets you write a very simple statement to produce a significantly greater result simply by repetition.• http://eglobiotraining.com
  • Three types of Loops: for While and do• http://eglobiotraining.com
  • FORFor ( variable initialization; condition; variable update ) { Code to execute while the condition is true}• http://eglobiotraining.com
  • WHILE loops are very simple.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.• http://eglobiotraining.com
  • DO WHILE are useful for thingsthat want to loop at least once. do { } while ( condition ) ; • http://eglobiotraining.com
  • FOR, WHILE, DO WHILE examples • http://eglobiotraining.com
  • FOR #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(); }• http://eglobiotraining.com
  • WHILE#include <iostream>using namespace std; // So we can see cout and endlint main(){ int x = 0; // Dont 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();}• http://eglobiotraining.com
  • DO WHILE#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();}• http://eglobiotraining.com
  • The examples and their explanations • http://eglobiotraining.com
  • SWITCH CASE EXAMPLES:• http://eglobiotraining.com
  • Switch Case #1 #include <stdio.h> #include <conio.h> void1 playgame() // Creates a function/method called playgame { printf( "Play game called" ); // Displays the string found inside the statement getch(); //getch function prompts the user to press a character and that character is not printed on screen } void loadgame() // Creates a function/method called loadgame { printf( "Load game called" ); // Displays the string found inside the statement getch(); //getch function prompts the user to press a character and that character is not printed on screen } void playmultiplayer() // Creates a function/method called playmultiplayer { printf( "Play multiplayer game called" ); // Displays the string found inside the statement getch(); //getch function prompts the user to press a character and that character is not printed on screen }• http://eglobiotraining.com
  • Continuation: int main() // Creates a function/method main to be called first upon running the program { int input; // Declares a variable name input to be used printf( "1: Play gamen" ); // printf( "2: Load gamen" ); // printf( "3: Play multiplayern" ); // Displays string in order for the user to select printf( "4: Exitn" ); // printf( "Selection: " ); // scanf( "%d", &input ); // Gets the user number input switch ( input ) { // Start of the switch case statement based on the input variable case 1: /* Note the colon, not a semicolon */ playgame(); //Returns playgame function/method if 1 is the value of input variable break; case 2: loadgame(); //Returns playgame function/method if 2 is the value of input variable break; case 3: playmultiplayer(); //Returns playgame function/method if 3 is the value of input variable break; case 4: printf( "Thanks for playing!n" ); //Returns playgame function/method if 4 is the value of input variable break; default: printf( "Bad input, quitting!n" ); //Returns playgame function/method if the value of input variable is not with the range from 1-4 break; } getchar(); return 0; }• http://eglobiotraining.com
  • Explanation: /* The flow of this Dev C++ programming source code is the user is asked to select a number from the given choices. When the user has selected a number from the choices an output varies from one another. If 1 is selected, it displays a "Play game called". If 2 is selected, it displays a "Load game called". If 3 is selected, it displays a "Play multiplayer game called". If 4 is selected, it displays a "Thanks for playing!" And if none of the above are inputted by the user then "Bad input, quitting!" would be displayed. Using of functions/methods is a good way of programming. Since, you can have different process/output without crowding a switch case statement like this for example. programming using of functions/methods is not only limited to switch case statements and it could also be very useful depending on how a developer use it. */• http://eglobiotraining.com
  • Switch Case #2 #include <stdio.h> #include <conio.h> int main() // Creates a function/method main to be called first upon running the program { int color = 1; // Creates a variable name called color with a value of 1 printf("Please choose a color(1: red,2: green,3: blue):n"); scanf("%d", &color); // Gets the input number switch (color) { case 1: printf("You chose red colorn"); break; case 2: printf("You chose green colorn"); break; case 3: printf("You chose blue colorn"); break; default: printf("You did not choose any colorn"); } getche(); return 0; } /* The flow of this C++ programming example is where the program asks the user to input a number. The output also varies depending on the number inputted by the user. If 1 is selected, it displays "You chose red color". If 2 is selected, it displays "You chose green color". If 3 is selected, it displays "You did not choose any color." This is just a simple programming example about switch-case statements but a good start when it comes to programming. */
  • Switch Case #3 #include <iostream> using namespace std; int main() { char letter; cout << "A)tHouse MDn" << "B)tAmerican Idlen" << "C)tFamily Guynn"; cout << "What TV show do you like (Enter A, B, C)?: "; cin >> letter; cout << endl; switch (toupper(letter)) { case A : cout << "Dr. House is a radical doctor!nn"; break; case B : cout << "Wannabe singers!nn"; break; case C : cout << "One of the craziest cartoons Ive ever seen.nn"; break; default: cout << "Invalid choice.nn"; break; } return 0; } • http://eglobiotraining.com
  • Explanation: As you can see in this C++ programming sample source code it is a Switch-Case statement where the programs asks the user to input a letter either A, B or C and not a number. As you go along the source code you can see at line 19 which is the case A, it will be called when the user inputted letter A. On line 21, it is being called when the user inputted letter B. On line 23, it is being called when the user inputted letter C. And the default is only triggered when none of the choices are inputted by the user. Oftenly, the default is used to display an error like in this example stating that "Invalid choice".• http://eglobiotraining.com
  • Switch Case #4 #include <stdio.h> #include <iostream> using namespace std; int main() { int choice; cout << "MENUnn"; cout << "1." << "t" << "Lobstern"; cout << "2." << "t" << "Steakn"; cout << "3." << "t" << "Turkeyn"; cout << "4." << "t" << "Hambergern"; cout << "5." << "t" << "Vegetariannn"; cout << "Choose your dinner entree: "; cin >> choice; cout << endl; switch (choice) { case 1: cout << "Lobster is my favorite! Dig in!nn"; break; case 2: cout << "Yummy! Steak is great...n" << "but limit yourself to once a week," << endl << "or risk the chance of high cholesterol!nn"; break; case 3: cout << "Turkey is healthier than steak! ...Enjoy!nn"; break; case 4: cout << "Hamburger is another form of steak. :-)nn"; break;
  • Explanation: As you can see in this C++ programming sample source code is a switch case statement where the program asks an input from the user with the choices given. On case 1, it will output a string "Lobster is my favorite! Dig in!". On case 2, it will output a string "Yummy! Steak is great... but limit yourself to once a week, or risk the chance of high cholesterol!" On case 3, it will output a string "Turkey is healthier than steak! ...Enjoy!" On case 4, it will output a string "Hamburger is another form of steak. :-)" On case 5, it will output a string "Finally, a vegitarian is in the house!" And on the default in which none of the choices are inputted by the user it will output a string "Invalid number, please enter a number from the entrees above." Practicing or trying some simple C++ programming source codes like this could help you get a hold on how to use this more effectively.• http://eglobiotraining.com
  • Switch Case #5 #include <stdio.h> main() { int grade; printf ("Input grade [1-5]:"); scanf("%d", & grade); switch (grade) { case 1: printf("Fail (F)n");break; case 2: printf("Bad (D)n");break; case 3: printf("Good (C)n");break; case 4: printf("Very Good (B)n");break; case 5: printf("Excellent (A)n");break; default: printf("You have inputted false graden"); break; // break isn’t necessary here } } As you can see in this programming source code it is an example of a switch case statement. Where the program asks the user to input a number from 1 to 5. If the input number is 1 then the case 1 is being called in the switch case statement then the output will be "Fail (F)". If the input number is 2 then the result will be "Bad (B)". If the input number is 3 then the result will be "Good (C)". If the input number is 4 then the result will be "Very Good (B)". If the input number is 5 then the result will be "Excellent (A)". If the input number is none of the choices then the output will be "You have inputted false grade". This is one of the simplest example when it comes to C++ programming so its better to try the code above or start with simple programming examples
  • LOOPING EXAMPLES: • http://eglobiotraining.com
  • Looping example #1 #include <iostream> using namespace std; int main() { for ( int x = 0; x < 10; x++ ) { cout<< x <<endl; } cin.get(); } This C++ programming example, it shows how to use the for loop statement with a namespace std so that the program can see cout and endl. The loop goes while x < 10, and x increases by one every loop. 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. For each loop made, there is an output being displayed which is equal to the value of x on that current loop.• http://eglobiotraining.com
  • Looping example #2 #include <iostream> using namespace std; // So we can see cout and endl int main() { int x = 0; // Dont 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(); } In this sample programming source code tackles about while loop statement. On the third line of the code, it is used in order the cout and endl. After declaring the function main() a variable x is declared with a value of 0. On line 9 of this code is where the while loop starts. It can also be understand as While variable x is less than 10. It will continue to loop until the variable x meets the condition which is 10. In order for the x to meet the condition, a statement x++ is used to update the value of x. x++ statements is equal to (x = x + 1)• http://eglobiotraining.com
  • Looping example #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(); } A do while loop is used in this programming sample source code. This programming source code displays a string "Hello, world" until it meets the condition (x != 0). It is likely the same with other looping statement where it loops until it meets its condition. A good indention for each line of code when programming is a good practice especially when using looping statements to avoid confusion.
  • Looping example #4 #include <iostream> using namespace std; int main() { int counter, howmuch; cin >> howmuch; counter = 0; do { counter++; cout << counter << n; } while ( counter < howmuch); return 0; } In this programming source code, is an example of do while looping statement. As you go along with the program, on the line 6 to start with is a declaration of variables counter and howmuch. Then on the line 8 is where the program asks the user on how many times does he want to loop the program. Line 9, the developer sets a value to the counter variable which is 0. Line 10, is where the Do While loop starts. Line 12, a statement counter++ is used to update the value of counter variable or it simply increments the variable by 1 every the program encounters this line of code. Line 13, it is where the value of counter is displayed of the current loop. Line 15, is the end of the Do While statement where the variable is checked if it meets the condition. Whenever the variable meets the condition, the looping stops. A good indention for each line of code when programming is a good practice especially when using looping statements to avoid confusion. Another good practice when programming is one must understand the flow of codes.
  • Looping example #5 #include<stdio.h> #include <conio.h> int main() // Creates the main function/method or it is where the program starts first { int counter, howmuch; // Declares a counter and howmuch variable to be used in the while statement printf("How many times would you like to loop? "); // Asks the user on how many times does he/she wants the program to loop. scanf("%d", &howmuch); // Scans users input counter = 0; // Set the value of counter variable to 0 while ( counter < howmuch) // Start of while statement. The condition is if counter is lesser than the howmuch variable then the loop continues. { counter++; // for each loop the counter variable adds 1 in its value printf("%dn", counter); //Prints each result on the screen. } getche(); //getch function prompts the user to press a character and that character is not printed on screen return 0; //returns a value of 0 } /* In this source code, it is an example of a While looping statement where it retrieves the users input on how many times does he/she wants to loop the program. programming a code like this where indentions are really used is a good way or a good practice especially when it comes to programming a looping statement.
  • The Screenshot output of my program using• Dev C++  http://eglobiotraining.com
  • Switch case 1 /* The flow of this Dev C++ programming source code is the user is asked to select a number from the given choices. When the user has selected a number from the choices an output varies from one another. If 1 is selected, it displays a "Play game called". If 2 is selected, it displays a "Load game called". If 3 is selected, it displays a "Play multiplayer game called". If 4 is selected, it displays a "Thanks for playing!" And if none of the above are inputted by the user then "Bad input, quitting!" would be displayed. Using of functions/methods is a good way of programming. Since, you can have different process/output without crowding a switch case statement like this for example. programming using of functions/methods is not only limited to switch case statements and it could also be very useful depending on how a developer use it. */ • http://eglobiotraining.com
  • Switch case 2 /* The flow of this C++ programming example is where the program asks the user to input a number. The output also varies depending on the number inputted by the user. If 1 is selected, it displays "You chose red color". If 2 is selected, it displays "You chose green color". If 3 is selected, it displays "You did not choose any color." This is just a simple programming example about switch-case statements but a good start when it comes to programming. */• http://eglobiotraining.com
  • Switch case 3 As you can see in this C++ programming sample source code it is a Switch-Case statement where the programs asks the user to input a letter either A, B or C and not a number. As you go along the source code you can see at line 19 which is the case A, it will be called when the user inputted letter A. On line 21, it is being called when the user inputted letter B. On line 23, it is being called when the user inputted letter C. And the default is only triggered when none of the choices are inputted by the user. Oftenly, the default is used to display an error like in this example stating that "Invalid choice".
  • Switch case 4 As you can see in this C++ programming sample source code is a switch case statement where the program asks an input from the user with the choices given. On case 1, it will output a string "Lobster is my favorite! Dig in!". On case 2, it will output a string "Yummy! Steak is great... but limit yourself to once a week, or risk the chance of high cholesterol!" On case 3, it will output a string "Turkey is healthier than steak! ...Enjoy!" On case 4, it will output a string "Hamburger is another form of steak. :-)" On case 5, it will output a string "Finally, a vegitarian is in the house!" And on the default in which none of the choices are inputted by the user it will output a string "Invalid number, please enter a number from the entrees above." Practicing or trying some simple C++ programming source codes like this could help you get a hold on how to use this more effectively.• http://eglobiotraining.com
  • Switch case 5 As you can see in this programming source code it is an example of a switch case statement. Where the program asks the user to input a number from 1 to 5. If the input number is 1 then the case 1 is being called in the switch case statement then the output will be "Fail (F)". If the input number is 2 then the result will be "Bad (B)". If the input number is 3 then the result will be "Good (C)". If the input number is 4 then the result will be "Very Good (B)". If the input number is 5 then the result will be "Excellent (A)". If the input number is none of the choices then the output will be "You have inputted false grade". This is one of the simplest example when it comes to C++ programming so its better to try the code above or start with simple programming examples to get a better hold on how the program flows.
  • Looping 1 This C++ programming example, it shows how to use the for loop statement with a namespace std so that the program can see cout and endl. The loop goes while x < 10, and x increases by one every loop. 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. For each loop made, there is an output being displayed which is equal to the value of x on that current loop.
  • Looping 2 In this sample programming source code tackles about while loop statement. On the third line of the code, it is used in order the cout and endl. After declaring the function main() a variable x is declared with a value of 0. On line 9 of this code is where the while loop starts. It can also be understand as While variable x is less than 10. It will continue to loop until the variable x meets the condition which is 10. In order for the x to meet the condition, a statement x++ is used to update the value of x. x++ statements is equal to (x = x + 1)
  • Looping 3 A do while loop is used in this programming sample source code. This programming source code displays a string "Hello, world" until it meets the condition (x != 0). It is likely the same with other looping statement where it loops until it meets its condition. A good indention for each line of code when programming is a good practice especially when using looping statements to avoid confusion.
  • Looping 4 In this programming source code, is an example of do while looping statement. As you go along with the program, on the line 6 to start with is a declaration of variables counter and howmuch. Then on the line 8 is where the program asks the user on how many times does he want to loop the program. Line 9, the developer sets a value to the counter variable which is 0. Line 10, is where the Do While loop starts. Line 12, a statement counter++ is used to update the value of counter variable or it simply increments the variable by 1 every the program encounters this line of code. Line 13, it is where the value of counter is displayed of the current loop. Line 15, is the end of the Do While statement where the variable is checked if it meets the condition. Whenever the variable meets the condition, the looping stops. A good indention for each line of code when programming is a good practice especially when using looping statements to avoid confusion. Another good practice when programming is one must understand the flow of codes.
  • Looping 5 /* In this source code, it is an example of a While looping statement where it retrieves the users input on how many times does he/she wants to loop the program. programming a code like this where indentions are really used is a good way or a good practice especially when it comes to programming a looping statement. • http://eglobiotraining.com
  • My slideshare URL :http://www.slideshare.net/aeden_brines• http://eglobiotraining.com
  • Submitted to:Prof. Erwin M. Globiohttp://eglobiotraining.com/Submitted by:Briñes, Aden Mae S.BM10203• http://eglobiotraining.com