SkidForums


Where skids go hackers
 
HomeFAQSearchMemberlistUsergroupsRegisterLog in

Share | 
 

 C++ Basic switch.

View previous topic View next topic Go down 
AuthorMessage
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: C++ Basic switch.   Sun Apr 29, 2012 1:26 pm

All right. In the previous tutorial I covered control flow and using if statements. This time I'm going to cover switch.
What is a switch? Well it's another form of an if statement, I gues it's a cleaner form of an if statement.
Example.cpp
Code:

#include <iostream>

using std::cout;
using std::cin;

int main()
{
   cout << "Enter integer (1 - Exit, 2 - Travel through time, 3 - Get a girl, 4 . drive drunk)->";
   int x;
   cin >> x;
      if (x == 1)
      {
         close();
      }
      else if (x == 2)
      {
         TimeMachine();
      }
      else if (x == 3)
      {
         BadPickupLinesOnReallyHotGirls();
      }
      else if (x == 4)
      {
         Die();
      }
      else
         cout << "Input not recognized\n";

   return 0;
}
Okay a bunch of beautiful if and if else statements right?
Actually wrong. It just looks very ugly, repetitive and unprofessional.
Now we can pull off the same exact set of instructions using switch statements.

Code:

int x;
switch(x) //Consider switch as the if and x is what the values will be compared to.
      { //Begin the switch block

      case 1: //condition user enters 1 began by a colin
         { //Begin the block
            close();
            break; //I will explain break; after. but pay attention to it.
         }//End the case block

      case 2: //Condition user enters 2
         {
            TimeMachine();
            break;
         }

      case 3:
         {
            BadPickupLinesOnReallyHotGirls();
            break;
         }

      case 4:
         {
            Die();
            break;
         }
      default: //I will explain this in a second
         cout << "Input not recognized\n";

      } //end the switch block
That is what a switch statement looks like, and depending on what values the user enters is where the case comes in.
If I wanted to jump off a bridge when the user enters 100 for x, I can do.
Code:

   switch(x)
   {
      case 100:
         {
            JumpOffABridge();
            break;
         }
      default:
                      cout << "I did not understand what you said";
   }
Okay if you haven't guesd by now. At the end of both switch statements the last instruction I put was a default. What is a default? It's exactly like an else. In my if else statements above I had an if, if else, if else, if else, and an else for the final condition meaning the program didn't meat any of the conditions above. That's what a default does.
In the example above, it shows case 100:
only if the user enters 100 will I jump off a bridge. But let's say we entered a value that we didn't have a condition for in our switch statement? Then the program jumps to the default and can run a last set of instructions before ending the switch.

Now what is a break? Well a break ends the switch instructions. So why do we need it?
Let's say did this.

Code:

int x;
switch(x) //Consider switch as the if and x is what the values will be compared to.
      { //Begin the switch block

      case 1: //condition user enters 1 began by a colin
         { //Begin the block
            close();
            
         }//End the case block

      case 2: //Condition user enters 2
         {
            TimeMachine();
            
         }

      case 3:
         {
            BadPickupLinesOnReallyHotGirls();
            
         }

      case 4:
         {
            Die();
            
         }
      default: //I will explain this in a second
         cout << "Input not recognized\n";

      } //end the switch block
There are no breaks in that switch. So the program runs, asks me to enter a value. I enter 2. Oh woo! A condition was met If we enter 2 we call the function TimeMachine(); So the function TimeMachine() runs, and after how ever many instructions, it ends. Now when it ends we covered in our previous tutorial that the program will go back to the main function and continue where it left off.
Can you gues where we will continue from after the function TimeMachine(); is ran?

If you gused inside the switch statement, you are absolutely correct. Because even though the condition was met, we never told the switch to end. What will it do? Continue checking if condition 3 was met, and then continue checking if condition 4 was met, and then defualt. But that's unnecessary since we all ready entered 1. So what do we do to prevent that?
We enter a break at the end of each case, so when the condition is met, it will run the instructions you told it, let's say function TimeMachine(); come back to the main function and realize there is a break. So no more needing to check through the switch statement we can just break out of it and move on.

That is pretty much all I can cover about switch statements. I hope I did not cause any confusion. If you have any questions feel free to ask.
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: C++ Basic switch.   Sun Apr 29, 2012 8:54 pm

Man, are you teacher irl or what Very Happy?
Great tutorial again :
i enjoy tutorials of you ways more then from books Smile
ur explaining everything realy good sunny
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: C++ Basic switch.   Mon Apr 30, 2012 10:36 am

I don't teach programming.
But I do tutor in Mathematics.
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: C++ Basic switch.   Mon Apr 30, 2012 10:54 am

okay Smile
like those mega advanced maths with numbers etc o-o?>
btw ill be off for some time, got alot of infections on my 2 machines....need to reinstall both
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: C++ Basic switch.   Mon Apr 30, 2012 1:17 pm

Ah that's terrible to hear.
Well I tutor up to Calculus III. So anything from Algebra all the way up to Multi-variable differential equations and Integration.
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: C++ Basic switch.   Tue May 01, 2012 5:32 am

Nikolai wrote:
Ah that's terrible to hear.
Well I tutor up to Calculus III. So anything from Algebra all the way up to Multi-variable differential equations and Integration.
scratch how old are you affraid ?
Back to top Go down
View user profile http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master


Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

PostSubject: Re: C++ Basic switch.   Tue May 01, 2012 10:59 am

I'm 21 years old. I will be 22 in a month.
Back to top Go down
View user profile
woefie
Owner
Owner
avatar

Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

PostSubject: Re: C++ Basic switch.   Tue May 01, 2012 8:06 pm

oh kewl O-o
didnt even suprise me so much Razz
Back to top Go down
View user profile http://skid.forum-leaks.com
Rzdude
Beginner
Beginner


Posts : 78
Points : 110
Reputation : 0
Join date : 2014-06-27

PostSubject: Re: C++ Basic switch.   Fri Jun 27, 2014 5:41 am

You really is a pro..
Back to top Go down
View user profile
Sponsored content




PostSubject: Re: C++ Basic switch.   

Back to top Go down
 
C++ Basic switch.
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» example of Basic turbo C program in PLC Programming
» BASIC TWO CHANNEL RF TRANSMISSION (27MHz)
» Basic PLC interfacing Circuits
» DMR SWITCH
» automatic count up/down circuit just pressing a latch switch

Permissions in this forum:You cannot reply to topics in this forum
SkidForums ::   :: Coding :: Coding :: C/C++-
Jump to: