Monday, September 8, 2014

Switch Statement Program

Example of a Switch Statement Program
#include <cstdlib>
#include <iostream>
using namespace std;

int main() {
int n;
cout<<"Please enter a number: ";
cin>>n;
switch (n) {
case 1: {
         cout<<"n is equal to 1! ";
         break;}
      case 2: {
         cout<<"n is equal to 2!";
         break;}
      case 3: {
         cout<<"n is equal to 3!";
         break;}
      default: {
         cout<<"n isn't equal to 1, 2, or 3.";
         break;}
   }
   system("PAUSE");
   return 0;}




Additional Example of a Switch Statement Program (For those who are asking)

#include <cstdlib>

#include <iostream>

using namespace std;

int main() {

char n;

cout<<"Please enter a letter from A to C: ";

cin>>n;

switch (n) {

case a: {

cout<<"The user of this program is a real beauty that he/she will add a comment to this post. ";

break;}

case b: {

cout<<"The user of this program is very intelligent that he/she will add a comment to this post. ";

break;}

case c: {

cout<<"The user of this program is a great person that he/she will add a comment to this post.";

break;}

default: {

cout<<"The user of this program is simply amazing!!!";

break;}

}

system("PAUSE");

return 0;}

40 comments:

  1. a program that explains a lot.. thanks!!

    ReplyDelete
  2. thanks for the info here..

    ReplyDelete
  3. more examples please!!

    ReplyDelete
  4. can i put as many cases as i want?

    ReplyDelete
  5. Yes as long as the cases are different from each other.

    ReplyDelete
  6. Replies
    1. it is also applicable to c language.. just change some of the syntax and it will run properly..

      Delete
    2. thanks for asking by the way..

      Delete
  7. this program goes to c and c++ language.. you just need to change some syntax but the logic is the same.

    ReplyDelete
  8. Replies
    1. my friend.. here is another example.. hope this helps you. ^_^

      Delete
  9. This blog is very halpful. Thanks

    ReplyDelete
  10. It's brilliant to have a blog like this. :)

    ReplyDelete
  11. the syntax is easy and convenient to use for c and c++..

    ReplyDelete
  12. more samples please!!!

    ReplyDelete
    Replies
    1. here is another example of a switch a program.. sorry for the late update! i hope this helps you a lot !

      Delete
  13. a simple program for simple thinkers like me...

    ReplyDelete
  14. it works when i tried it on the programming application.. thanks for this!

    ReplyDelete
  15. nice.. great!.. i really love your blog!

    ReplyDelete
  16. does this work with other programming languages??

    ReplyDelete
  17. the logic yes.. you just have to change the syntax but the output woud be the same..

    ReplyDelete
    Replies
    1. i see.. thanks for this ms author!!

      Delete
    2. you're very much welcome.. visit my blog anytime ok??? ^_^

      Delete
  18. the second example is funny.. but it suits me heheh! thanks!!

    ReplyDelete