ChEaTdEViCe
WELCOME TO CHEAT DEVICE
GTAOnline popping here
ChEaTdEViCe
WELCOME TO CHEAT DEVICE
GTAOnline popping here



 
HomeHomeGallerySearchLatest imagesRegisterLog in

Share | 
 

 Lesson 7 - Jump Staements

View previous topic View next topic Go down 
AuthorMessage
DarK_DemoN
Member
DarK_DemoN

Points Points : 6144
Posts : 381
Reputation : 25
Join date : 2011-01-25
Location : Hacking Kingdom

Lesson 7 - Jump Staements _
PostSubject: Lesson 7 - Jump Staements   Lesson 7 - Jump Staements I_icon_minitimeTue Jan 25, 2011 12:59 pm

Hello. While your mind is still fresh from if statements, I'll introduce switch statements. Switch statements work just like if statements except for a few syntax differences. The syntax of a switch statement is as follows:

Code:
switch(variable){
  case x:
      code
      break;
  case y:
      code
      break;
  default:
      code
      break;
}

The code is pretty self-explanatory. You give a case and tell the compiler what to do in that case. The default is what to do if no cases are met. Heres an example program:

Code:
cout << "Enter 1,2, or 3:";
int x;
cin >> x;
switch(x)
{
  case 1:
      cout << "You entered 1";
      break;
  case 2:
      cout << "You entered 2";
      break;
  case 3:
      cout << "You entered 3";
      break;
  default:
      cout << "Please try again";
      break;
}

Switch statements are great replacements for if statements if you want to look organized and if you have a lot of cases.

Next up is the goto statement. I DON'T recommend using goto statements. They can harm your program. But heres an example:

Code:
start:

  cout << "Enter the number 1";
  int x;
  cin >> x;

switch(x){
  case 1:
      cout << "Good job";
      break;
  default:
      goto start;
      break;
}

Here we define start as a place in the code. Then if they don't enter the number one, like the program says, the program jumps back up to the start.

Finally we have continue. Continues are placed in loops and are used if you don't want to do anything if a certain condition if met.

Example:

Code:
#include <iostream>
using namespace std;

int main ()
{
  for (int n=10; n>0; n--) {
    if (n==5) continue;
    cout << n << ", ";
  }
  cout << "FIRE!\n";
  return 0;
}

The code above will start at the number 10 and count down till it gets to 1. After it displays the number one, it will say the word 'FIRE!'. You might be saying, "Well then what does the continue statement do?" Well, my dear friend, you will see that it says if (n==5) continue. When this program runs it display, "10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!" Did you catch it? That's right. It didn't display the number 5. That code says. If n is equal to 5 then go back to the beginning of the loop.

Last AND least is the exit function. The exit function is used for terminating a program. It is done like so.

Code:
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
  char again;
  play:
  coayut << "Play Again? [Y/N]" << endl;
  cin >> again;
  if (again == 'Y' || again == 'y') {
      goto play;
  }
  else {
      exit(0);
  }
}

This program asks you if you would like to play again. If you type in 'y', then it repeats the question. If you type in anything other than 'y', then the program terminates. Hopefully with a return value of '0'.

You Try:
1) Remember that game you made in the past lessons. Add a game over section at the end.
2) Create a calculator that adds, subtracts, multiplies, and divides the user's numbers.

Watch the video here: [Not Posted]
Back to top Go down
http://1337codez.co.cc
 

Lesson 7 - Jump Staements

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
ChEaTdEViCe ::  :: Programming :: C++ Programinng Basics-
Make a forum | ©phpBB | Free forum support | Report an abuse | Forumotion.com