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

Amdahls law - calculate speed up performance, To calculate speed up perform...

To calculate speed up performance various laws have been developed. These laws are discussed here.  Amdahl's Law Remember, the speed up factor helps us in knowing the relat

Sort Wars, If quicksort is so quick, why bother with anything else? If bubb...

If quicksort is so quick, why bother with anything else? If bubble sort is so bad, why even mention it? For that matter, why are there so many sorting algorithms?

Explain normans execution-evaluation cycle model, Question 1: (a) How ...

Question 1: (a) How would you explain human reasoning? (b) Explain the different types of human reasoning? Question 2: (a) What are the computational factors affec

Find the shortest path, The following is the required interface for the mou...

The following is the required interface for the mouse and cheese problem. Your program is required to read its input from a file named 'maze.txt' In the maze.txt

Discuss about simple mail transfer protocol briefly, Discuss about Simple M...

Discuss about Simple Mail Transfer Protocol briefly. SMTP: It is sands for Simple Mail Transfer Protocol, is a protocol for sending e-mail messages among servers. Most e-

Give some examples of malicious data, Give some examples of malicious data....

Give some examples of malicious data.  In May 2002, the Norton Anti-Virus software for Windows operating systems detects about 61000 malicious programs. Some of them are named

What is python?, Python is an interpreter, interactive, object-oriented pro...

Python is an interpreter, interactive, object-oriented programming language. It is frequently compared to Tcl, P e r l, Scheme or Java. Python combines remarkable power with v

What are the special features of direct rdram, What are the special feature...

What are the special features of Direct RDRAM? It is a two channel Rambus It has 18 data lines intended to transfer two bytes of data at a time There are no divide

What are the advantages of having e-commerce over extranets, What are the a...

What are the advantages of having e-commerce over extranets?     Extranets are about " joining up" the supply chain-suppliers, resellers, distributors, customers, etc.-enabling

Prove using boolean algebra, Q. Prove using Boolean Algebra 1. AB + AC ...

Q. Prove using Boolean Algebra 1. AB + AC + BC' = AC + BC' 2. (A+B+C) (A+B'+C') (A+B+C') (A+B'+C)=A 3. (A+B) (A'+B'+C) + AB = A+B 4. A'C + A'B + AB'C + BC = C + A'B

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