Showing posts with label beginning c. Show all posts
Showing posts with label beginning c. 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.


Thursday, May 14, 2009

Hello World

Okay, so I think I've downloaded everything properly, but there is only one way to find out.
We created a program called hello world. 
(I called mine hello worlda, since this is not my first hello world).



You should see on the top bar, a "run" tab, if you click on it you will get a list of functions. Open the console function.  The console is where all the magic of our programming happens.  This is where you see the fruits of your labor.  

With any luck you see a screen that looks like this.
















Now to test if we had install everything properly, if you click build and go you should see on the console.  (session started)  and the magical "Hello, World!"  Yessss, we did it!!! But wait, that is actually the default program, so you'll always get "Hello, Word!".  We actually just cheated to get our first hello world.  So lets go into the code and see what is happening.  And actually make hello world from scratch. 

On the left hand side you see the project folder with hello world, you'll see the source, documentation, and products.  Well, I can tell you I know next to nothing on most of those.
Click on source and you'll see a file name main.c, that is the meat of our first program.
Below the file name, there is a window, called the editing window.  That is where the code we will write goes.  

the #include form what I gather is like a set of rules for the program you are about to make.   So the computer knows what rules it is going to follow.  I may not be saying this right, but I'll edit this in the future if I can think of a better example.

The next part is the int main.  This is what c programmers called a function.  A function is basically the task that you want the computer to do.  So therefore the int main in this case is the task of writing hello world.  

Now we have to tell the computer how to write hello world and where.  This is were another function printf comes in.  Well, I consider it a function because it is tell the computer to do something.  printf tells the computer that you want to print something on the console.
you printf from int main by brackets.  Programmers love brackets because this keeps the computer for getting confuse.  It is essential in keeping the computer focus and on track.  

To use the printf(). We have to write what to print.  We put what we want to print inside the printf brackets.  We insert what we want to say in between two quotations marks.  The computer knows that anything in quotations will be the actually saying.  

So lets delete all the things after int main, and see if we can get hello world, from scratch.
we'll put curly brackets { printf ( "hello world\n");  return 0;.  why the \n? well that tells the computer to return the cursor to the next line.  You may also wonder why did we put ; at the end of printf?  the ; is actually very important.  From what I read it is like putting a period at the end of a sentence.  If you do not have ; the computer will get confuse very easily.  

Lastly, what is this return 0? (that's return zero, not o.) That is to tell the computer everything is done with printf and return the control back to main.  It's like telling the computer, you've done the task, good job.

With any luck you won't have any nasty syntax errors.  Syntax errors is the computer telling you that your grammar is wrong.  It does not understand what you are trying to say.  Not the end of the world.  Usually I find I might have forgot a ; or a closing bracket somewhere.

YEEESSS, we did it, the console this time reads "hello world"!!.  We wrote a program from scratch and the computer understood us.  I would pat myself on the back if I could, this is actually pretty good for someone with no programming experience.  The first program that all programmers write is done.  

So recap, we learn how to create projects in c, learn that to get the computer to do something you need functions.  Functions are the task that you want to do.  We learn about the printf, the all import ; and return 0.  Not bad, that is the very very basic things in c.


Nex: Onward to variables and operators.