473,466 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

semi colon, the difference in loop without body

8 New Member
Hi everyone

the book that i'm learning from says that this for loop has no body,
first of all, I don't even no what that means, this for loop looks quit
the usual.

besides the above issue another bazaar thing is that all other for loops
do not have a semi-colon after the brackets, this one does and if
i remove it i get a funny output, if i place the semi-colon there i get
SUM IS 55 as an output and I dont understand why.
help please.

#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i, sum = 0;

//sum the numbers from 1 to 10
for(i=1; i <= 10; sum += i++);
cout << "sum is " << sum;

cin.get();
cin.get();
return 0;
}
Apr 16 '10 #1
9 9566
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
If you end the loop with semicolon that means end of line

if you want some calculation to be performed for say 10 times. Your loop should looks like this
Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<10;i++)
  2. {
  3. //Your code
  4. }
  5.  
sum = 55 is a correct answer
Traverse your loop. Loop is calculating the value correctly.

Expand|Select|Wrap|Line Numbers
  1. sum = sum + i
  2. sum = 0+1;
  3. sum = 1+2;
  4. sum = 3+3;
  5. sum = 6+4;
  6. sum = 10+5;
  7. sum = 15+6;
  8. sum = 21+7;
  9. sum = 28+8;
  10. sum = 36+9;
  11. sum = 45+10;
  12.  
Look into your for loop
Expand|Select|Wrap|Line Numbers
  1. for(i=1; i <= 10; sum += i++)
  2.  
I think it should not be
Expand|Select|Wrap|Line Numbers
  1. sum+=i++
  2.  
it should be
Expand|Select|Wrap|Line Numbers
  1. for(i=1;i<=10;i++)
  2.  
Regards
Dheeraj Joshi
Apr 16 '10 #2
lyle777
8 New Member
thanks alot man
Apr 16 '10 #3
donbock
2,426 Recognized Expert Top Contributor
The semicolon only looks like it is at the end of the for statement. Actually it is the body of the for statement -- a body that accomplishes nothing. I disagree with saying the loop "has no body"; I think it is more accurate to say that the loop has a null or empty body. (You might prefer not to further overload the word null.) The following code snippets are all equivalent:
Expand|Select|Wrap|Line Numbers
  1. for(i=1; i <= 10; sum += i++);
  2.  
  3. for(i=1; i <= 10; sum += i++)
  4.    ;
  5.  
  6. for(i=1; i <= 10; i++)
  7.     sum += i;
By the way, there ought to be an initialization statement prior to the loop that sets sum to zero. You could embed that initialization statement in the for instruction. These snippets are equivalent:
Expand|Select|Wrap|Line Numbers
  1. sum = 0;
  2. for(i=1; i <= 10; sum += i++);
  3.  
  4. for(sum=0,i=1; i <= 10; sum += i++);
Apr 16 '10 #4
jkmyoung
2,057 Recognized Expert Top Contributor
At dheerajjoshim.
The loop IS correct, if you read the definition of what the program is supposed to do.

I find calling the loop empty deceiving, as these are equivalent:
Expand|Select|Wrap|Line Numbers
  1. for(i=1; i <= 10; sum += i++) ;
  2. OR
  3. i=1;
  4. for( ; i <= 10; ){
  5.   sum+= i;
  6.   i++; 
  7. }
  8.  
In c code the loop 'looks' empty.
--
In compiled machine code they would look the same. The loop is most definitely not empty, in strict loop sense. As a side note, for loops can be converted to while loops like so, where a, and c are statements, and b is a comparison.
Expand|Select|Wrap|Line Numbers
  1. for(a;b;c){
  2.   //loop contents
  3. }
  4. becomes
  5. a;
  6. while(b){
  7.   //loop contents
  8.   c;
  9. }
Apr 16 '10 #5
donbock
2,426 Recognized Expert Top Contributor
I didn't say the loop was empty. I said the body of the loop was empty; where body has a strict syntactical meaning.

Compare line 1 and lines 6-7 of the first code block in my earlier reply. These two snippets have equivalent semantics [meaning] but different syntax [expression].
Apr 16 '10 #6
Banfa
9,065 Recognized Expert Moderator Expert
where a, and c are statements, and b is a comparison.
Expand|Select|Wrap|Line Numbers
  1. for(a;b;c){
  2.   //loop contents
  3. }
  4. }
Strictly a, b and c are all expressions. An expression is any statement that returns a value. B does not have to be a comparison for instance

Expand|Select|Wrap|Line Numbers
  1. int i, sum=0;
  2. for(i=11; i -= 1; sum += i) 
  3.     ;
  4. cout << sum << endl;
  5.  
Not that I would recommend that syntax
Apr 16 '10 #7
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Yes my bad. Loop is correct.
But it is always prefered to write your code clearly.

Rather than adding
Expand|Select|Wrap|Line Numbers
  1. sum+=i++
  2.  
in for loop.
Add
Expand|Select|Wrap|Line Numbers
  1. sum = sum +i
  2.  
This increases program readability. It really helps the guy who is maintaining the program.

Regards
Dheeraj Joshi
Apr 17 '10 #8
donbock
2,426 Recognized Expert Top Contributor
Which is better:
Expand|Select|Wrap|Line Numbers
  1. sum += i;
  2. or
  3. sum = sum + i;
Reasonable people can disagree. Personally, I prefer the += form for the following reasons:
  1. I immediately know the variable on the left side of the equals sign is being incremented. There is no need to compare the variable names on each side of the equals.
  2. Typing in the variable name twice introduces the small risk there will be a typo in one of them. Suppose both sum and som exist in my program, is the following a typo or is it what I really want?
Expand|Select|Wrap|Line Numbers
  1. sum = som + i;
Which is better:
Expand|Select|Wrap|Line Numbers
  1. sum += i++;
  2. or
  3. sum += i;
  4. i++;
Using pre- or post-increment in a larger expression is certainly tricky. However, you need to be able to accurately read this sort of code if you are going to use C professionally -- even if you don't intend to use it yourself.

In questions of style, I contend it is much more important to be consistent with norms of your coworkers than it is to eke out a marginal [and debateable] improvement through an innovative style.
Apr 18 '10 #9
jkmyoung
2,057 Recognized Expert Top Contributor
Looking back at my previous post, I was concerned about a problem that did not exist, and thus sidetracked the thread a little.

sum+= i;
vs
sum = sum + i; ?

sum+= i; can be optimized by the compiler better. I almost always go with this option.
Apr 19 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Cindy Mueller | last post by:
--F0.5BD08B6_ABBB95C Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%">...
0
by: Maurice Mclain | last post by:
--7FC.__338__A.F0643A1 Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1"...
0
by: Lola Ervin | last post by:
--7F90A0.__A71 Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%"> <tr>
2
by: Dennis M. Marks | last post by:
I am never sure of when a semi-colon is required in javascript. Is there a definite rule? -- Dennis M. Marks -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----...
1
by: jw56578 | last post by:
I've seen some system sp's that have a semi colon and number after the the name such as create procedure sp_procedure_params_rowset;2 what does this do?
27
by: StevePBurgess | last post by:
With a string of authors such as: Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen, Shawyer, Andrea I would like to write a function to change every other comma into a semi...
161
by: Dan Lenski | last post by:
Hi all, I'm a recent, belated convert from Perl. I work in a physics lab and have been using Python to automate a lot of measurement equipment lately. It works fabulously for this purpose. ...
3
by: nigel | last post by:
Hi, I'm using VBA to export data from a table direct to a CSV file DoCmd.TransferText acExportDelim, , "ExportTable", filePath this produced a file with COMMA separated values,...
3
by: Sylvain | last post by:
Hi, I'm playing with Google App Engine and during my tests it seems that there is a bug in cgi. parse_header function. If we upload a file with a semi-colon (i.e : "C:/my;file.jpg") :...
26
by: machineghost | last post by:
First off, let me just say that as someone with no DBA training whatsoever, any help I can get with this issue will be very, very much appreciated. My company recently migrated our database from...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.