mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-26 17:43:48 +01:00
162 lines
8.8 KiB
HTML
162 lines
8.8 KiB
HTML
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="English">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<title>Netscript Loops and Conditionals — Bitburner 1.0 documentation</title>
|
|
<link rel="stylesheet" href="_static/agogo.css" type="text/css" />
|
|
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT: './',
|
|
VERSION: '1.0',
|
|
COLLAPSE_INDEX: false,
|
|
FILE_SUFFIX: '.html',
|
|
HAS_SOURCE: true,
|
|
SOURCELINK_SUFFIX: '.txt'
|
|
};
|
|
</script>
|
|
<script type="text/javascript" src="_static/jquery.js"></script>
|
|
<script type="text/javascript" src="_static/underscore.js"></script>
|
|
<script type="text/javascript" src="_static/doctools.js"></script>
|
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
|
<link rel="index" title="Index" href="genindex.html" />
|
|
<link rel="search" title="Search" href="search.html" />
|
|
<link rel="next" title="Netscript Script Arguments" href="netscriptscriptarguments.html" />
|
|
<link rel="prev" title="Netscript Operators" href="netscriptoperators.html" />
|
|
</head>
|
|
<body>
|
|
<div class="header-wrapper" role="banner">
|
|
<div class="header">
|
|
<div class="headertitle"><a
|
|
href="index.html">Bitburner 1.0 documentation</a></div>
|
|
<div class="rel" role="navigation" aria-label="related navigation">
|
|
<a href="netscriptoperators.html" title="Netscript Operators"
|
|
accesskey="P">previous</a> |
|
|
<a href="netscriptscriptarguments.html" title="Netscript Script Arguments"
|
|
accesskey="N">next</a> |
|
|
<a href="genindex.html" title="General Index"
|
|
accesskey="I">index</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="content-wrapper">
|
|
<div class="content">
|
|
<div class="document">
|
|
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body" role="main">
|
|
|
|
<div class="section" id="netscript-loops-and-conditionals">
|
|
<h1>Netscript Loops and Conditionals<a class="headerlink" href="#netscript-loops-and-conditionals" title="Permalink to this headline">¶</a></h1>
|
|
<div class="section" id="loops-and-conditionals">
|
|
<h2>Loops and Conditionals<a class="headerlink" href="#loops-and-conditionals" title="Permalink to this headline">¶</a></h2>
|
|
<p>Netscript loops and conditionals are the same as Javascript. However, the one caveat is that when declaring variables such as the
|
|
iterator for traversing a loop, you should not use the 'var' or 'let' keyword. For reference, you can see the Javascript
|
|
documentation for loops/conditionals here:</p>
|
|
<p><a class="reference external" href="https://www.w3schools.com/js/js_loop_while.asp">While loops</a></p>
|
|
<p><a class="reference external" href="https://www.w3schools.com/js/js_loop_for.asp">For loops</a></p>
|
|
<p><a class="reference external" href="https://www.w3schools.com/js/js_if_else.asp">Conditionals (If/Else statements)</a></p>
|
|
<p>Here are some simple code examples that show the use of loops and conditionals in Netscript.</p>
|
|
<p>The following is a while loop that runs the hack() Netscript function ten times:</p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
|
|
<span class="k">while</span> <span class="p">(</span><span class="n">i</span> <span class="o"><</span> <span class="mi">10</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">hack</span><span class="p">(</span><span class="s1">'foodnstuff'</span><span class="p">);</span>
|
|
<span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The following is a for loop that runs the hack() Netscript function ten times:</p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="mi">10</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">hack</span><span class="p">(</span><span class="s2">"foodnstuff"</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The following is a conditional that uses the getServerMoneyAvailable() Netscript function to check how much money
|
|
exists on the 'foodnstuff' server. If there is more than $200,000 on the server, then the server will be hacked.
|
|
Otherwise, the money available on the server will be grown using the grow() Netscript function:</p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="n">getServerMoneyAvailable</span><span class="p">(</span><span class="s1">'foodnstuff'</span><span class="p">)</span> <span class="o">></span> <span class="mi">200000</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">hack</span><span class="p">(</span><span class="s2">"foodnstuff"</span><span class="p">);</span>
|
|
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
|
|
<span class="n">grow</span><span class="p">(</span><span class="s2">"foodnstuff"</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sidebar">
|
|
<h3>Table Of Contents</h3>
|
|
<p class="caption"><span class="caption-text">Contents:</span></p>
|
|
<ul class="current">
|
|
<li class="toctree-l1 current"><a class="reference internal" href="netscript.html"> Netscript</a><ul class="current">
|
|
<li class="toctree-l2"><a class="reference internal" href="netscriptdatatypes.html"> Data Types and Variables</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="netscriptoperators.html"> Operators</a></li>
|
|
<li class="toctree-l2 current"><a class="current reference internal" href="#"> Loops and Conditionals</a><ul>
|
|
<li class="toctree-l3"><a class="reference internal" href="#loops-and-conditionals">Loops and Conditionals</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l2"><a class="reference internal" href="netscriptscriptarguments.html"> Script Arguments</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="netscriptfunctions.html"> Basic Functions</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="netscriptadvancedfunctions.html"> Advanced Functions</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="netscripthacknetnodeapi.html"> Hacknet Node API</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="netscriptixapi.html"> Trade Information eXchange (TIX) API</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="netscriptsingularityfunctions.html"> Singularity Functions</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<div role="search">
|
|
<h3 style="margin-top: 1.5em;">Search</h3>
|
|
<form class="search" action="search.html" method="get">
|
|
<input type="text" name="q" />
|
|
<input type="submit" value="Go" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="footer-wrapper">
|
|
<div class="footer">
|
|
<div class="left">
|
|
<div role="navigation" aria-label="related navigaton">
|
|
<a href="netscriptoperators.html" title="Netscript Operators"
|
|
>previous</a> |
|
|
<a href="netscriptscriptarguments.html" title="Netscript Script Arguments"
|
|
>next</a> |
|
|
<a href="genindex.html" title="General Index"
|
|
>index</a>
|
|
</div>
|
|
<div role="note" aria-label="source link">
|
|
<br/>
|
|
<a href="_sources/netscriptloopsandconditionals.rst.txt"
|
|
rel="nofollow">Show Source</a>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="right">
|
|
|
|
<div class="footer" role="contentinfo">
|
|
© Copyright 2017, Bitburner.
|
|
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.4.
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
</div>
|
|
|
|
</body>
|
|
</html> |