Trending October 2023 # How Does The Assert Function Work In Lua? # Suggested November 2023 # Top 10 Popular | Vibergotobrazil.com

# Trending October 2023 # How Does The Assert Function Work In Lua? # Suggested November 2023 # Top 10 Popular

You are reading the article How Does The Assert Function Work In Lua? updated in October 2023 on the website Vibergotobrazil.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Does The Assert Function Work In Lua?

Definition of Lua assert Function

Lua assert is the function to use in the error handling of the Lua programming language. This is the function helping to handle the runtime error of the Lua source code to avoid complications between compile-time errors and run-time errors. This keyword helps to return the true value or condition of the source code otherwise displays the error message of assert. This is the function that declares the error message of the condition when values of the Lua assert argument is failed. It works on Boolean condition error handling conditions in the Lua source code. It returns the final statements if the given value is true otherwise asserts, declaring the error message in the output screen.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

The syntax is below.

assert ( value or condition statement, [ message of the error])

Example: assert(type(firstvariable) = “string”, “the value of argument a is not string”)

assert ( value or condition statement, [ message of the error]);

Example: assert(type(firstvariable) = “string”, “the value of argument a is not string”);

Description:

The Lua has compile-time errors and run-time errors to handle the complicated errors. The Lua assert handles the run time error and declares the assertion error. The “assert” keyword is useful for showing the Boolean condition of the declaration statement. The “value or condition statement” is used for declaring the value of the argument or Boolean condition which is true. The “message of the error” is used for declaring the error message when the Boolean condition is failed. The semicolon symbol does not need at the last of the assert function, but we can use Lua’s assert syntax with or without the semicolon symbol.

How does the assert function work in Lua?

Step 1: Lua programming IDE Environmental Setup.

The Lua text editor, Lua compiler, and Lua interpreter install in your computer as per the operating system and software version.

Or

If you do not have software then you can use Lua Online IDEs for coding and start Lua programming.

Step 2: Create the Lua File.

The Lua file creates with the .lua extension and writes a source code.

Step 3: Use Lua assert in the source code.

Initialize the Lua variable with a value.

local _vaiable1 = 10;

The syntax is used as per the user’s requirement.

Assert (_variable1 == 12, "the variable is not greater than 12 ")

Print the variable value.

Print ( -variable1 )

If the Boolean condition is true, then the variable value is the display.

If the Boolean value is failed, then an error message is displayed.

Examples

Let us discuss examples.

Example #1

The basic Lua asserts with condition examples and output.

Part 1: if the condition is Boolean then Lua assert work in the source code.

print("Lua assert example is here") local _variable1 = 10 assert( _variable1 == 12, "the variable is not greater than 12 ") print( _variable1 )

Output:

print("Lua assert example is here") local _variable1 = 10 assert( _variable1 <= 12, "the variable is not greater than 12 ") print( _variable1 ) print( _variable1 )

Output:

Description:

The part1 and part 2 are shows the outputs of the different conditions using Lua assert. The given variable is equal to require value the output is the given value otherwise, assert declare error message. If a given variable is fulfilled the “less than” or “greater than condition, then the initial value declares in the display window.” If a given variable does not fulfill the condition, then the assert error message displayed in the output window.

Example #2

the basic Lua assert with two variable examples and output.

Part 1: if Lua assert applies on the first variable, then the output is below.

print("Lua assert example is here") function myChunk(_str1, _str2) assert(type( _str1 ) == "string", " first variable is not a string ") assert(type( _str2 ) == "string ", "second variable is not a string ") return _str1,_str2 end print(myChunk(6, "my variable"))

Output:

Part 2: if Lua assert applies on the second variable, then the output is below.

print("Lua assert example is here") function myChunk(_str1, _str2) assert(type( _str1 ) == "string", " first variable is not a string ") assert(type( _str2 ) == "string ", "second variable is not a string ") return _str1,_str2 end print(myChunk( "my variable", 8))

Output:

Part 3: if Lua assert applies on both variables then the output is below.

print("Lua assert example is here") function myChunk(_str1, _str2) assert(type( _str1 ) == "string", " first variable is not a string ") assert(type( _str2 ) == "string ", "second variable is not a string ") return _str1,_str2 end print(myChunk( "lua variable", "my variable"))

Description:

You can see the difference between the three example condition and their output. If one variable failed in the condition, then the respective Assert message is declared in the output window. If both variables failed in the condition, then the first Assert error message is declared in the output window.

Example #3

the basic Lua asserts with file handling examples and output.

Part 1: open input/output file with Lua assert example, and output is below.

print("Lua file with assert") file = assert(io.open("no file available", "r+")) print(file())

Output:

Part 2: open input/output file without Lua assert example, and output is below.

print("Lua file without assert") file = io.open("no file available", "r+") print(file())

Output:

Description:

If the Lua file is trying to open in the source code but the file is not available, then shows the difference between with or without Lua assert. If the file is not available, then it gives an understandable error message includes the file name. If the file is not available and we do not apply to assert then the output window shows a complicating error message.

Conclusion

It helps to avoid complicated errors in the lengthy source code. It is useful to avoid runtime errors and declared an understandable error message. It makes a web application readable, systematic, and neat.

Recommended Articles

We hope that this EDUCBA information on “Lua assert” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

You're reading How Does The Assert Function Work In Lua?

Update the detailed information about How Does The Assert Function Work In Lua? on the Vibergotobrazil.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!