How to Redirect Code Using Goto Continue C

Lua goto

Introduction to Lua goto

The Lua goto statement is used to jump to the specified label. The goto statement is the jump statement which transfer the control of the program to the specific label. The Lua 5.2.0-beta-rc1 [6] [1] added Lua goto statement and farther Lua 5.2.0-beta-rc2 [7] refine it. This goto statement is a restrictive form in that first restriction is if where we have defined a label is visible in the entire bock. The second restriction is goto can transfer the control to any visible label if it does not enter into the scope of local variable and the third restriction is the goto can not transfer the control into the block outside the bloc.

Syntax of Lua goto:

The syntax of the lua goto where the label first defined and used goto statement to jum to that label:

:: Label :: --defining a label
--farther code
goto Label

The syntax of the lua goto where the goto statement used first to jump to the label which is defined below:

goto Label
--farther code
:: Label :: --defining a label

Parameters:

  • Label: This is not an optional, that specifies the label, it is a user defined identifier, which indicates the target statement and specify the location or line number in the code to jump for the goto statement.

Working of goto Structure in Lua Programming

  • The goto structure used in lua programming to transfer the execution control to the specific location in program.
  • In lua the label is created by prefixing and post fixing by :: as ::label::, and which can be used by the goto statement as "goto label", so where ever this statement is used (either before or after the label define) the execution control jump to this label and the execution start the following statement.

Examples of Lua goto

Given below are the examples mentioned:

Example #1

Example of goto structure in lua programming to show the usage of goto statement.

Code:

-- create a variable and initialize by some value
num = 21
print("The number is : ", num)
-- checking whether the number is even or odd
if (num % 2 == 0) then
-- if num even, then jump to the label even
goto even
else
-- if num odd, then jump to the label odd
goto odd
end
-- define even label
::even::
print("Number is even.")
-- define odd label
::odd::
print("Number is odd.")

Output:

Lua goto 1

As in the above lua program the num variable is created to store the number as 21, next in the code the num variable is checking whether it have even or odd number by using the if statement. If the num have the even number then the execution control jump to the even block, else for the odd number the control jump to the odd block which is performing by using the goto statement. As in the program the num have odd number so it control jump to the odd block and print "Number is odd.", as we can see in the output.

Example #2

Example of goto structure in lua programming to simulate redo and continue usage.

Code:

-- create a variable and initialize by some value
no = 0
print("The number value is : ", no)
-- checking whether the number is less than 6
while no < 6 do
-- create label for the redo
::redo::
no = no + 1
if no%2 == 1 then
-- if i is odd, then jump to the label continue
goto continue
else
print("The next number value is :", no)
-- jump to the label redo
goto redo
end
-- create label for the continue
::continue::
end

Output:

to simulate redo and continue usage

As in the above lua program the no variable is created to store the number as 0, next in the code the no value is incrementing by 1 in the while loop. Inside the while loop two labels are used redo and continue, the redo to repeatedly run the loop and every time jump to it after printing the no, whereas the continue label is created to continue the loop when the no is odd ( as continue skip the following statement in the loop). And so the program printing the even numbers by implementing the redo and continue functionalities.

Example #3

Example of goto structure in lua programming to demonstrate goto cannot jump into the scope of a local variable.

Code:

do
-- to jump to the local_label block
goto local_label
local no = 1
print("The number is : ", no)
-- within the scope of the local variable, so can not jump
::local_label::
no = no + 1
-- The scope of the local variable ends, so you can jump.
::ok::
end

Output:

Lua goto 3

As in the above lua program the goto statement is trying to jump to the local_label which is created inside the local scope, so the local_label is not visible from there and that's way the error is thrown that "<goto local_label> into the scope of local no. where as farther in the code the another label is also created that is "ok", in place of local_label if we used ok label then the control will be jump to ok without any error, because the ok label is defined outside of the local scope.

Conclusion

The Lua goto statement is a built in the lua programming, which is used to jump to the specified label. In the lua the goto statement is more restricted.

Recommended Articles

This is a guide to Lua goto. Here we discuss the introduction, working of goto structure in lua programming and examples respectively. You may also have a look at the following articles to learn more –

  1. Best Programming Languages
  2. #else in C
  3. Web Programming Languages
  4. Programming Errors in C

westconsecter.blogspot.com

Source: https://www.educba.com/lua-goto/

0 Response to "How to Redirect Code Using Goto Continue C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel