bitburner-src/doc/build/html/netscriptloopsandconditionals.html
2018-05-02 12:38:11 -05:00

158 lines
8.7 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 &#8212; 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>
<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">&lt;</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">&#39;foodnstuff&#39;</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">&lt;</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">&quot;foodnstuff&quot;</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">&#39;foodnstuff&#39;</span><span class="p">)</span> <span class="o">&gt;</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">&quot;foodnstuff&quot;</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">&quot;foodnstuff&quot;</span><span class="p">);</span>
<span class="p">}</span>
</pre></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></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>
<li class="toctree-l2"><a class="reference internal" href="netscriptmisc.html"> Miscellaneous</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="shortcuts.html"> Keyboard Shortcuts</a></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">
&#169; 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>