Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
NOP (code)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Code == A function or a sequence of programming language statements is a NOP or null statement if it has no effect. Null statements may be required by the [[programming language syntax|syntax]] of some languages in certain contexts. === Ada === In [[Ada (programming language)|Ada]], the <code>null</code> statement serves as a NOP.<ref>[http://www.adaic.org/resources/add_content/standards/05aarm/html/AA-5-1.html#S0134 Ada Reference Manual — null statements]. "The execution of a null_statement has no effect."</ref> As the syntax forbids that control statements or functions be empty, the <code>null</code> statement must be used to specify that no action is required. (Thus, if the programmer forgets to write a sequence of statements, the program will fail to compile.) === C and derivatives === The simplest NOP statement in C is the ''null statement'', which is just a semi-colon in a context requiring a statement. Most C compilers generate no code for null statements, which has historical and performance reasons. ; An empty block (compound statement) is also a NOP, and may be more legible, but will still have no code generated for it by the compiler. {} In some cases, such as the body of a function, a block must be used, but this can be empty. In C, statements cannot be empty—simple statements must end with a <code>;</code> (semicolon) while compound statements are enclosed in <code>{}</code> (braces), which does not itself need a following semicolon. Thus in contexts where a statement is grammatically required, some such null statement can be used. The null statement is useless by itself, but it can have a syntactic use in a wider context, e.g., within the context of a loop: <syntaxhighlight lang="c"> while (getchar() != '\n') {} </syntaxhighlight> alternatively, <syntaxhighlight lang="c"> while (getchar() != '\n') ; </syntaxhighlight> or more tersely: <syntaxhighlight lang="c"> while (getchar() != '\n'); </syntaxhighlight> (note that the last form may be confusing, and as such generates a warning with some compilers or compiler options, as semicolon usually indicates an end of function call instruction when placed after a parenthesis on the end of line). The above code continues calling the function <code>getchar()</code> until it returns a <code>\n</code> (newline) character, essentially fast-forwarding the current reading location of standard input to the beginning of next line. === Fortran === In [[Fortran]], the <code>CONTINUE</code> statement is used in some contexts such as the last statement in a DO loop, although it can be used anywhere, and does not have any functionality. === JavaScript === The [[JavaScript]] language does not have a built-in NOP statement. Many implementations are possible: * Use the <code>;</code> ''empty statement''<ref>MDN JavaScript reference – [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/Empty empty statement]. "The empty statement is a semicolon (<code>;</code>) indicating that no statement will be executed, even if JavaScript syntax requires one."</ref> or the <code>{}</code> empty ''block statement'' the same way as in the [[NOP (code)#C_and_derivatives|C and derivatives]] examples; * Use the <code>undefined</code> or the <code>null</code> expression as a complete statement (an ''expression statement'') when the previous methods are not allowed by the syntax. Alternatives, in situations where a function is required, are: * Use the <code>Function.prototype()</code> built-in function, that accepts any arguments and returns <code>undefined</code>;<ref>ECMAScript Language Specification – Edition 5.1 – [https://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4 Properties of the Function Prototype Object]</ref> * Use a NOP function available in a third-party library —see below; * Define a custom NOP function, as in the following example (using the [[ES6]] arrow function syntax): <syntaxhighlight lang="javascript">const noop = () => {};</syntaxhighlight> ==== AngularJS ==== The [[AngularJS]] framework provides [https://docs.angularjs.org/api/ng/function/angular.noop angular.noop] function that performs no operations. ==== jQuery ==== The [[jQuery]] library provides a function <code>jQuery.noop()</code>, which does nothing.<ref>[http://api.jquery.com/jquery.noop/ jQuery.noop()] from jQuery API documentation</ref> ==== Lodash ==== The [[Lodash]] library provides a function <code>_.noop()</code>, which returns undefined and does nothing.<ref>{{Cite web|url=https://lodash.com/docs/#noop|title=Lodash Documentation|website=lodash.com|language=en|access-date=2017-12-15}}</ref> === Pascal === As with C, the ; used by itself can be used as a null statement in [[Pascal (programming language)|Pascal]]. In fact, due to the specification of the language, in a BEGIN / END block, the semicolon is optional before the END statement, thus a semicolon used there is superfluous. Also, a block consisting of <code> BEGIN END;</code> may be used as a placeholder to indicate no action, even if placed inside another BEGIN / END block. === Python === The [[Python (programming language)|Python]] programming language has a [[Python (programming language)#Statements and control flow|<code>pass</code> statement]] which has no effect when executed and thus serves as a NOP. It is primarily used to ensure correct syntax due to Python's [[off-side rule|indentation-sensitive syntax]]; for example the syntax for definition of a [[class (computer programming)|class]] requires an indented block with the class logic, which has to be expressed as <code>pass</code> when it should be empty. === Shell scripting (bash, zsh, etc.) === The '<code>:</code>' [colon] command is a shell builtin that has similar effect to a "NOP" (a do-nothing operation). It is not technically an NOP, as it changes the special parameter $? (exit status of last command) to 0. It may be considered a synonym for the shell builtin 'true', and its exit status is true (0).<ref>[http://tldp.org/LDP/abs/html/special-chars.html Advanced Bash-Scripting Guide > Chapter 3. Special Characters]</ref><ref>bash manpage > SHELL BUILTIN COMMANDS</ref><ref>zsh manpage (zshbuiltins) > SHELL BUILTIN COMMANDS</ref> === TeX macro language (ConTeXt, LaTeX, etc.) === The [[TeX]] typographical system's macro language has the <code>\relax</code> command.<ref>{{cite book |last1=Bausum |first1=David |title=TeX Reference Manual |date=2002 |publisher=Kluwer Academic Publishers |url=https://www.tug.org/utilities/plain/cseq.html#relax-rp |access-date=1 April 2020 |chapter=TeX Primitive Control Sequences |quote=According to The TeXbook, 'TeX does nothing' when it encounters <code>\relax</code>. Actually, <code>\relax</code> may tell TeX, 'This is the end of what you've been doing'.}}</ref> It does nothing by itself, but may be used to prevent the immediately preceding command from parsing any subsequent tokens.<ref>TeX wikibook – [https://en.wikibooks.org/wiki/TeX/relax relax]</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)