Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

syntax of function, for loop

for loop loop syntax

  • Please log in to reply
4 replies to this topic

#1 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 03 May 2011 - 02:00 PM

Hi :)

Personally I would prefer to give function's definition before making calls to it because it provides more understanding how C++ works. As some member here told me that time-travel (moving back and forth) is not possible all the time in C++. The statements are executed in a sequence so we should also try to write the code in a sequence to make it more understandable.

for(int j=0; j<45; j++) - The for loop is executed, then it's value is incremented, then the incremented value is checked against the test expression. So, don't you think having the syntax this way, for(int j=0; j++; j<45), makes more sense?

Please help me. Thanks a lot.

// table.cpp
// demonstrates simple function
#include <iostream>
using namespace std;

void starline();                          //[B]function declaration[/B]
                                          //   (prototype)
int main()
   {
   starline();                            //call to function
   cout << "Data type   Range" << endl;
   starline();                            //call to function
   cout << "char        -128 to 127" << endl
        << "short       -32,768 to 32,767" << endl
        << "int         System dependent" << endl
        << "long        -2,147,483,648 to 2,147,483,647" << endl;
   starline();                            //call to function
   return 0;
   }
//--------------------------------------------------------------
// starline()
// [B]function definition[/B]
void starline()                           //function declarator
   {
   for([B]int j=0; j<45; j++[/B])                //function body
      cout << '*';
   cout << endl;
   }

  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 03 May 2011 - 02:55 PM

The problem with your suggestion is it would break about 30 years worth of code.

It's based more on:
set value of variable, check if loop should be run, increment variable at end of loop.

Additionally, the statements can be MUCH more complicated than a simple init;test;increment.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 03 May 2011 - 03:20 PM

The statements are executed in a sequence so we should also try to write the code in a sequence to make it more understandable.

If you mean that statements should be shaped in such a way as to produce a natural reading when you're reading a certain segment of code, than I agree. For example, if you meant that a loop should be shaped like
loop from x to y:
    // Perform code here
this is generally a good way to write, and programming languages should support it.

If, however, you meant that as a software developer you should start writing your program from where the programs execution begins and work your way to the end, I very much disagree. Writing your code should be done in logical segments, usually classes or modules, that have according unit tests for each one of these segments. Generally speaking you want to start with the more complicated parts of your algorithm unless you have to deliver something quickly (for milestones or what have you), then you should get some of the particularly showy and "progress-y" looking parts first, then your complex algorithms, then the meat in the middle.

Also, all of the Data Type values are system dependent, or nearly all of them. Off the top of my head, I cannot think of a single one of them that is guaranteed by the ANSI standard to be any precise number of bytes, only an "At least" value. For your computer, you can simply use the #defines in the limits.h header.

Now the loop suggestion. If you were programming in Hypothetica, you most certainly could shape your for loops (assuming Hypothetica was an Imperative Language) with the incrementor prior to the test condition, but realistically most every programmer is used to the C method of doing things, so this would represent a stumbling block to new users of Hypothetica. You could write a preprocessor that does what you're trying to do (actually pretty trivially, too), but then all your code would be non-standard, and posting that code on boards like this one would only result in C/C++ users telling you the loop was done wrong. :P
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 04 May 2011 - 04:39 AM

Like ZekeDragon said, primitive/basic data type ranges are system dependent, you can compute their ranged with sizeof operator, which returns number of data type in Bytes.
std::cout << (-1 << (sizeof(T) * 7)) << ", " << (1 << (sizeof(T) * 7)) - 1 << std::endl;
This might look scary, but it's really simple. Let's say T is char. Then this would compute ranged for signed char like so:
  • sizeof() returns 1 (Byte)
  • we multiply it this by 7 (1 Byte = 8 bits, but 1 bit is taken for signed/unsigned representation)
  • 1 << 7 is a left shift and it's equivalent to 1 * 2^7
  • for the upper bound (right side) we must subtract 1 because 0 is positive number

for loop can be translated to while loop easly
int j = 0;
while (j < 45) {
    //...
    j++;
}
As you can see, evaluation comes before increment and if you'd set j to 45, then the loop wouldn't execute because 45 < 45 is false. If you want to learn a language that reads better than C++ I suggest you pick up Python. There may be others but I'm not familiar with them.
  • 0

The roots of education are bitter, but the fruit is sweet.


#5 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 31 May 2011 - 08:46 PM

Hi again, :)

I think I was wrong when I said this:

for(int j=0; j<45; j++) - The for loop is executed, then it's value is incremented, then the incremented value is checked against the test expression. So, don't you think having the syntax this way, for(int j=0; j++; j<45), makes more sense?


The test expression is processed first than the increment of the loop variable. The following loop will not be executed even for once because when the test is performed at the beginning, the loop variable has already larger value than the allowed one, so it will be execute any further. Please confirm it for me. Thank you.

for (int i = 1; i < 1; i++)
     {
        //statements
     }

  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)





Also tagged with one or more of these keywords: for loop, loop, syntax

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download