Define the for loop, Computer Engineering

Assignment Help:

The for Loop

The for loop works well where the number of iterations of the loop is known before the loop is entered. The head of the loop consists of three parts separated by semicolons.

  • The first is run before the loop is entered. This is usually the initialization of the loop variable.
  • The second is a test, the loop is exited when this returns false.
  • The third is a statement to be run every time the loop body is completed. This is usually an increment of the loop counter.

The example is a function which calculates the average of the numbers stored in an array. The function takes the array and the number of elements as arguments.

float average(float array[], int count)

{      float total = 0.0;

int i;

for(i = 0; i < count; i++)

total += array[i];

return(total / count);

 }

The for loop ensures that the correct number of array elements are added up before calculating the average.

However The three statements at the head of a for loop usually do just one thing each, any  of  them  can  be  left  blank.  A  blank  first  or  last  statement  will  mean  no initialisation or running increment. A blank comparison statement will always be treated as true. This will cause the loop to run indefinitely unless interrupted by some other means. This might be a return or a break statement.

It is also possible to squeeze several statements into the first or third position, separating them with commas. This allows a loop with more than one controlling variable. The example below illustrates the definition of such a loop, with variables hi and lo starting at 100 and 0 respectively and converging.

for (hi = 100, lo = 0; hi >= lo; hi--, lo++)

The for loop is extremely flexible and allows many types of program behaviour to be specified simply and quickly.

 


Related Discussions:- Define the for loop

Signals and systems, Im doing computer engineering in Thailand.. Second yea...

Im doing computer engineering in Thailand.. Second year, and got project regarding sysnal and systems.. I and my team mates have no idea on what should we do.. Could u kindly sugge

Subtraction of 01000-01001 using 2's complement method, Subtraction of 0100...

Subtraction of 01000-01001 using 2's complement method. Ans. Firstly 1's complement of 01001 is 10110 and 2's complement is 10110+ 1 =10111. Thus   01000 =  01000 - 01001

Explain about associative memory organisations, Q. Explain about Associativ...

Q. Explain about Associative Memory Organisations? The associative memory is arranged in w words with b bits per word. In w x b array, every bit is known as a cell. Every cell

Determine reduced boolean equation and the karnaugh map, Determine reduced ...

Determine reduced Boolean equation and the Karnaugh Map? Illustration : Determine reduced Boolean equation and the Karnaugh Map for the truth table shown below:

Calculating block size - computer architecture, Calculating Block Size: ...

Calculating Block Size: B A unit of associatively -one tag for B A words B T unit of transfer-B T words to/from primary memory as a unit             One valid bit for

Types of port, Define port? Give types of port available? Ans: An I/O in...

Define port? Give types of port available? Ans: An I/O interface consists of circuitry needed to connect an I/O device to computer bus. One side consists of a data path contain

Design of structure chart, Design of structure chart - Standard qualit...

Design of structure chart - Standard quality structure charts result in programs that are modular, reusable and simple to execute. - Measures include: Cohesion

Selecting integerated learning experiences, Q. Selecting integerated learni...

Q. Selecting integerated learning experiences? It is important that we select appropriate experiences for children, more so those that are from the child's world and emerge fro

What is rational administrator, Use the Rational Administrator to: * M...

Use the Rational Administrator to: * Make and manage projects. * Make a project under configuration management. * Make a project outside of configuration management.

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd