This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
notes:cprog:spring2024:projects:cnv0 [2024/02/26 13:21] – created - external edit 127.0.0.1 | notes:cprog:spring2024:projects:cnv0 [2024/03/06 20:54] (current) – [do-while loops] amelvil2 | ||
---|---|---|---|
Line 4: | Line 4: | ||
====Determining factor pairs==== | ====Determining factor pairs==== | ||
+ | To determine factor pairs you can make an iterative loop, and check for how many times your input value is evenly divided by a number, ie. for 6 you would check: | ||
+ | 6 % 1 = 0, 6 % 2 = 0, 6 % 3 = 0, 6 % 4 = 2, 6 % 5 = 1, 6 % 6 = 0 | ||
+ | |||
+ | There are 4 zeros, so 6 has 4 factors. | ||
+ | |||
+ | Determining factor pairs takes a little bit more work than you would imagine, as you cannot simply divide by 2, because of square numbers, who have an odd number of factors. | ||
+ | |||
+ | Instead, you can ignore 1 as a factor, divide the amount of other factors by 2, then add 1 to your amount of factor pairs. | ||
+ | |||
+ | This process may look like: | ||
+ | < | ||
+ | for (i=2; i< | ||
+ | if ((input % i) == 0) { | ||
+ | factorpairs++; | ||
+ | } | ||
+ | } | ||
+ | factorpairs = (factorpairs / 2) + 1</ | ||
=====Compiling===== | =====Compiling===== | ||
Line 10: | Line 27: | ||
====for() loops==== | ====for() loops==== | ||
+ | A for loop is a loop that iterates based on a set of given parameters | ||
+ | for example a for loop that starts at 0 and iterates 1000 times would look like: | ||
+ | < | ||
+ | for (i=0; i<=1000; i++) { | ||
+ | whatever you want to happen; | ||
+ | } </ | ||
+ | An example of a for loop that shows what iteration the loop is on, i.e Iteration: 0, Iteration: 1, etc. until it reaches Iteration: 9 and ends the loop: | ||
+ | < | ||
+ | for (i = 0; i < 10; i++) { | ||
+ | printf(" | ||
+ | }</ | ||
====while() loops==== | ====while() loops==== | ||
+ | A while loop is a loop that continues to iterate as long as the input evaluates to true. | ||
+ | a while loop that iterates until an input variable reaches some designated value may look like: | ||
+ | < | ||
+ | whatever you want it to do; | ||
+ | }</ | ||
+ | If you arent careful with your condition a while loop will iterate endlessly, preventing your code from moving past the loop | ||
+ | < | ||
+ | var = 20; | ||
+ | while (var == 20){ | ||
+ | do something that doesnt effect var; | ||
+ | } | ||
+ | the rest of your code;</ | ||
+ | In this case, because var never changes from 20, the while loop will never end | ||
+ | You can also achieve this endless iteration by typing | ||
+ | < | ||
+ | some code; | ||
+ | }</ | ||
+ | Because the condition is always true it will never escape the loop | ||
====do-while loops==== | ====do-while loops==== | ||
+ | A do-while loop issues commands then checks the condition rather than checking the condition and then issuing the commands. | ||
+ | |||
+ | < | ||
+ | int i = 5; | ||
+ | do | ||
+ | { | ||
+ | i--; | ||
+ | } while ( i > 0 ) | ||
+ | </ | ||
+ | |||
+ | An example of a do-while loop being used. This loop will check if the user has inputted the correct password. The loop will continue until number equals 1234: | ||
+ | |||
+ | < | ||
+ | int number; | ||
+ | do { | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | } while (number != 1234); | ||
+ | </ |