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

Compute the positive integer and square root, Question: Q1) Write a cod...

Question: Q1) Write a code that asks the user for a positive integer, computes the square root of that integer, and return the result to the user. The computational error needs

Define google calendar, Define Google calendar Google Calendar is free,...

Define Google calendar Google Calendar is free, full featured, and easy to use. It lets the user create both personal and shared calendars, which makes it ideal for tracking bu

What are the differences between one hot and binary encoding, What are the ...

What are the differences between one hot and binary encoding? Common classifications used to explain the state encoding of an FSM is Binary or highly encoded and one hot. A bin

C, "Super ASCII", if it contains the character frequency equal to their asc...

"Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''a''-''z'') and the ascii values will starts from

Explain a schematic diagram of thousand line exchanges, Using a combination...

Using a combination of uniselectors and two motion selectors draw a schematic of thousand line exchanges. The schematic diagram for such an exchange is demonstrated in Fig. All

Explain the commonly used code optimization techniques, Explain briefly any...

Explain briefly any three of the commonly used code optimization techniques. 1. Common sub expression elimination: In given expression as "(a+b)-(a+b)/4", in such "common

Device drivers in ms-dos, In MS-DOS device drivers are installed and loaded...

In MS-DOS device drivers are installed and loaded dynamically it implies that they are loaded into memory when computer is started or re-booted and accessed by operating system as

Illustration of equivalent exe program, Q. Illustration of equivalent EXE p...

Q. Illustration of equivalent EXE program? An illustration of equivalent EXE program for COM program is: ; ABSTRACT this program adds 2 8-bit numbers in the memory locations

Set screen and leave screen, What happens if only one of the commands SET S...

What happens if only one of the commands SET SCREEN and LEAVE SCREEN is used without using the other? If we use SET SCREEN without LEAVE SCREEN, the program ends processing for

What is enhanced ide, Q. What is Enhanced IDE? The principle behind EID...

Q. What is Enhanced IDE? The principle behind EIDE interface is the same as in IDE interface however this drive has capacities varying from 10.2GB to 20.5GB. The rotation speed

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