answersLogoWhite

0

There's no such thing as "jumping" in C++ (the equivalent of the JMP assembly instruction). A goto is quite similar to a jump, but the jump is localised to the current function. If you need to jump outside the function you need to make a function call instead. However a function call is not the same as a jump because functions return to the caller but a jump does not return.

A goto does not use a pointer, it simply "jumps" to the labelled section of code, where the label must reside within the current function. For instance, the following example demonstrates a procedural loop using goto:

unsigned loop=0;

:again

std::cout << ++loop << std::endl;

if (loop != 100) goto again;

Note that a label begins with a colon.

A switch is similar to a goto where an expression is evaluated and control passes to the label that matches the result of that evaluation. For instance, suppose you have the following conditional goto:

unsigned r = rand() %10 + 1; // a random number in the range [1:10]

if (r<3) goto less_than_three;

else if (r<5) goto less_than_five;

else if (r<7) goto less_than_seven;

else if (r<9) goto less_than_nine;

else goto less_than_eleven;

:less_than_three

// ...

:less_than_five

// ...

:less_than_seven

// ...

:less_than_nine

// ...

:less_than_eleven

// ...

Note that if r is less than 3 then it is also less than 5, 7, 9 and 11, so execution passes through all the labels in succession (unless we were to place another goto, a break or a return before the next label). But if we suppose this is exactly what we want, consider that if the value is 9 or 10, then we have to evaluate all the expressions, r<3, r<5, r<7, r<9 and r<11, before we reach the point where we can make the jump. With a switch, we can achieve the same thing more efficiently:

switch (r)

{

case (1):

case (2):

// ...

case (3):

case (4):

// ...

case (5):

case (6):

// ...

case (7):

case (8):

// ...

case (9):

case (10):

// ...

}

Note that switch labels begin with the case keyword and end with a colon (the parenthesis are optional).

If we do not wish our code to flow through to the next case label we must place a goto, break or return statement before the next case label.

We can also choose a default case. For instance, since the last case has to be 9 or 10, we can combine these into a default case:

switch (r)

{

case (1):

case (2):

// ...

case (3):

case (4):

// ...

case (5):

case (6):

// ...

case (7):

case (8):

// ...

default:

// ...

}

The default does not have to be last option, it simply caters for any result not handled by a specific case label. Execution continues from that point until the end of the switch statement, unless a break, return or goto is encountered.

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

What is an address in C plus plus programming?

An address in C or C++ is the location in memory of an object or function. An address is the contents of a pointer, as opposed to the contents of the memory location pointed to by the pointer.


Find the greater no between two nos using pointer in c plus plus programming?

if (*&amp;a &gt; *&amp;b) *&amp;max = *&amp;a; else *&amp;max = *&amp;b;


What is the meaning of star d in c plus plus programming?

If d is a pointer variable, then *d is the value stored in the memory address pointed to by d.


What is step involve is using data file in c plus plus programming?

Declaration of file pointer opening of file in desired mode. performing the desired operation. closing the file


What is Dazzling Pointer in c plus plus?

The pointer that points to a block of memory that does not exist is called a dazzling pointer or wild pointer


What is 'this' pointer in c plus plus?

Address of the current object.


What are the two major types of programming languages in c plus plus?

Object oriented programming and structured programming.


When was Plus - programming language - created?

Plus - programming language - was created in 1976.


What is null object in c plus plus?

a pointer that is not pointing to anything


What changes the pointer to a plus icon in excel?

The application does that when the pointer is over the cells region of the spreadsheet.


What does multiplying a pointer by 5 in C plus plus mean How does it change the pointer's value?

Multiplication is yet another thing, what you should never do with pointers.


Which function is used to determine the position of the put pointer in a file in c plus plus?

The function ftell returns the position of the file pointer for a file.