Introduction:
for Statement:
The ‘for’ statement is a somewhat general while that lets us place the initialization and increment portions of a loop into a single statement all along with the test. The common form of the for is
for( initialization; expression; increment ) statement
The meaning is precisely:
initialization; while( expression ) { statement increment; }
This slightly more elaborate illustration adds up the elements of an array:
sum = 0; for( i=0; i<n; i++)sum = sum + array[i];
In the ‘for’ statement, the initialization can be left out when you want, however the semicolon has to be there. The increment is as well optional. It is not followed by the semicolon. The second clause, the test, works in similar way as in the while: if the expression is true (that is, not zero) do another loop, or else get on with the next statement. Since with the while, the for loop might be done zero times. If the expression is left out, it is taken to be for all time true, therefore,
for( ; ; ) ... while( 1 ) ...
are both infinite loops.
You valor ask why we employ a for as it is so much like a while. (You may too ask why we use a while because...) The for is generally preferable as it keeps the code where it is employed and sometimes removes the requirement for compound statements, as in this code that zeros a 2-dimensional array:
for( i=0; i<n; i++ ) for( j=0; j<m; j++ ) array[i][j] = 0;Functions; Comments:
Assume that we want, as part of a big program, to count the occurrences of the ASCII characters in some input text. Let us as well map illegal characters (such with value>127 or <0) into one pile. As this is presumably an isolated portion of the program, good practice dictates making it a separate function. Here is one method:
main( ) { int hist[129]; /* 128 legal chars + 1 illegal group*/ ... count(hist, 128); /* count the letters into hist */ printf( ... ); /* comments look like this; use them */ ... /* anywhere blanks, tabs or newlines could appear */ } count(buf, size) int size, buf[ ]; { int i, c; for( i=0; i<=size; i++ ) buf[i] = 0; /* set buf to zero */ while( (c=getchar( )) != '\0' ) { /* read til eof */ if( c > size || c < 0 ) c = size; /* fix illegal input */ buf[c]++; } return; } We know many instances of calling a function; therefore let us concentrate on how to define one. As count has two arguments, we require declaring them, as shown, giving their types, and in the case of buf, the fact which it is an array. The declarations of arguments go among the argument list and the opening `{'. There is no requirement to specify the size of the array buf, for it is defined exterior of count.
The return statement merely says to go back to calling routine. However, we could have omitted it, as a return is implied at the end of a function.
What if we required count to return a value, state the number of characters read? The return statement permits for this too:
int i, c, nchar; nchar = 0; ... while( (c=getchar( )) != '\0' ) { if( c > size || c < 0 ) c = size; buf[c]++; nchar++; } return(nchar);
Any expression can emerge in the parentheses. Here is a function to evaluate the minimum of two integers:
min(a, b) int a, b; { return( a < b ? a : b ); }
To copy a character array, we write the function:
strcopy(s1, s2) /* copies s1 to s2 */ char s1[ ], s2[ ]; { int i; for( i = 0; (s2[i] = s1[i]) != '\0'; i++ ); }
As is frequently the case, all the work is completed by the assignment statement embedded in the test portion of the for. Again, the declarations of arguments s1 and s2 omit the sizes, since they do not matter to strcopy.
There is a subtlety in function usage that can trap the unsuspecting FORTRAN programmer. Simple variables (that is, not arrays) are passed in C by ‘call by value’, that means that the called function is given a copy of its arguments, and does not know their addresses. This makes it unfeasible to modify the value of one of the real input arguments.
There are two methods to out of this problem. One is to make special arrangements to pass to the function, the address of a variable rather than its value. The other is to make the variable a global or an external variable that is known to each function by its name.
Latest technology based Programming Languages Online Tutoring Assistance
Tutors, at the www.tutorsglobe.com, take pledge to provide full satisfaction and assurance in Programming Languages help via online tutoring. Students are getting 100% satisfaction by online tutors across the globe. Here you can get homework help for Programming Languages, project ideas and tutorials. We provide email based Programming Languages help. You can join us to ask queries 24x7 with live, experienced and qualified online tutors specialized in Programming Languages. Through Online Tutoring, you would be able to complete your homework or assignments at your home. Tutors at the TutorsGlobe are committed to provide the best quality online tutoring assistance for Programming Languages Homework help and assignment help services. They use their experience, as they have solved thousands of the Programming Languages assignments, which may help you to solve your complex issues of Programming Languages. TutorsGlobe assure for the best quality compliance to your homework. Compromise with quality is not in our dictionary. If we feel that we are not able to provide the homework help as per the deadline or given instruction by the student, we refund the money of the student without any delay.
Distribution of Natural gases tutorial all along with the key concepts of Petroleum Industry, Oil Tanker, World Natural Gas distribution and production, World Natural Gas consumption and trade
Disconnect the supply cables at the motor’s terminal box, separate the motor from the driven machine, detach the foundation bolts or nuts and eliminate the motor to the maintenance shop.
periodic table-gradations of atomic properties tutorial all along with the key concepts of electronic configuration, periodic table and atomic properties, ionization energy, electron affinity and electronegativity
tutorsglobe.com second law of thermodynamics assignment help-homework help by online chemical thermodynamics tutors
Mechanism of Simple Reactions tutorial all along with the key concepts of Unimolecular Reaction, Bimolecular Reaction, Free-Radical Reactions, Consecutive Reactions, Opposing Reactions and Parallel Reactions
Physics of Lasers tutorial all along with the key concepts of Light Emission and Absorption, Einstein's Prediction, Prerequisites for a Laser, Types of Lasers, Application of lasers
conduction of heat tutorial all along with the key concepts of Conduction of Heat Energy, Factors Affecting Conduction, Thermal Conductivity of a Material, Lagged and Unlagged Bars, Measuring the Thermal Conductivity of a Good Conductor
Human helminths infections tutorial all along with the key concepts of Nematode Infections, Enterobius vermicularis, Ascaris lumbricoides, Trichuris trichiuria, Lymphatic filariasis, Onchocerca volvulus, Digenean Trematode Infections and Cestode Infections
Theory and lecture notes of International Economic Policy all along with the key concepts of Classical Gold Standard, Contractionary Policies, Collapse of Gold Standard, Bretton Woods System. Tutorsglobe offers homework help, assignment help and tutor’s assistance on International Economic Policy.
tutorsglobe.com association of kp and kc assignment help-homework help by online attainment of equilibrium in chemical reactions tutors
Theory and lecture notes on the concept of Frequency Response Considerations all along with the key concepts of Single Inverting Gain phase, Feedback and Stability, Two-Stage Non-Inverting Amplifier, Gain Margin and Phase Margin. Tutorsglobe offers homework help, assignment help and tutor’s assistance on Frequency Response Considerations.
build up a single phase single layer concentric winding for a 4 pole ac machine comprising 24 slots.
Theory and lecture notes of Externalities and Public Goods all along with the key concepts of Market Failure, Resource Allocation, Market Performance, Imperfect Competition, Externalities. Tutorsglobe offers homework help, assignment help and tutor’s assistance on Externalities and Public Goods.
tutorsglobe.com reabsorption in proximal convoluted tubule assignment help-homework help by online mechanism of urine formation tutors
Hire the best tutors of Britain 1688 revolution Assignment Help service and get assured top-notch grades at reasonable rates.
1950252
Questions Asked
3689
Tutors
1466610
Questions Answered
Start Excelling in your courses, Ask an Expert and get answers for your homework and assignments!!