C++ Tutorial #10 – Jump Statements

Facebooktwittergoogle_plusredditpinterestlinkedinmail

In this tutorial, I’m going to teach you about Jump Statements. Jump Statements allow you to change the flow of a program by making jumps to certain locations of your program, hence the name “Jump Statements”.

 

There are three different types of Jump Statements: break statement, continue statement, and goto statement. We are going to use all of these statements in separate programs, but before we get started, let me tell you what each statement does.

 

Break Statement is used to break out of loops. It is placed inside a conditional if statement. In our example we will ABORT a countdown in a For Loop.

 

Continue Statement is used to skip specified statements inside of a loop. It is also used with a condition if statement. In our example we will skip numbers in our loop that we choose.

 

GoTo Statement can also be used to break out of loops, but you can also use it to jump to (or goto) specific locations on your program. The goto statment is my favorite. In our example, we will write a more complex program. We’re going to go back to what we did on Tutorial #3 – Making a Simple Calculator. Only this time, we’re going to code it so that the user can choose an operator.

 

Lets get started on our three programs.

 

BREAK STATEMENT EXAMPLE:

 

 

The program should look something like this:

 

10
9
8
7
6
ABORT COUNTDOWN!

 

Take a look at the code:

 

We create a for loop starting with int countDown = 10, and make it decrement by 1 while countDown is greater than 0. Then we write an if statement with countdown == 6 for the condition. When that statement becomes true, the program will print “ABORT COUNTDOWN” on the screen and break out of the loop (using break; ).

 

 

 

CONTINUE STATEMENT EXAMPLE:

 

 

The program should look something like this:

 

2
4
6
8
10
12
13
14
15
Loop Finished!

 

Take a look at the code:

 

We create a for loop starting with int countUp = 1, and make it increment by 1 while countUp is less than or equal to 15. Then we create an if statement. When any of the conditions become true, our statement (continue; ) will be executed. Continue skips the statement inside of our for loop, and that’s why the odd numbers don’t print on the screen.

 

 

 

GOTO STATEMENT EXAMPLE:

 

 

 

Pick an operator (+,-,*,/): +
Enter first number: 100
Enter second number: 250
The sum is: 350

 

Take a look at the code:

 

We create three integers: num1, num2, and answer. Then we create a string named opType. We don’t set a value to any of these variables because we are going to let the user assign the values.

 

 

 

We create our label beginning, and have our program tell the user to pick an operator. cin >> opType; allows the user to assign a value to our string opType. Then we have an if statement with several conditions. If the user picks any of the operators, the program will goto or jump to the label in the statement. If any of these conditions are false, the program will tell the user that they didn’t choose an operator and goto the beginning label.

 

 

 

 

We then have our four labels: addition, subtraction, multiplication, and division. Inside of these labels we write our code to allow the user assign a value to num1 and num2. Then the program calculates the two numbers and prints out the answer. We use getch(); to make the program wait for an input (Enter), then the program jumps to the label beginning and the program starts over again.

 

 

 

Now this program is not perfect. If you place anything that is not a number when the program asks for a number, you will get an error. There is a way to fix that, but I didn’t want tot confuse you. I will show you how to fix this in another tutorial.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.