Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Thursday, May 21, 2009

So on to loops

Now that we have established if and else,

We can concentrate on the while, for, and do.

All of them essentially makes the program go back the the starting step, and will continue to do so until the cycle is broken.  Be careful of infinity loops, that is loops that never breaks.
That means your program will never end, always a bad thing.

The simplest is the while loop.

here is an example code, include the usually int main and all that nice stuff.


{int i;                (interger i)

i = 0;                  (i equals 0)

while (i <3,
1++};                                                                    (increase i by 1)

printf ("finished.\n");                                (print finished when out of loop)

return 0;}                          (stop program)

So there you have it the most basic of loop programming.  The "for" function is about the same as the while function, and I do not know the real basic difference between them.  I do know that for some programs "for" is better than "while" though like I said, I'm not sure why.
The "do" is the same, but the does it's execution at the end of the function instead of the beginning.

Now our program is actually starting to look like a real computer program.  It is running and we can stop it when we want.  You can even play around with the "if" and "else" to control the flow while in the loop.

Next time, we'll try to make a more complicated program.

Sunday, May 17, 2009

Loopy for loops.

Well, so far we have created programs that basically repeat what we say, "hello world."
or a program that can add 3 to 3, together.  Not bad, but these programs are a bit simplistic.
Like all programs the execute their functions the close.  However to make things more interesting, we need to make a program to will continue to run until we tell it to stop.

How do we do that? We do that with loops.
Loops is when the program restarts after completing its' primary function.  It would go back to designated points in the program, like the beginning of the program or function.  

We will investigate a few of these loop functions. 
The ones we'll look at include the "if", "while" and "for" loops.

The "if" function is not really a loop, but I put it in this category since it does direct the flow of the program.  Essentially you are using "if" to state that if something happens the program will do something else.  Like if the football game is on, then I'll watch.  Of course you can make it a bit more complicated.  If the football game is on, then I'll watch, but if it is not then I'll watch something else.   

Let's look at some code to see this in action.

#include stdio.h                         (our rules or parameters)
int main                                      (beginning of our program)

{int myNumber;                        (declaring my variable)
my Number = 1                       (establishing myNumber equals to 1)

if (myNumber == 2)                       (saying if myNumber is equal to 2)
printf ("myNumber is equal to 2");       (then print "myNumber is equal to 2")
else 
printf ("myNumber is not equal to 2");   (or else print "myNumber is not equal to 2")

return 0;}                     (end program)

"if" and "else" introduces the concept of true and false.  The computer will see either the state is true or the statement is false.  By doing this we can direct the program to do what we want by manipulating what happens when the computer encounters the true/false.



Not bad.  Now we can more deeply into loops, if you do not understand it, re do this part again.  It's important.  


I think I'll split this topic into two post, since
1) It is important
2) It's a big subject.


Saturday, May 16, 2009

Hello Operator? Are you there?

What are operators and variables?

Well, after rereading variables and operators, this is how I interpret it.  Variables is the data or the thing that you want to do something with and operators is function that is used.  For example, 1 + 1 = 2,  this is a complete function, 1 is the variable and the + is the operator.  

In C, you are essentially working with variables and how you work with them is the operator.  
Once you learn the terminology the more easier it is to follow.  

There are numerous of operators.  When working with math, the typical operators are all available.  There is +, -, / (divide), * (multiply).   If you see ++, that means plus 1, -- means minus one.  If you see myInt++, that means myInt plus one.  Example myInt = 3, therefore myInt++ = 4.  

Now to get variables to work you must first declare it.  Why do you need to declare it? Well, it is essential to tell the computer here is my data, and I will be working with this data.
For example, int myInt, that tells the computer you will be working with an interger named myInt.  You do this at the beginning of your function.  You can declare multiple variables at once, you just need to separate them by a comma.  For example: int myInt, myNumber.

For example:

#include stido.h  (the rules or sets of premade functions)

int main (always the beginning of our program)

{int myInt; ( declaring the data, myInt will be an integer)

myInt = 3; (stating that myInt equals number 3)

return 0;}  (telling the computer the program is finished)

Not every exciting example, but it shows that by telling the computer that I will be working with  myInt, it will know what to expect in the following code.  Sometimes the computer will even tell you that you have declared something but didn't use it.  That can point to an error where you mistyped a name of your variable or capitalized the wrong letter.

Simple isn't it.  Actually it took a bit for my poor brain the wrap around it.  There are lots of example in the books and the internet that I've read, and this is how I come to understand it.  You can correct me if I'm wrong or have any better examples that you come up with.

Next up:  Loops!!!!!

Wednesday, May 13, 2009

Day 1. Gathering the resources

I decided to make a blog chronicling my adventures in making an iphone app.  

First: A little background, I have no programming experience what so ever.  I did enroll in a beginning programming course in college but had to drop it due to conflicts with other classes.  I find this task quite daunting, since the programming language is as foreign to me as a language from another country.  My career is in the medical field.  I love my Iphone and love all the applications for it.  I wanted to make an app and put it in the app store, not only for the monetary gains, but mainly I wanted to expand my horizons.  I always wanted to make video games, since that is one of my main hobbies.  Learning to make an iphone app, will be a means to that end.

What I have gather so far.
I suggest those that want to dip their toes into programming, you should check out a website call Tilestack.com.  I program one stack called Mr.Sage.  That was my first programming effort of any kind.  Tilestack lets you play around in a environment that is more friendly to someone who knows nothing of programming.  It takes most of the intimidating coding out of it.  Infact if you make a stack good enough, they would even publish it in the iphone app store.  There are draw backs, since if you publish onto Tilestack there are chance people can steal your idea, as I found out.  And often times what you think is a good stack does not correspond with others, and your stack may not be publish.  But as a start, I find it a great tool to know the basic of programming.

Books:  I bought these books since I did a quick google search, and they seem to be the most recommended.

1)Learn C on the MAC by Dave Mark, publish by Apress
I find this book pretty easy to understand and it was written with people who have no experience in mind.  I have read this book through and was able to accomplish so of the simple programming.  The book becomes harder to read about half way through, but stick with it.  I had to reread it several times already.

2)Learn Objective-C on the Mac by Mark Dalrymple / Scott Knaster, publish by Apress
I only started to read this book, since it just came in the mail from amazon.
So far, some of the the things, I learned from the first book, carries over to this, but it is a bit more difficult to follow.  Probably will have to take it slow....really slow.

3)Beginning iPhone Development: Exploring the iPhone SDK by Dave Mark/ Jeff LaMarche, publish by APress.  
I have read only the first two chapters, from what I know this will probably be difficult to follow if I do not understand objective-c (which I don't yet).  This seems quite a bit different from C language, but we'll know more once I go through the book in more detail. 

WebSites:
So far the best website I found is iPhone development Central.  They have video tutorials, though I find most of them difficult to follow from a non programmer perspective.  I decided to join their forums since they appear to be a community of programmers from different level of expertise.  Some of the members are friendly and would gladly give a helping hand to newbies. 

Another great website is called Masters of the Void.  This is essentially learning C, but in a more truncated form.  If you do not want to buy the book, this would be the first step I recommend.  

Well, That's it for now, back to reading and programming.