Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1


Ratings: (0)|Views: 135 |Likes:
Published by NOOR AHMED

More info:

Categories:Types, School Work
Published by: NOOR AHMED on Feb 12, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC or read online from Scribd
See more
See less


Loops can be created to execute a block of code for a fixed number of times or(e.g. 7 or 17 or 70). Alternatively, loops can be created to repetitively executea block of code until a condition changes to a wanted state. For instance, theloop may continue until a condition changes from false to true, or from true tofalse. In this case, the block of code being executed must update the conditionbeing tested in order for the loop to terminate at some point. If the testcondition does not change somehow within the loop, the loop will neverterminate - the so known 'endless loop'. This is logical error.
loop is used to execute a block of code as long as some condition istrue. If the condition is false from the start the block of code is not executed atal. The while loop tests the condition before it's executed so sometimes theloop may never be executed if initially the condition is not met. Its syntax is asfollows.while (tested condition is satisfied){block of code}In all constructs, curly braces should only be used if the construct is to executemore than one line of code. The above program executes only one line of codeso it not really necessary (same rules apply to if...else constructs) but you canuse it to make the program seem more understandable or readable.Here is a simple example of the use of the while loop. This program countsfrom 1 to 100.#include <stdio.h>int main(void){int count = 1;while (count <= 100){printf("%d\n",count);count += 1; // Notice this statement}return 0;}
Note that no semi-colons ( ; ) are to be used after the while (condition)statement. These loops are very useful because the condition is tested beforeexecution begins. However i never seem to like these loops as they are not asclear to read as the do ...while loops. The while loop is the favorite amongstmost programmers but as for me, i definitely prefer the do ...while loop.
do ....while
The do loop also executes a block of code as long as a condition is satisfied.The difference between a "do ...while" loop and a "while {} " loop is that thewhile loop tests its condition before execution of the contents of the loopbegins; the "do" loop tests its condition after it's been executed at least once.As noted above, if the test condition is false as the while loop is entered theblock of code is never executed. Since the condition is tested at the bottom of a do loop, its block of code is always executed at least once.Some people don't like these loops because it is always executed at least once.When i ask them "so what?", they normally reply that the loop executes evenif the data is incorrect. Basically because the loop is always executed, it willexecute no matter what value or type of data is supposed to be required. The"do ....while" loops syntax is as followsdo{block of code} while (condition is satisfied);Note that a semi-colon ( ; ) must be used at the end of the do ...while loop.This semi-colon is needed because it instructs whether the while (condition)statement is the beginning of a while loop or the end of a do ...while loop.Here is an example of the use of a do loop.include <stdio.h>int main(void){int value, r_digit;printf(“Enter a number to be reversed.\n”);scanf(“%d”, &value);do{r_digit = value % 10;printf(“%d”, r_digit);value = value / 10;} while (value != 0);printf(“\n”);return 0;
The third and last looping construct in C is the for loop. The for loop canexecute a block of code for a fixed or given number of times. Its syntax is asfollows.for (initializations;test conditions;increment value){block of code}The simplest way to understand for loops is to study several examples.First, here is a for loop that counts from 1 to 10.for (count = 1; count <= 10; count++){printf("%d\n",count);}The test conditions may be unrelated to the variables being initialized andupdated. Here is a loop that counts until a user response terminates the loop.for (count = 1; response != 'N'; count++){printf("%d\n",count);printf("Dam man, you still want to continue? (Y/N): \n");scanf("%c",&response);}More complicated test conditions are also allowed. Suppose the user of the lastexample never enters "N", but the loop should terminate when 100 is reached,regardless.for (count = 1; (response != 'N') && (count <= 100); count++){printf("%d\n",count);printf("Dam man, you still want to continue? (Y/N): \n");scanf("%c",&response);}It is also possible to have multiple initializations and multiple actions. This loopstarts one counter at 0 and another at 100, and finds a midpoint betweenthem. (This is advanced material).for (i = 0, j = 100; j != i; i++, j--){printf("i = %d, j = %d\n",i,j);}printf("i = %d, j = %d\n",i,j);

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->