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

Calculations for a standard vga graphics screen, Q. Calculations for a stan...

Q. Calculations for a standard VGA graphics screen? Let's do the calculations for a standard VGA graphics screen (640×480) using 16 colours. Total number of Pixels = 640 ×48

How do you recognize the performance bottlenecks, Performance Bottlenecks c...

Performance Bottlenecks can be identified by using monitors. These monitors might be application server monitors, database server monitors, web server monitors and network monitors

Data base, why to learn data base?

why to learn data base?

Instruction per cycle in RISC, Q. Instruction per cycle in RISC? One in...

Q. Instruction per cycle in RISC? One instruction per cycle: A machine cycle is total time taken to fetch two operands from registers perform ALU operation on them and store re

What are problems of clock skew, What are problems of clock skew? This ...

What are problems of clock skew? This is typically because of two causes. The primary is a material flaw that causes a signal to travel faster or slower than imagined. The seco

Decision trees, Decision Trees: Visualize you only ever do four things...

Decision Trees: Visualize you only ever do four things at the weekend such as go shopping or watch a movie or play tennis and just stay in. In such case what you do depends on

How debugger works internally, Whenever we compile with -g option, it will ...

Whenever we compile with -g option, it will make a symbol table, and according that table for every function and line it will call ptrace.

Explain the term confidentiality - firewall design policy, Explain the term...

Explain the term Confidentiality - Firewall Design Policy Whilst some corporate data is for public consumption, the vast majority of it should remain private.

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