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;
}
9 9566
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 -
for(i=0;i<10;i++)
-
{
-
//Your code
-
}
-
sum = 55 is a correct answer
Traverse your loop. Loop is calculating the value correctly. -
sum = sum + i
-
sum = 0+1;
-
sum = 1+2;
-
sum = 3+3;
-
sum = 6+4;
-
sum = 10+5;
-
sum = 15+6;
-
sum = 21+7;
-
sum = 28+8;
-
sum = 36+9;
-
sum = 45+10;
-
Look into your for loop -
for(i=1; i <= 10; sum += i++)
-
I think it should not be
it should be
Regards
Dheeraj Joshi
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: - for(i=1; i <= 10; sum += i++);
-
-
for(i=1; i <= 10; sum += i++)
-
;
-
-
for(i=1; i <= 10; i++)
-
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: - sum = 0;
-
for(i=1; i <= 10; sum += i++);
-
-
for(sum=0,i=1; i <= 10; sum += i++);
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: -
for(i=1; i <= 10; sum += i++) ;
-
OR
-
i=1;
-
for( ; i <= 10; ){
-
sum+= i;
-
i++;
-
}
-
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. -
for(a;b;c){
-
//loop contents
-
}
-
becomes
-
a;
-
while(b){
-
//loop contents
-
c;
-
}
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].
Banfa 9,065
Recognized Expert Moderator Expert
where a, and c are statements, and b is a comparison. -
for(a;b;c){
-
//loop contents
-
}
-
}
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 -
int i, sum=0;
-
for(i=11; i -= 1; sum += i)
-
;
-
cout << sum << endl;
-
Not that I would recommend that syntax
Yes my bad. Loop is correct.
But it is always prefered to write your code clearly.
Rather than adding
in for loop.
Add
This increases program readability. It really helps the guy who is maintaining the program.
Regards
Dheeraj Joshi
donbock 2,426
Recognized Expert Top Contributor
Which is better: - sum += i;
-
or
-
sum = sum + i;
Reasonable people can disagree. Personally, I prefer the += form for the following reasons: - 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.
- 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?
Which is better: - sum += i++;
-
or
-
sum += i;
-
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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%">...
|
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"...
|
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>
|
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 =-----...
|
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?
|
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...
|
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. ...
|
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,...
|
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") :...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| | |