Looping and Switchcase BDCR
Upcoming SlideShare
Loading in...5
×
 

Looping and Switchcase BDCR

on

  • 303 views

 

Statistics

Views

Total Views
303
Views on SlideShare
303
Embed Views
0

Actions

Likes
0
Downloads
3
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

Looping and Switchcase BDCR Looping and Switchcase BDCR Presentation Transcript

  • LOOPING http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://www.cprogramming.com/tutorial/lesson3.html DEFINITION 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 -- many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times. (They may be executing a small number of tasks, but in principle, to produce a list of messages only requires repeating the operation of reading in some data and displaying it.) Now, think about what this means: a loop lets you write a very simple statement to produce a significantly greater result simply by repetition. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://www.cprogramming.com/tutorial/lesson3.html LOOPING There are three different loop constructs that can be used depending on whether the number of repetitions is known and also (where the number of repetitions is not known and is dependent on a condition) whether the loop is allowed to be bypassed if the termination condition is met before the loop is first executed. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://javascript.about.com/library/blstruc3.htm “FOR” LOOP A for loop in programming allows a statement to be executed a specified number of times. The for loop begins with a loop control variable assigned a specific initial value. This control variable in then incremented (or decremented) by a specified amount each time around the loop until a specified terminating value is reached at which time the statement following the loop is then executed. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://www.cprogramming.com/tutorial/lesson3.html “FOR” LOOP 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. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://javascript.about.com/library/blstruc3.htm “WHILE” LOOP A while loop in programming allows a statement to be executed until a given condition is met. If the condition is met prior to executing the loop then the loop will not be executed. As soon as the condition is met, execution continues with the statement following the loop. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://www.cprogramming.com/tutorial/lesson3.html “WHILE” LOOP 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. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://www.cprogramming.com/tutorial/lesson3.html “DO WHILE” LOOP A form of programming loop in which the condition for termination (continuation) is computed each time around the loop. There are several variants on this basic idea. The basic 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". http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLES http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLE 1 #include <stdio.h> main(){ float price; short quantity; char answer; printf("Do you wish to enter a purchase (Y/N)? "); scanf(" %c", &answer); while(answer ==Y||answer==y) { printf("Enter price quantity: "); scanf("%f %hi", &price, &quantity); printf("The total for this item is $%6.2f.n", price * quantity); printf("Another (Y/N)? "); scanf(" %c", &answer); } printf("Thank you for your patronage.n");} http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLE 1THIS IS A PROGRAMMING OF A PURCHASE ORDER THAT A CUSTOMER MAY FILL UP. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://www.codeobsessed.com/c++loops.html EXAMPLE 2 // - Show all odd numbers using a for loop #include <iostream> using namespace std; int main() { for (int count = 1; count <= 41; count += 2) { cout << count << ", "; } cout << endl; return 0; } http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLE 2THIS I S A PROGRAMMING LOOP THA T WOULD SHOW A LL ODD NUMBERS FROM 1 TO 41. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://www.codeobsessed.com/c++loops.html EXAMPLE 3 // - Count backwards using a for loop. #include <iostream> using namespace std; int main() { for (int count = 15; count > -1; count--) { cout << count << ", "; } cout << endl; return 0; } http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLE 3THIS IS A PROGRAMMING LOOP THAT COUNTS BACKWA RDS FROM 15 TO 1. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://www.codeobsessed.com/c++loops.html EXAMPLE 4 // - Count by fives using a for loop. #include <iostream> using namespace std; int main() { for (int count = 0; count <= 100; count += 5) { cout << count << ", "; } cout << endl; return 0; } http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLE 4THIS PROGRA MMING LOOP THA T WOULD COUNT BY 5 FROM 0 TO 100. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://computer.howstuffworks.com/c9.htm EXAMPLE 5 #include <stdio.h> int main() { int a; a = 0; while (a <= 100) { printf("%4d degrees F = %4d degrees Cn", a, (a - 32) * 5 / 9); a = a + 10; } return 0; } http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLE 5THIS PROGRA MMING LOOP THA T WOULD CONVERT FAHRENHEIT TO CELSIUS. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • SWITCHCASE http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_028.htm DEFINITION 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 switch case 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. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_028.htm SWITCHCASE The switch case statement is a better way of writing a program when a series of if elses occurs. The general format for this is, switch ( expression ) { case value1: program statement; program statement; ...... break; case valuen: program statement; ....... break; default: ....... ....... break; The keyword break must be included at the end of each case statement. The default clause is optional, and is executed if the cases are not met. The right brace at the end signifies the end of the case selections. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLES http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLE 1#include <stdio.h>void main(void){ float price; short quantity; char answer; do { printf("Enter price quantity: "); scanf("%f %hi", &price, &quantity); printf("The total for this item is $%6.2f.n", price * quantity); printf("Another (Y/N)? "); scanf(" %c", &answer); }while (answer == Y | | answer == y); printf("Thank you for your patronage.n");} http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLE 1 THIS IS PROGRAMMING OF A BASIC INVENTORY SYSTEMTHAT WOULD A DD, EDI T, DELETE A ND A CCESS PRODUCTS. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_028.htm EXAMPLE 2 #include <stdio.h> main() { int menu, numb1, numb2, total; printf("enter in two numbers -->"); scanf("%d %d", &numb1, &numb2 ); printf("enter in choicen"); printf("1=additionn"); printf("2=subtractionn"); scanf("%d", &menu ); switch( menu ) { case 1: total = numb1 + numb2; break; case 2: total = numb1 - numb2; break; default: printf("Invalid option selectedn"); } if( menu == 1 ) printf("%d plus %d is %dn", numb1, numb2, total ); else if( menu == 2 ) printf("%d minus %d is %dn", numb1, numb2, total ); } http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLE 2THIS IS A PROGRAMMING OF A SORT-OF A CALCULATOR THAT ADDS AND SUBTRACTS 2 NUMBERS. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://programmingexamples.wikidot.com/c-switch-case EXAMPLE 3 #include <stdio.h> #include <stdlib.h> int main() { int input; do { printf( "1. Play gamen" ); printf( "2. Load gamen" ); printf( "3. Play multiplayern" ); printf( "4. Exitn" ); printf( "Selection: " ); scanf( "%d", &input ); switch ( input ) { case 1: /* Note the colon, not a semicolon */ printf("Playing the gamen"); break; case 2: printf("Loading the gamen"); break; case 3: printf("Playing multiplayern"); break; case 4: printf( "Thanks for playing!n" ); break; default: printf( "Bad input!n" ); break; } }while(input != 4); } http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLE 3THIS IS A PROGRAMMING THAT ENABLES YOU TO PLAY GAME, LOAD GAME AND PLAY MULTIPLAYER. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://www.java-samples.com/showtutorial.php?tutorialid=327 EXAMPLE 4 #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 << "nn"; return 0; } http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLE 4THIS IS A PROGRAMMING THAT ENABLES TO ENCODE WI TH THE PROPER VA LUE. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • Source: http://www.codingmagic.com/CPPTutorials/CPPTutorial04.html EXAMPLE 5 #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 isnt equal to 1, 2, or 3.n"); break; } } system("PAUSE"); return 0; } http://eglobiotraining.com RIVERA, BERNARD D.C.
  • EXAMPLE 5THIS IS A PROGRAMMING THAT ENABLES YOU TO CHECK THE VALUE YOU ENCODE. http://eglobiotraining.com RIVERA, BERNARD D.C.
  • http://eglobiotraining.com RIVERA, BERNARD D.C.
  • PROF. ERWIN GLOBIO HTTP://EGLOBIOTRAINING.COM/ http://eglobiotraining.com RIVERA, BERNARD D.C.