Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

If Statement In A Loop

loop

  • Please log in to reply
7 replies to this topic

#1 meridius10

meridius10

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 60 posts

Posted 04 May 2012 - 08:35 AM

These two examples definitely aren't right but how would I write out this code so the loop stops at 6 and 2 (for the second example) but still continues as a loop?:

#include <iostream>
using namespace std;

int main()
{

int i ;

   for ( i = 1 ; i < 10; i++ )
   {
   if  ( i = 6 )
   {
   cout << "Error.Exclude Number 6" << endl ;
   }
   cout << "Loop iteration: " << i << endl ;
   system("PAUSE");
  }

return 0;

}


#include <iostream>
using namespace std;

int main()
{

int i ;

  for ( i = 1 ; i < 4; i++ )
  {
  cout << "Loop iteration: " << i << endl ;
  if  ( i = 2 )
  cout << "Error.Exclude Number 2" << endl ;
  system("PAUSE");	
  }

return 0;

}

While I'm here I might as well ask a few more questions!...

- What are the rules for the { } symbols in C++ in terms of spacing and leaving blank lines between the next { or }? Also do I need to have a gap before a semi-colon placed at the end of a line?

- I am still trying to fully understand pointers and addresses. I was told that if I use these on my pc it will change things on my physical memory and HDD. Is this true?

- Is anyone using any non Microsoft C++ compliers in Windows 7 64-bit? I tried CodeBlocks but it didn't work. I have figured something out in Linux but Windows is the issue for now.

Thanks.
  • 0

#2 sepp2k

sepp2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 183 posts
  • Programming Language:C, Java, C++, Python, Ruby, Scheme, Bash, Haskell, Others
  • Learning:Others

Posted 04 May 2012 - 09:03 AM

In C++ = is the assignment operator and == is the equality operator. So if you do if(i=6) that actually sets i to 6. If you want to check whether i is 6, use ==, not =.

- What are the rules for the { } symbols in C++ in terms of spacing and leaving blank lines between the next { or }? Also do I need to have a gap before a semi-colon placed at the end of a line?


There are no official (or quasi-official) style conventions for C++. Each project or workplace seems to have their own coding conventions. So pick one you like and when you collaborate with others, adhere to the style guide of the project.

Gaps before semicolons are pretty uncommon though (in my experience), so I wouldn't do that.

- I am still trying to fully understand pointers and addresses. I was told that if I use these on my pc it will change things on my physical memory and HDD. Is this true?


No, writing to pointers will not change anything on your hard drive (unless you use mmap and co., but you're not, so ignore that). It will change the contents of your memory though, but only in the way that writing to a variable (almost) always does.
  • 0

#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 04 May 2012 - 09:31 AM

Yes, in C/C++, = is an assignment operator -- assigning a value (from an expression, literal or variable) to a variable. As example, "int a = 10;", this will assign 10 to a. But you write the assigning process in an expression, it will always evaluate to true. That is, the assigning value to a type-safe (if not type-safe, you will get compile time error) variable is always true.
int a;
if (a = 10) { // a = 10 is always true.
// All the statements inside this if clause will always get executed.
}

In your both code-segment, those two loops will run forever. In case of first code-segment the execution as follows...
  • first i set to 1 and as 1 is less than 10, it will execute the for-clause code-segment
  • Then, for "if (i=6)", i will assigned new value 6, all the message will be printed on stdout and will wait for a keypress.
  • After kepress, it again check the 'for''s condition express.
  • Now it will move to step 2 again.
And this will run for ever. Same for second code-segment's loop. So yes, if you want to do comparing i with 2 or 6, you need to use as "2 == i" or "6 == i".

You can keep as many gaps before or after '{' or '}'. Yes, in case of putting ';' at the end of statement, you can keep from zero to as many space you want.


-No, pointer will not change anything to your HDD.

-I do use MinGW on Window7 64 bits without problem (but I guess the programs are 32 bits).
  • 0

#4 sepp2k

sepp2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 183 posts
  • Programming Language:C, Java, C++, Python, Ruby, Scheme, Bash, Haskell, Others
  • Learning:Others

Posted 04 May 2012 - 09:47 AM

But you write the assigning process in an expression, it will always evaluate to true. That is, the assigning value to a type-safe (if not type-safe, you will get compile time error) variable is always true.


No, it's not. It will evaluate to the assigned value. So if you write "if( x = 0 )", you'll get false because "x = 0" evaluates to 0, which is false.
  • 0

#5 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 04 May 2012 - 09:50 AM

No, it's not. It will evaluate to the assigned value. So if you write "if( x = 0 )", you'll get false because "x = 0" evaluates to 0, which is false.


Yeah, that's right. Thanks for the correction.
  • 0

#6 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 04 May 2012 - 01:56 PM

- Is anyone using any non Microsoft C++ compliers in Windows 7 64-bit? I tried CodeBlocks but it didn't work. I have figured something out in Linux but Windows is the issue for now.


it seems that all of your questions have been answered correctly by experts :)
I would just like to introduce you into a compiler that I've been using for years now.. Please Try Bloodshed DevCPP
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#7 meridius10

meridius10

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 60 posts

Posted 04 May 2012 - 04:07 PM

Thanks for all the feedback.

It's as simple as replacing = with == and the problem is resolved and there is no endless loop.

I have just installed Bloodshed Dev C++ and it works on Windows 7 64bit!
  • 0

#8 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 04 May 2012 - 04:46 PM

Thanks for all the feedback.

It's as simple as replacing = with == and the problem is resolved and there is no endless loop.

I have just installed Bloodshed Dev C++ and it works on Windows 7 64bit!


Good.. It was the best IDE that i've been using for years and its free :) have fun
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped





Also tagged with one or more of these keywords: loop

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