Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,367 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,293 people online right now. Registration is fast and FREE... Join Now!




Combining while and for

 
Reply to this topicStart new topic

Combining while and for, Can not understand a detail in this

Zeddicus
post 21 Aug, 2008 - 07:03 AM
Post #1


New D.I.C Head

*
Joined: 14 Jul, 2008
Posts: 41


My Contributions


I guess it is kind of hard for me to explain my question in clear way but I will give it a try.

How can the primecand variable in the program below be incremented to the prime 3? Isn't the primecand variable set to 0 everytime the is_prime function returns a value back to it which in turn would mean that the ++ operation is always performed on 0 and thus always resulting in the value 1? Or does the while loop somehow "remember" the previous increments so that it knows where to begin again, regardless of the current value of the variable it operates on?

Please excuse if my description is confusing, it is probably because I am confused too. smile.gif

c
int main(int argc, char **argv) {
int primecand;
while (is_prime(primecand) == FALSE) {
++primecand;
}
printf("%d är ett primtal.\n", primecand);
}

int is_prime(int n) {
int m, retval;
for (m=2;m<n;m++) {
if (n % m == 0)
return FALSE;
else
retval = TRUE;
}
return retval;
}


User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 21 Aug, 2008 - 07:07 AM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Look inside the while loop.

++primecand;
This means "add 1 to primecand." Therefore, we are actually passing the next number in the sequence. If 0 was passed, 1 will be passed next, then 2, then 3, etc...

Also, it's a good habit to initialise a variable before using it. Otherwise, it could be given some ridiculous value at compile-time and the program won't have the expected output. So, int primecand = 0; This way, it should exit the loop when it reaches 2.

Hope this helps smile.gif

One thing to note is that retval is not initialised, and since primecand is being passed as 0, the loop is not entered.
User is offlineProfile CardPM

Go to the top of the page

Zeddicus
post 21 Aug, 2008 - 11:36 AM
Post #3


New D.I.C Head

*
Joined: 14 Jul, 2008
Posts: 41


My Contributions


I don't understand your explanation fully. Isn't the while loop entered on the specific condition that primecand = FALSE, or in other words, primecand = 0?
And how can it be incremented to 3 when entering as 0 (over and over again)?

Sorry for not understanding... I read your post several times though, so I really tried.

This post has been edited by Zeddicus: 21 Aug, 2008 - 11:38 AM
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 21 Aug, 2008 - 11:57 AM
Post #4


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


OK, a while loop will be re-entered at the end, if the condition is still true. By passing a function to it, it will pass it again.

Maybe this will make more sense:
cpp
bool sentinel = false; // the value which we search for
while (!sentinel)
if (is_prime (++primecand))
sentinel = true; // now it will exit
It does the same thing.

Basically, in your example, it will try the function again before deciding whether the loop should be executed again.

Get it?
User is offlineProfile CardPM

Go to the top of the page

Zeddicus
post 21 Aug, 2008 - 10:38 PM
Post #5


New D.I.C Head

*
Joined: 14 Jul, 2008
Posts: 41


My Contributions


You know, all this confusion was based on a misconception from my side.

I thought that the value returned from the is_prime function was copied into (primecand) where the call was made. But this is not the case obviously. Rather it is the value itself in retval that is returned and compared with ==FALSE. Primecand value is only modified by ++-operator and not by the returning function. It was vital for me to learn this. I understand the program completely now. Thanks for your help.

Just for curiousity: the program was taken from Swedish version of Wikipedia as an example of iteration, do you think I should initialise the above mentioned variables in Wikipedia? smile.gif

This post has been edited by Zeddicus: 21 Aug, 2008 - 10:45 PM
User is offlineProfile CardPM

Go to the top of the page

9 one one
post 22 Aug, 2008 - 01:33 AM
Post #6


New D.I.C Head

*
Joined: 21 Aug, 2008
Posts: 2


My Contributions


QUOTE(Zeddicus @ 21 Aug, 2008 - 11:38 PM) *

Just for curiousity: the program was taken from Swedish version of Wikipedia as an example of iteration, do you think I should initialise the above mentioned variables in Wikipedia? smile.gif

Hi Zeddicus

There are some basic issues with the code. Although the code essentially do the right thing, and is alright if you really want to learn about iterations, but the basic problem here is that whenever you will run the code, the code will return back some unknown value in "primecand". The variables, if not initialized, will contain what we call Garbage value and hence the results would be unpredictable. So we ought to initialize all the variables we need in the code in the beginning itself. It is a good programming practice. Just have a look at the code.

CODE


int main(int argc, char **argv) {  
    int primecand;
    primecand = 0; <--- HERE
    while (is_prime(primecand) == FALSE) {  
        ++primecand;  
    }  
    printf("%d är ett primtal.\n", primecand);  
}  



Now with this code your premise of primecand having the value of 0 (atleast for the first time) will be correct.

This is just for your information. Hope it helps.

Regards
9 one one
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 22 Aug, 2008 - 04:00 AM
Post #7


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


You know, you could just use int primecand = 0; on one line. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Zeddicus
post 22 Aug, 2008 - 04:54 AM
Post #8


New D.I.C Head

*
Joined: 14 Jul, 2008
Posts: 41


My Contributions


Hi 9 one one!

Thanks for your recommendations. I will take them and gabehabes viewpoints into consideration when I modify the code on Wikipedia to ensure high reliability.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 05:10AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month