![]() |
|
#1
|
|||
|
|||
so i got this booki got this book on C programming, the whole teach yourself thing, i've been doing fine all the way up till the for loop concept, i thought i understood it, until i had to create a program involving the for loop. i'm looking for a brief but oxymoronically well explained. the program code that i can't quite grasp goes like this.
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
|
Quote:
yes the loop would run from when x=1 to when x became 101, each time incrementing x by 1, but it would not run the 101st time. this same thing could be expressed as this: for(int x=1;x<=100;x++) means x starts with value 1, and then increments with each run till it becomes 100. The middle expression means that while x<100 or x=100 keep doing this loop. so once x becomes 100 and passes thru and then is incremented to 101, the loop will cease running. x<101 or x<=100 is essentially the same in this case since x is an integer and is being incremented by 1. in the above posted code, i starts with the value of 2, and runs till i reaches num/2 and increments each time. i = i+1 is same as i++. since num is an integer num/2 will always yield an integer result. for example if num was 5, then 5/2 would yield 2, instead of 2.5 because when you perform division on two integer operands, the resultant outcome is an integer since the system takes off any decimal points after the first digit. to yield a decimal result the resultant variable needs to be stored in a floating point number space, e.g. float or double and also atleast one of the two operands need to be floating point e.g. 5/2.0 or 5.0/2 or 5.0/2.0 basically the for loop works this way: for(variable=value;while loop exit condition not met;perform operation on variable) in the above case you've seen the variable i being incremented by 1. However, it's not necessary to always increment it by 1. you can say i=i+2 or i=i+3, this way it would add 2 or 3 each run to i. you can also say i-- or i=i-2 or i =i-3. or i = i/3 or i=i*2. just as long as you know that at some point the loop exit condition will be met, you'll be fine. also, i should point out shorthand notation, since it's quicker. below are short hand notations for a few kinds of operations: i = i+1 : i++ i = i+4 : i+=4 i = i-1 :i-- i = i-4 :i-=4 i = i*(-1) :i*=(-1) i = i*4 :i*=4 i = i/4 :i/=4 and so on you could also run the loop with real numbers: for(double i=0;i<1;i+=0.01) //runs the loop a hundred times {} cout<<i; //i = 1 p.s. someone plz let me know if im absolutely right about this, the for loop still messes me up sometimes as soon as it comes to counting as to how many times it executed Last edited by machinated : 02-May-2004 at 21:29.
|
|
#3
|
|||
|
|||
|
something peculiar happened while i was testing the above posted for loop repeatedly:
for(double i=0;i<1;i+=0.01) //runs the loop a hundred times {} cout<<i; //i = 1 below, the 2 loops produce similar results: CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
|
well my problem now is understanding what happens after the loop goes true or false, if it's true according to the book it repeats, and if so what for, then when it's false program execution picks up with the next line of code... the next line of code being what? the if statement? and after all is said and done i still don't understand how the code determines prime numbers...
|
|
#5
|
||||
|
||||
|
Then let's clean up the code to make it less ambiguous:
CPP / C++ / C Code:
__________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#6
|
|||
|
|||
|
Quote:
i've been starring at this for over a day, and i just figured it out right before i read this post, but what if the value is prime, i'm still a little blurry and fuzzy on that. why is is_prime = 1, declared at the top of everything, that's the last thing i can't figure out, and i know about i++ i'm just doing things straight from the book, so i didn't bother to change the code..thanks though i'll change it in the future. also when i divide by i say my value for num is 8 am i dividing by 2,3,and 4? |
|
#7
|
|||
|
|||
|
Quote:
even though is_prime is of type int, it is actually being used as a boolean value(true or false, 1 or 0). I think what you have there is a pure c code, which is fine. In c++ there is a boolean type bool which only allows variable of its type to be true or false or 0 or 1. so is_prime is declared true before the loops starts. Sometimes when you declare boolean variables you have to declare them based on what situation demands. Anyway after you enter the loop, yes num is being modularly divided by 2,3,4 and so on each time, and if the remainder is 0,meaning that number goes evenly into num, then is_prime is changed to false. But if no value of i goes into num till i reaches num/2 according to the loop, then is_prime remains 1 as it was declared before the loop. |
|
#8
|
|||
|
|||
|
Quote:
so what about the number 8 when 3 goes into it twice with the remainder of 2, what happens to the code? |
|
#9
|
||||
|
||||
|
Quote:
Like Machinated said, is_prime is an integer that is being used as a BOOLEAN variable. In this case, the program is assuming that the number is prime before it enters the for loop by setting the value to 1 (true). Then if the number is evenly divisable by any of the test numbers, the boolean is set to 0 (false) and the number is not a prime. |
|
#10
|
|||
|
|||
|
Quote:
it doesnt stop after 3 goes into 8, it will actually stop after 4 goes into 8 (since 8/2 = 4 which is stop conditionf or loop) twice with remainder being 0, and at that point, is_prime will evaluate to false or 0 i know its very confusing and a lot to digest, but keep curious and ask any questions you might have, and maintain an interest and don't give up, pretty soon after a few months, it'll all clear up and you'll be very good at it |
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| The Traps of Linux...&open source software | michael | Open Discussion Forum | 6 | 22-May-2004 02:05 |
| need help with passing 3 arrays into a function | tommy69 | C Programming Language | 14 | 07-Apr-2004 00:22 |
| C++ books for newbie | mak90thug | C++ Forum | 4 | 04-Feb-2004 14:58 |
| Where do I go now? - Questions about PHP, and etc. | PerfectMercy | MySQL / PHP Forum | 7 | 11-Sep-2003 05:03 |
| Mfc | gmn | C++ Forum | 4 | 03-Sep-2003 09:47 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The