1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Iterative Fibonacci (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Iterative Fibonacci (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Iterative Fibonacci (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="The-First-Example.html" rel="up" title="The First Example">
|
---|
33 | <link href="Complete-Program.html" rel="next" title="Complete Program">
|
---|
34 | <link href="Stack.html" rel="prev" title="Stack">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Iterative-Fibonacci"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Previous: <a href="Stack.html" accesskey="p" rel="prev">Stack</a>, Up: <a href="The-First-Example.html" accesskey="u" rel="up">The First Example</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Example_003a-Iterative-Fibonacci"></span><h3 class="section">1.3 Example: Iterative Fibonacci</h3>
|
---|
64 | <span id="index-iterative-Fibonacci-function"></span>
|
---|
65 | <span id="index-Fibonacci-function_002c-iterative"></span>
|
---|
66 |
|
---|
67 | <p>Here’s a much faster algorithm for computing the same Fibonacci
|
---|
68 | series. It is faster for two reasons. First, it uses <em>iteration</em>
|
---|
69 | (that is, repetition or looping) rather than recursion, so it doesn’t
|
---|
70 | take time for a large number of function calls. But mainly, it is
|
---|
71 | faster because the number of repetitions is small—only <code><var>n</var></code>.
|
---|
72 | </p>
|
---|
73 |
|
---|
74 | <div class="example">
|
---|
75 | <pre class="example">int
|
---|
76 | fib (int n)
|
---|
77 | {
|
---|
78 | int last = 1; /* <span class="roman">Initial value is <code>fib (1)</code>.</span> */
|
---|
79 | int prev = 0; /* <span class="roman">Initial value controls <code>fib (2)</code>.</span> */
|
---|
80 | int i;
|
---|
81 |
|
---|
82 | for (i = 1; i < n; ++i)
|
---|
83 | /* <span class="roman">If <code>n</code> is 1 or less, the loop runs zero times,</span> */
|
---|
84 | /* <span class="roman">since <code>i < n</code> is false the first time.</span> */
|
---|
85 | {
|
---|
86 | /* <span class="roman">Now <code>last</code> is <code>fib (<code>i</code>)</code></span>
|
---|
87 | <span class="roman">and <code>prev</code> is <code>fib (<code>i</code> - 1)</code>.</span> */
|
---|
88 | /* <span class="roman">Compute <code>fib (<code>i</code> + 1)</code>.</span> */
|
---|
89 | int next = prev + last;
|
---|
90 | /* <span class="roman">Shift the values down.</span> */
|
---|
91 | prev = last;
|
---|
92 | last = next;
|
---|
93 | /* <span class="roman">Now <code>last</code> is <code>fib (<code>i</code> + 1)</code></span>
|
---|
94 | <span class="roman">and <code>prev</code> is <code>fib (<code>i</code>)</code>.</span>
|
---|
95 | <span class="roman">But that won’t stay true for long,</span>
|
---|
96 | <span class="roman">because we are about to increment <code>i</code>.</span> */
|
---|
97 | }
|
---|
98 |
|
---|
99 | return last;
|
---|
100 | }
|
---|
101 | </pre></div>
|
---|
102 |
|
---|
103 | <p>This definition computes <code>fib (<var>n</var>)</code> in a time proportional
|
---|
104 | to <code><var>n</var></code>. The comments in the definition explain how it works: it
|
---|
105 | advances through the series, always keeps the last two values in
|
---|
106 | <code>last</code> and <code>prev</code>, and adds them to get the next value.
|
---|
107 | </p>
|
---|
108 | <p>Here are the additional C features that this definition uses:
|
---|
109 | </p>
|
---|
110 | <dl compact="compact">
|
---|
111 | <dt>Internal blocks</dt>
|
---|
112 | <dd><p>Within a function, wherever a statement is called for, you can write a
|
---|
113 | <em>block</em>. It looks like <code>{ <span class="roman">…</span> }</code> and contains zero or
|
---|
114 | more statements and declarations. (You can also use additional
|
---|
115 | blocks as statements in a block.)
|
---|
116 | </p>
|
---|
117 | <p>The function body also counts as a block, which is why it can contain
|
---|
118 | statements and declarations.
|
---|
119 | </p>
|
---|
120 | <p>See <a href="Blocks.html">Blocks</a>.
|
---|
121 | </p>
|
---|
122 | </dd>
|
---|
123 | <dt>Declarations of local variables</dt>
|
---|
124 | <dd><p>This function body contains declarations as well as statements. There
|
---|
125 | are three declarations directly in the function body, as well as a
|
---|
126 | fourth declaration in an internal block. Each starts with <code>int</code>
|
---|
127 | because it declares a variable whose type is integer. One declaration
|
---|
128 | can declare several variables, but each of these declarations is
|
---|
129 | simple and declares just one variable.
|
---|
130 | </p>
|
---|
131 | <p>Variables declared inside a block (either a function body or an
|
---|
132 | internal block) are <em>local variables</em>. These variables exist only
|
---|
133 | within that block; their names are not defined outside the block, and
|
---|
134 | exiting the block deallocates their storage. This example declares
|
---|
135 | four local variables: <code>last</code>, <code>prev</code>, <code>i</code>, and
|
---|
136 | <code>next</code>.
|
---|
137 | </p>
|
---|
138 | <p>The most basic local variable declaration looks like this:
|
---|
139 | </p>
|
---|
140 | <div class="example">
|
---|
141 | <pre class="example"><var>type</var> <var>variablename</var>;
|
---|
142 | </pre></div>
|
---|
143 |
|
---|
144 | <p>For instance,
|
---|
145 | </p>
|
---|
146 | <div class="example">
|
---|
147 | <pre class="example">int i;
|
---|
148 | </pre></div>
|
---|
149 |
|
---|
150 | <p>declares the local variable <code>i</code> as an integer.
|
---|
151 | See <a href="Variable-Declarations.html">Variable Declarations</a>.
|
---|
152 | </p>
|
---|
153 | </dd>
|
---|
154 | <dt>Initializers</dt>
|
---|
155 | <dd><p>When you declare a variable, you can also specify its initial value,
|
---|
156 | like this:
|
---|
157 | </p>
|
---|
158 | <div class="example">
|
---|
159 | <pre class="example"><var>type</var> <var>variablename</var> = <var>value</var>;
|
---|
160 | </pre></div>
|
---|
161 |
|
---|
162 | <p>For instance,
|
---|
163 | </p>
|
---|
164 | <div class="example">
|
---|
165 | <pre class="example">int last = 1;
|
---|
166 | </pre></div>
|
---|
167 |
|
---|
168 | <p>declares the local variable <code>last</code> as an integer (type
|
---|
169 | <code>int</code>) and starts it off with the value 1. See <a href="Initializers.html">Initializers</a>.
|
---|
170 | </p>
|
---|
171 | </dd>
|
---|
172 | <dt>Assignment</dt>
|
---|
173 | <dd><p>Assignment: a specific kind of expression, written with the ‘<samp>=</samp>’
|
---|
174 | operator, that stores a new value in a variable or other place. Thus,
|
---|
175 | </p>
|
---|
176 | <div class="example">
|
---|
177 | <pre class="example"><var>variable</var> = <var>value</var>
|
---|
178 | </pre></div>
|
---|
179 |
|
---|
180 | <p>is an expression that computes <code><var>value</var></code> and stores the value in
|
---|
181 | <code><var>variable</var></code>. See <a href="Assignment-Expressions.html">Assignment Expressions</a>.
|
---|
182 | </p>
|
---|
183 | </dd>
|
---|
184 | <dt>Expression statements</dt>
|
---|
185 | <dd><p>An expression statement is an expression followed by a semicolon.
|
---|
186 | That computes the value of the expression, then ignores the value.
|
---|
187 | </p>
|
---|
188 | <p>An expression statement is useful when the expression changes some
|
---|
189 | data or has other side effects—for instance, with function calls, or
|
---|
190 | with assignments as in this example. See <a href="Expression-Statement.html">Expression Statement</a>.
|
---|
191 | </p>
|
---|
192 | <p>Using an expression with no side effects in an expression statement is
|
---|
193 | pointless except in very special cases. For instance, the expression
|
---|
194 | statement <code>x;</code> would examine the value of <code>x</code> and ignore it.
|
---|
195 | That is not useful.
|
---|
196 | </p>
|
---|
197 | </dd>
|
---|
198 | <dt>Increment operator</dt>
|
---|
199 | <dd><p>The increment operator is ‘<samp>++</samp>’. <code>++i</code> is an
|
---|
200 | expression that is short for <code>i = i + 1</code>.
|
---|
201 | See <a href="Increment_002fDecrement.html">Increment/Decrement</a>.
|
---|
202 | </p>
|
---|
203 | </dd>
|
---|
204 | <dt><code>for</code> statements</dt>
|
---|
205 | <dd><p>A <code>for</code> statement is a clean way of executing a statement
|
---|
206 | repeatedly—a <em>loop</em> (see <a href="Loop-Statements.html">Loop Statements</a>). Specifically,
|
---|
207 | </p>
|
---|
208 | <div class="example">
|
---|
209 | <pre class="example">for (i = 1; i < n; ++i)
|
---|
210 | <var>body</var>
|
---|
211 | </pre></div>
|
---|
212 |
|
---|
213 | <p>means to start by doing <code>i = 1</code> (set <code>i</code> to one) to prepare
|
---|
214 | for the loop. The loop itself consists of
|
---|
215 | </p>
|
---|
216 | <ul>
|
---|
217 | <li> Testing <code>i < n</code> and exiting the loop if that’s false.
|
---|
218 |
|
---|
219 | </li><li> Executing <var>body</var>.
|
---|
220 |
|
---|
221 | </li><li> Advancing the loop (executing <code>++i</code>, which increments <code>i</code>).
|
---|
222 | </li></ul>
|
---|
223 |
|
---|
224 | <p>The net result is to execute <var>body</var> with 0 in <code>i</code>,
|
---|
225 | then with 1 in <code>i</code>, and so on, stopping just before the repetition
|
---|
226 | where <code>i</code> would equal <code>n</code>.
|
---|
227 | </p>
|
---|
228 | <p>The body of the <code>for</code> statement must be one and only one
|
---|
229 | statement. You can’t write two statements in a row there; if you try
|
---|
230 | to, only the first of them will be treated as part of the loop.
|
---|
231 | </p>
|
---|
232 | <p>The way to put multiple statements in those places is to group them
|
---|
233 | with a block, and that’s what we do in this example.
|
---|
234 | </p></dd>
|
---|
235 | </dl>
|
---|
236 |
|
---|
237 | <hr>
|
---|
238 | <div class="header">
|
---|
239 | <p>
|
---|
240 | Previous: <a href="Stack.html" accesskey="p" rel="prev">Stack</a>, Up: <a href="The-First-Example.html" accesskey="u" rel="up">The First Example</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
241 | </div>
|
---|
242 |
|
---|
243 |
|
---|
244 |
|
---|
245 | </body>
|
---|
246 | </html>
|
---|