About Me

Ontario, Canada
In regards to this blog, I am simply trying to learn to program in my free hours after work in order to make an iPhone game.
Showing posts with label loops. Show all posts
Showing posts with label loops. Show all posts

Friday, May 15, 2009

While we wait

A quick update.

I have started on chapter 17 of C for Dummies. It discusses 'while'
loops and their pros and cons versus using 'for' loops.

My understanding so far is that while using 'while' loops may be
easier in the short term, using 'for' loops provide you with more
flexibility in what you can accomplish.

I will update again once I have finished the chapter.

Cheers!

Saturday, May 9, 2009

Repeat After Me

Ok, so since I have not been reading much lately due to being sick I figure I can post a short piece of code that I had previously written to show you what kind of level I am at.

So, first, the code (it is in an image format since when I use the 'code' function in here the formatting goes to shit).



This is simply one piece of a 5 part code, though this piece represents, almost exactly, what happens later on.

Basically this program tells the user to repeat after it using the correct keys.

It tells the user to type every number it says in numerals. ie. ONE = 1

(the program goes up to the number 5, since the code is redundant, there was no need to keep going as the ideas behind it are easily learned from the first few instances)

The fun of writing this program though was figuring out how to take the users WRONG answer and allow them to try again, and again, and again, and then continuing on with the program when they finally get it right.

Obviously it is very hard to screw up 1, 2, 3, 4, 5 but the ideas behind it hold true. Being able to take a wrong answer and allowing the user to try once more. ie. error checking, so to speak.

Another part of the fun of writing this program was the fact that I was still very green on how do/while loops worked so it was very pleasing when it finally worked just right. I had some looping issues with certain areas not being displayed properly, or areas being printed twice in a row. The fix for that was making sure I was using the 'fpurge' function after each 'getchar' function.

Alright, so that is that.

Cheers! :D

Thursday, April 30, 2009

C for Dummies: Chapter 16

Alright, for my first official post (apart from my droll mission statement) I will simply tell you about my current experience and where I am at in my studies.

To learn the C language I am using the C for Dummies book which you can probably find at your local book store or buy online straight from the Dummies Store.

Currently I have just finished chapter 16 where the book discusses the for() loops in a bit more detail, and explained some mathematical shortcuts that programmers are able to take.

For instance, 5x5x5x5x5. . .etc is equivalent to z*=5 (where 'z' is the variable and * is the multiply symbol).

Example:


for(z=5 ; z>=500 ; z*=5)
{
statement;
}


That simply says to start the 'z' variable at 5 [ z=5; ], keep running the loop until the 'z' variable is greater than, or equal to 500 [ z>=500; ], achieve this by continuously multiplying the 'z' variable by 5 [ z*=5 ].

Right now I am unsure of where I would want to use this type of loop, but I am sure as I start to learn more the answers will become clear to me :)

Cheers!