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

How are standard query operators useful in linq, Standard Query Operators i...

Standard Query Operators in LINQ can be used for working with collections for any of the following and more. 1. Get total count of elements in a collection. 2. Order the resu

Explain the real time process control, Explain the Real time process contro...

Explain the Real time process control Process control is a real time system. Though this is very different to real time (transaction) processing; this system generally involves

How do you find out the flaw, How do you find out the flaw, which of the ad...

How do you find out the flaw, which of the address getting written wrongly. Fill the full memory once with either random data or sequential data then after filling totally rea

Use verilog function to define the width of a multi-bit port, Can I use a V...

Can I use a Verilog function to define the width of a multi-bit port, wire, or reg type? Width  elements  of  ports,  wire  or  reg  declarations  require  a  constant  in  bot

Find out the excess code of decimal number, The excess 3 code of decimal nu...

The excess 3 code of decimal number 26 is ? Ans. (26) 10 in BCD is (00100110) BCD Add 011 to all BCD 01011001 for excess - 3

Relational algebra expressions , Write Relational algebra expressions for t...

Write Relational algebra expressions for the given queries. (i) List the employee numbers, employee names and their manager numbers working in several branches who earn more than

Determine the decimal equivalent of binary number, The decimal equivalent o...

The decimal equivalent of Binary number 11010 is ? Ans. 11010 = 1 X 2 4 + 1 X 2 3 + 0 X 2 2 + 1 X 2 1 = 26.

Define software is in machine language or not, Define Software is in machin...

Define Software is in machine language or not Software is in machine language, today it is often developed by first writing in a high-level language or an assembly language or

Replacement policy - cache memories , Replacement policy: On a particul...

Replacement policy: On a particular cache miss we require to evict a line to build room for the new line "In an A-way set associative cache, we have A option of which block

Add references dialog box, What is the difference among "using System.Data;...

What is the difference among "using System.Data;" and directly adding the reference from "Add References Dialog Box"?  When you compile a program using command line, you add th

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