-4

I just started learning c++. [MinGw, c++14].

If I want to transfer the control of a program to certain part of the code such that the control doesn't flow to any other part of the code. which is more efficient ?

  1. Using switch with goto statement and dividing the program into fragments by using {..}
  2. Using switch with function call

pls. suggest if there's an other way more efficient than these 2 mentioned above!

Sinister
  • 1
  • 6

3 Answers3

1

I totally depend upon what you want to do. As you might know, everything that is achieved using goto can be done using the 3 flows sequences, selection and iteration it's not a good practice to use goto as it violates structured programming. As for purely choosing which would be more efficient in terms of performance goto might lead by a very insignificant margin.

0

Break your codes into Functions or methods

return_type function_name(args..){

//code goes here }

then user if else or switch to call those functions or methods

Mohan
  • 41
  • 3
0

You can use any of them either by function or goto. It depends upon your program concept. You can do it in a very practical way as you've started learning. Please try to run a same program by goto first than try to run by calling function instead of goto. You'll better understand it and one more thing try to make very simple and easy programs. This approach will increase your programming understanding. I recommend this site to any beginner https://www.geeksforgeeks.org