Switch case statements are a substitute for long if statements that compare to an integral value. The basic format for usi...
The condition of a switch statement is a value. Thecase says that if it has the value of whatever is afterthat case then d...
int a = 10;int b = 10;int c = 20;switch ( a ) {case b:  // Code  break;case c:  // Code  break;default:  // Code  break;}h...
The default case is optional, but it is wise toinclude it as it handles any unexpected cases.Switch statements serves as a...
#include <iostream>using namespace std;void playgame();void loadgame();void playmultiplayer();int main(){  int input; cout...
switch ( input ) {    case 1:       // Note the colon, not a semicolon      playgame();      break;    case 2:       // No...
This program will compile, but cannot be run until the undefined functions are given bodies, but it serves as a model (alb...
Here are some program that I use in switch case c programming1)#include<stdio.h>main(){    float up, q, p;    double s, c;...
printf("nnntttThe total Sales of the Company is: %.2lf", s);     if(s>=20000){     c= s*(.20);     printf("nnnnttttThe Com...
In here I use a float variable that may contain decimal values    other than integer, and may also be expressed as scienti...
2) #include<stdio.h>main(){  /*  int x,y;   printf("nt"POSSIBLE OUTPUT #1"");   printf("nnntttEnter a number:");   scanf("...
int x, sum ;     printf("nt"POSSIBLE OUTPUT #2"");     printf("nnntttEnter the number:");     scanf ("%d", &x);   x=x;   w...
So for my second switch case program I just wanted to determinethe possible values using variable x and v in c programming...
3) #include<stdio.h>main(){    float x, ld, id, td;    printf("nnnntttt"COMPUTER VISION"");    printf("nnntttEnter the amo...
printf("nnttYour Local Discount is: %.2f", ld);         id= x*.07;            printf("nnttYour Imported Discount is: %.2f"...
printf("nnttYour Local Discount is: %.2f", ld);         id= x*.09;           printf("nnttYour Imported Discount is: %.2f",...
So here by using C programming I found this program which is allabout computer vision and use to know the amount bought by...
4) #include <iostream>using namespace std;void playgame(){  cout << "Play game called";}void loadgame(){  cout << "Load ga...
int main(){  int input; cout<<"1. Play gamen"; cout<<"2. Load gamen"; cout<<"3. Play multiplayern"; cout<<"4. Exitn"; cout...
case 4:        // Note the colon, not a semicolon    cout<<"Thank you for playing!n";    break;  default:       // Note th...
Thus by using C programming I used this source code so used intmain () as the first function to be executed, and note the ...
5) #include <stdio.h>main(){    int Choice;    float Value10, Value12;    do{                  printf("Enter value 10: ");...
printf("n5. Quit");              printf("nEnter your choice [10-15]: ");              scanf("%i", &Choice);              s...
case 4:                       printf("nThe result is: %g", Value10+Value12);                       break;                 ...
Thus by using C programming I used this source code so usedmain () as the first function to be executed, and enter the val...
Loopshttp://eglobiotraining.com
Every loop will always contain three   main elements:   Priming: initialize your variables.   Testing: test against some  ...
Type of LoopsIndefinite Loop:    You do not know ahead of time how many times your    loop will execute.    For example, y...
For loops• Another type of loop in Java is the for  loop.• It is very good for definite loops• All the parts (priming, tes...
Basic For Loop Syntax• for loops are good for creating definite  loops.int counter; 1. Priming: Set    2. Test Condition: ...
for Loop Flowchart                    1. Priming                    Set counter=1                             2. Test   TR...
Infinite Loop• You can still end up with an infinite loop when  using for loops  for (counter = 0; counter <= 10; counter-...
For Loop Variations• The limit can be a variable:  for ( i = 1; i <= limit; i++)  – This counts from 1 to limit• Update ma...
The for Structure: Notes and Observations• Arithmetic expressions   – Initialization, loop-continuation, and increment can...
The Comma Operator• Commas known here as comma operators• by using commas, you can put more than one  statement in priming...
Warnings• Do not use a float or double for the counter   – May result in imprecise counter values and faulty     evaluatio...
Off-by-one error• In the first example, shown here, if had  written       counter < 10  then loop would execute 9 times, n...
Help avoid off-by-one errors• Try to make your conditions in the form <= not  <  – Avoid code like counter < 11 or counter...
Good Programming Practices• Do not change the value of the counter variable in your loop.    – Do not attempt to manually ...
1) #include<stdio.h>main(){   int x,y;   for(x=1;x<=3;x++){     for (y=1;y<=3;y++){     printf("*");     }    printf("n");...
For looping I just used variable x, and y and printf as the variable operator in Cprogramming and thus here is the output ...
2) #include <iostream>using namespace std;int main(){           int number = 0; // Variable for user to enter a number.   ...
In my second attempt using C programming source code I used int main () and enter anumber and run the program in dev C++ p...
3) #include <stdio.h>           // including standard input output library#include <conio.h>            // including the c...
Furthermore this is the result of my third attempt of using C programming I put thequestion s like what is the perfect num...
4) #include <iostream>using namespace std;int main(){              int myArray[10][10];              for (int i = 0; i <= ...
Thus in my fourth program using C programming I used some source codes and includearray to give each element a value and a...
5) #include <iostream>using namespace std;int main(){         int a;         cout << "How many times do you want the loop ...
Lastly in my 5th looping program using C programming source codes andask a question on how many times do want the loop to ...
http://www.slideshare.net/daisy_arcangelhttp://eglobiotraining.com
Submitted to Sir Globio E.M for Final requirements                             By : Daisy Nghttp://eglobiotraining.com
Upcoming SlideShare
Loading in …5
×

Presentation1

168 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
168
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Presentation1

  1. 1. Switch case statements are a substitute for long if statements that compare to an integral value. The basic format for using switch case is outlined below.switch ( value ) {case this: Code to execute if value == this break;case that: Code to execute if value == that break;...default: Code to execute if value != this or that break;}http://eglobiotraining.com.
  2. 2. The condition of a switch statement is a value. Thecase says that if it has the value of whatever is afterthat case then do whatever follows the colon. Thebreak is used to break out of the case statements.Break is a keyword that breaks out of the codeblock, usually surrounded by braces, which it is in.In this case, break prevents the program fromfalling through and executing the code in all theother case statements. An important thing to noteabout the switch statement is that the case valuesmay only be constant integral expressions. Sadly, itisnt legal to use case like this:http://eglobiotraining.com.
  3. 3. int a = 10;int b = 10;int c = 20;switch ( a ) {case b: // Code break;case c: // Code break;default: // Code break;}http://eglobiotraining.com.
  4. 4. The default case is optional, but it is wise toinclude it as it handles any unexpected cases.Switch statements serves as a simple way towrite long if statements when therequirements are met. Often it can be usedto process input from a user.Below is a sample program, in which not allof the proper functions are actually declared,but which shows how one would use switchin a program.http://eglobiotraining.com.
  5. 5. #include <iostream>using namespace std;void playgame();void loadgame();void playmultiplayer();int main(){ int input; cout<<"1. Play gamen"; cout<<"2. Load gamen"; cout<<"3. Play multiplayern"; cout<<"4. Exitn"; cout<<"Selection: "; cin>> input;http://eglobiotraining.com.
  6. 6. 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, quittingn"; break; } cin.get();}http://eglobiotraining.com.
  7. 7. This program will compile, but cannot be run until the undefined functions are given bodies, but it serves as a model (albeit simple) for processing input. If you do not understand this then try mentally putting in if statements for the casestatements. Default simply skips out of the switch case construction and allows the program to terminate naturally. If you do not like that, then you can make a loop around the whole thing tohave it wait for valid input. You could easily makea few small functions if you wish to test the code.http://eglobiotraining.com.
  8. 8. Here are some program that I use in switch case c programming1)#include<stdio.h>main(){ float up, q, p; double s, c; printf("nnnntttt"Ku Ripot Company""); printf("nnttEnter the Quantity:"); scanf("%f", &q); printf("nnttEnter the unit price:"); scanf("%f", &up) s = up*q;http://eglobiotraining.com.
  9. 9. printf("nnntttThe total Sales of the Company is: %.2lf", s); if(s>=20000){ c= s*(.20); printf("nnnnttttThe Commission of the salesmen is: %.2lf", c);} else if(s>15000&&s<=20000){ c= s*.15; printf("nnnnttttThe Commission of the Salesmen is: %.2lf", c);} else { c= s*.13; printf("nnnnttttThe Commission of the Salesmen is: %.2lf", c); } getch();}. http://eglobiotraining.com.
  10. 10. In here I use a float variable that may contain decimal values other than integer, and may also be expressed as scientificnotation like q and p. In C programming and used printf or a word operator in C programming, lastly I used scanf for breaking the codes, so in this program I wanted to clarify Ku ripot’s Company quantity and unit price by using C programming in dev C++ and thus here is the result of my screen shot.http://eglobiotraining.com
  11. 11. 2) #include<stdio.h>main(){ /* int x,y; printf("nt"POSSIBLE OUTPUT #1""); printf("nnntttEnter a number:"); scanf("%d", &x); x=x; while (x<=5) { if (x<=5){ printf("%d", x); x++;} else { printf("nnntttERROR!!!");} } */http://eglobiotraining.com
  12. 12. int x, sum ; printf("nt"POSSIBLE OUTPUT #2""); printf("nnntttEnter the number:"); scanf ("%d", &x); x=x; while (x<=5) { if (x<=5){ printf("%d", x); x++;} else { printf("nnntttERROR!!!");} } sum= x+x+x; printf("nnntttThe total is: %d", sum); if (sum%2==0) { printf("nnttThis is an example of an EVEN NUMBER: %d", sum);} else { printf("nnttThis is an exaple of an ODD NUMBER: %d", sum);} getch();}http://eglobiotraining.com
  13. 13. So for my second switch case program I just wanted to determinethe possible values using variable x and v in c programming andrun it in C programming using dev c++ since only the source codesof c programming run in dev c++, and here is the output.http://eglobiotraining.com.
  14. 14. 3) #include<stdio.h>main(){ float x, ld, id, td; printf("nnnntttt"COMPUTER VISION""); printf("nnntttEnter the amount bought to the Company:"); scanf("%f", &x); if(x>=0 && x<=999 ) { ld= x*.05; printf("nnttYour Local Discount is: %.2f", ld); id= x*.06; printf("nnttYour Imported Discount is: %.2f", id); td= ld+id; printf("nnntttYour Total Discount is: %.2f", td);} else if (x>=1000 && x<=1999 ) { ld= x*.06;http://eglobiotraining.com
  15. 15. printf("nnttYour Local Discount is: %.2f", ld); id= x*.07; printf("nnttYour Imported Discount is: %.2f", id); td= ld+id; printf("nnntttYour Total Discount is: %.2f", td);} else if (x>=2000 && x<=3999 ){ ld= x*.07; printf("nnttYour Local Discount is: %.2f", ld); id= x*.08; printf("nnttYour Imported Discount is: %.2f", id); td= ld+id; printf("nnntttYour Total Discount is: %.2f", td);} else if (x>=4000 && x<=7999 ){ ld= x*.08;http://eglobiotraining.com
  16. 16. printf("nnttYour Local Discount is: %.2f", ld); id= x*.09; printf("nnttYour Imported Discount is: %.2f", id); td= ld+id; printf("nnntttYour Total Discount is: %.2f", td);} else { ld= x*.10; printf("nnttYour Local Discount is: %.2f", ld); id= x*.10; printf("nnttYour Imported Discount is: %.2f", id); td= ld+id; printf("nnntttYour Total Discount is: %.2f", td);}getch();http://eglobiotraining.com
  17. 17. So here by using C programming I found this program which is allabout computer vision and use to know the amount bought bythe company. Thus here is my output in C programming sourcecodes in dev C++.http://eglobiotraining.com
  18. 18. 4) #include <iostream>using namespace std;void playgame(){ cout << "Play game called";}void loadgame(){ cout << "Load game called";}void playmultiplayer(){ cout << "Play multiplayer game called";}http://eglobiotraining.com
  19. 19. int main(){ int input; cout<<"1. Play gamen"; cout<<"2. Load gamen"; cout<<"3. Play multiplayern"; cout<<"4. Exitn"; 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;http://eglobiotraining.com
  20. 20. 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, quittingn"; break; } cin.get();}http://eglobiotraining.com
  21. 21. Thus by using C programming I used this source code so used intmain () as the first function to be executed, and note the colonnot the semicolon and run the program play a game in dev C++programming. So this is my output using dev C++ C programminghttp://eglobiotraining.com
  22. 22. 5) #include <stdio.h>main(){ int Choice; float Value10, Value12; do{ printf("Enter value 10: "); scanf("%f", &Value10); printf("Enter value 12: "); scanf("%f", &Value12); printf("n1. Divide"); printf("n2. Add"); printf("n3. Multiply"); printf("n4. Subtract");http://eglobiotraining.com
  23. 23. printf("n5. Quit"); printf("nEnter your choice [10-15]: "); scanf("%i", &Choice); switch(Choice) { case 1: printf("nThe result is: %g", Value10*Value12); break; case 2: printf("nThe result is: %g", Value10/Value12); break; case 3: printf("nThe result is: %g", Value10-Value12); break;http://eglobiotraining.com
  24. 24. case 4: printf("nThe result is: %g", Value10+Value12); break; case 5: printf("nThank you"); break; default: printf("nInvalid choice!"); } }while (Choice != 15);} http://eglobiotraining.com
  25. 25. Thus by using C programming I used this source code so usedmain () as the first function to be executed, and enter the valuesto be substract,multiply, add or divide ,and run the program play agame in dev C++ programming. So this is my output using dev C++C programming.http://eglobiotraining.com
  26. 26. Loopshttp://eglobiotraining.com
  27. 27. Every loop will always contain three main elements: Priming: initialize your variables. Testing: test against some known condition. Updating: update the variable that is tested.http://eglobiotraining.com
  28. 28. Type of LoopsIndefinite Loop: You do not know ahead of time how many times your loop will execute. For example, you do not know how many books a person might order. Definite Loop: You know exactly how many times you want the loop to execute. not at compile time necessarilyhttp://eglobiotraining.com
  29. 29. For loops• Another type of loop in Java is the for loop.• It is very good for definite loops• All the parts (priming, testing and updating) are in one place.• format: for (prime expression; test expression; update expression)• Since the expressions are all in one place, many people prefer for to while when the number of iterations is known.• http://eglobiotraining.com
  30. 30. Basic For Loop Syntax• for loops are good for creating definite loops.int counter; 1. Priming: Set 2. Test Condition: 3. Update: Update the the start value. Set the stop value. value.for (counter =1;counter <= 10;counter++) System.out.println (counter); http://eglobiotraining.com Note that each section is separated by a semicolon.
  31. 31. for Loop Flowchart 1. Priming Set counter=1 2. Test TRUE counter 3a. print counter <= 10 3b. Update counter++; FALSEhttp://eglobiotraining.com
  32. 32. Infinite Loop• You can still end up with an infinite loop when using for loops for (counter = 0; counter <= 10; counter--)http://eglobiotraining.com
  33. 33. For Loop Variations• The limit can be a variable: for ( i = 1; i <= limit; i++) – This counts from 1 to limit• Update may be negative: for (i = 100; i >= 1; i--) – This counts from 100 to 1.• Update may be greater than 1: for (i = 100; i >= 5; i -= 5) – This counts from 100 to 5 in steps of 5 http://eglobiotraining.com
  34. 34. The for Structure: Notes and Observations• Arithmetic expressions – Initialization, loop-continuation, and increment can contain arithmetic expressions. If x equals 2 and y equals 10 for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to for ( j = 2; j <= 80; j += 5 )• Notes about the for structure: – If the loop continuation condition is initially false • The body of the for structure is not performed • Control proceeds with the next statement after the for structure – Control variable • Often printed or used inside for body, but not necessary http://eglobiotraining.com
  35. 35. The Comma Operator• Commas known here as comma operators• by using commas, you can put more than one statement in priming or updating expression for (i = 100, y = 0; i >= 1; i--) or for (i = 1; j + i <= 10; i++, j++) { code; }• Commas known here as comma operators http://eglobiotraining.com
  36. 36. Warnings• Do not use a float or double for the counter – May result in imprecise counter values and faulty evaluation for loop termination purposes• Don’t use commas instead of semicolons to separate the components of the for loop – (very common error)• As in the if and while, do not put a semicolon ; right after the parentheses – will be an empty loop!http://eglobiotraining.com
  37. 37. Off-by-one error• In the first example, shown here, if had written counter < 10 then loop would execute 9 times, not the desired 10 timesfor (counter = 1; counter <= 10; counter++){ System.out.println (counter);}/* end for counter */ http://eglobiotraining.com
  38. 38. Help avoid off-by-one errors• Try to make your conditions in the form <= not < – Avoid code like counter < 11 or counter < 10 http://eglobiotraining.com
  39. 39. Good Programming Practices• Do not change the value of the counter variable in your loop. – Do not attempt to manually adjust it to force an exit – Can cause subtle errors that are difficult to catch – Instead change your logic• Do not put other expressions in the for control structure – Manipulations of other variables should appear before or within the body of the loop depending on whether or not you want to repeat them• Put a blank line before and after each major control structure• Try to limit nesting to three levels, if possible – More than three levels of indentation can get confusing• Limit the for control header to one line if possiblehttp://eglobiotraining.com
  40. 40. 1) #include<stdio.h>main(){ int x,y; for(x=1;x<=3;x++){ for (y=1;y<=3;y++){ printf("*"); } printf("n"); }getch();}http://eglobiotraining.com
  41. 41. For looping I just used variable x, and y and printf as the variable operator in Cprogramming and thus here is the output for dev c++ C programminghttp://eglobiotraining.com
  42. 42. 2) #include <iostream>using namespace std;int main(){ int number = 0; // Variable for user to enter a number. int sum = 0; // To hold the running sum during all loop iterations. cout << "Enter a number: "; cin >> number; // Loop keeps adding until it reaches the number entered. for (int index = 0; index <= number; index++) { sum += index; // Each iteration Adds to the variable sum. } // cout statement is put after the loop. cout << "nThe sum of all numbers from 0 to " << number << " equals: " << sum << "nn"; return 0;}http://eglobiotraining.com
  43. 43. In my second attempt using C programming source code I used int main () and enter anumber and run the program in dev C++ programming. So this is my output using dev C++C programming.http://eglobiotraining.com
  44. 44. 3) #include <stdio.h> // including standard input output library#include <conio.h> // including the conio.h file#define MAX 10000 // defining a constant, MAX = 10000int main() // main program{unsigned long long i, j, s ; // declaring variablesprintf(" Perfect numbers till %d: n ", MAX) ; // outputting messagefor(i = 2; i <= MAX; i++) // loop for the prime numbers{s=0; // s (sum) is declared and initialized with a value of zerofor(j = 1; j <= i/2; j++)if(i % j == 0) // if the carry-over of i divided by j equals 0s += j ; // j = s+jif(i == s)printf(" %5u ", i) ; // outputting the perfect numbers}getch();} // end of programhttp://eglobiotraining.com
  45. 45. Furthermore this is the result of my third attempt of using C programming I put thequestion s like what is the perfect number till 10000 and run the program in dev C++programming. So this is my output using dev C++ C programming.http://eglobiotraining.com
  46. 46. 4) #include <iostream>using namespace std;int main(){ int myArray[10][10]; for (int i = 0; i <= 9; ++i){ for (int t = 0; t <=9; ++t){ myArray[i][t] = i+t; //This will give each element a value } } for (int i = 0; i <= 9; ++i){ for (int t = 0; t <=9; ++t){ cout << myArray[i][t]; } } system("pause");} http://eglobiotraining.com
  47. 47. Thus in my fourth program using C programming I used some source codes and includearray to give each element a value and after that run the program in dev C++programming. So this ismy output using dev C++ C programming.http://eglobiotraining.com
  48. 48. 5) #include <iostream>using namespace std;int main(){ int a; cout << "How many times do you want the loop to run? "; cin >> a; while (a){ cout << a << "n"; --a; } return 0;}http://eglobiotraining.com
  49. 49. Lastly in my 5th looping program using C programming source codes andask a question on how many times do want the loop to run, and startedto run the program in dev C++ programming. So this is my output usingdev C++ C programming.http://eglobiotraining.com
  50. 50. http://www.slideshare.net/daisy_arcangelhttp://eglobiotraining.com
  51. 51. Submitted to Sir Globio E.M for Final requirements By : Daisy Nghttp://eglobiotraining.com

×