Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
C for newcomers, Part 6
  • chroniccommand
    Posts: 1,389
    In the chapter I will be talking about loops.

    What is a loop?
    A loop, generally, is something that happens over and over until a certain condition is met. In C, it means just that. We would use loops to keep doing something until something happens, or we reach a certain condition. I will be talking about For and While loops in this newcomer chapter.

    The FOR loops
    The first type of loop you should know is the for loop. This loop has this type syntax:

    for(begin_statement; while_condition; do_something)
    {
    Arguments;
    }

    Let's tear this bit by bit. The for statement begins with the word for, followed by parenthesis. Inside the parentheses are 3 conditions, in the above statement. The first statement is the beginning statement. We will usually use this to set a variable that will be used in the loop(Notice how it ends in a semicolon).
    The second is while_condition. This basically means that while the condition is true, continue to the next part(Also followed by a semicolon).
    The third is what we're actually doing. If we had a variable "i", we would have the do_something condition actually do something. After this is a set of brackets, followed by code.
    Take a look at this simple code, that prints "Hello. Goodbye", 5 times.

    #include <stdio.h>

    int main()
    {
    int i;
    for(i=1; i<5; i=i+1)
    {
    printf(\"Hello. Goodbye\n\");
    }
    return(0);
    }

    Take a look at the for statement.
    The begin_statement, is setting the variable of i to 1. The second while_condition, is basically saying

    While i is less than five, do what's next in the for statement


    So now that i=1, i is less than 5. So the do_something statement is adding 1 to i
    In math, you are used to seeing something like this:
    i+1=i
    In C, it's flipped. The right side of the equals sign is calculated first. Then it "slides" to the other side of the equal sign, therefore making i = 1+1
    Now we print "Hello. Goodbye\n" five times. Save it, compile it, and run it with
    ./loop

    It should give this output:


    Hello. Goodbye
    Hello. Goodbye
    Hello. Goodbye
    Hello. Goodbye
    Hello. Goodbye


    See? Five "Hello. Goodbye's". That's the basic syntax of a for statement.

    Infinite loops!
    Beware of Infinite loops in C(Unless that's the sort of thing you're into). Infinite loops are loops that continue forever, without stopping until the user quits, or presses a break like Ctrl-C. Take a look at this code using the for loop.

    #include <stdio.h>
    int main()
    {
    int i;
    for(i=1; i=5; i=i+1)
    {
    printf(\"Oh Noes! Infinite loop!!\n\");
    }
    return(0);
    }

    Take your time to examine the for statement to find out what's wrong. If you look, we are setting i to 1, then as long as i = 5, print that statement. So basically, take a look at this in English:

    i is now equal to one. Now print the statement until i is equal to five. Great, printed 5 times and i equals 5. But oh wait, as long as i = 5 continue... looks like I need to continue with the loop


    So it loops until stopped.

    Good infinite loops
    There are good forms of infinite loops, you may want to use too. Take a word processor for example. It loops for the users input until told to stop. Take a look at this code, that is a basic "notepad"

    #include <stdio.h>
    int main()
    {
    char userinput;

    printf(\"Start your typing\n\");
    printf(\"Press ` to stop\n\");
    for(;;)
    {
    userinput = getchar();
    if(userinput == '`')
    {
    break;
    }
    }
    puts(\"Goodbye!\");
    return(0);
    }

    The only new thing in this code is the break statement.
    NOTE:for(;;), is the same as an infinite loop, because it's telling it to do nothing, meaning it loops forever

    The break statement is used to break out of a loop. The break statement can only be used inside of a loop, for it will error outside.

    Increment and Decrement
    So of course, we want to take a shortcut when coding, because it's easier on the coder. Take a look at that for statement again:

    for(i=1; i<5; i=i+1)

    i=i+1 can be shortened. Now take a look at this:

    for(i=1; i<5; i++)

    ++ is a C "statement" that means "increment by one". This is the exact same as:
    i=i+1

    Just shorter, and easier. To decrement, you guessed it, used the
    --
    So i=i-1 but using i--
    Now what if we wanted to use the ++ or -- operators but to increment or decrement by five? Take a look at this:

    i+=5

    This is the same as:

    i=i+5


    Theres the same for subtraction and all the others. Look below:

    +=
    -=
    /=
    *=

    You should be able to tell what each one does.



    While loop
    Now onto the While loop. You may find the while loop easier than the for loop. It's basically the for loop, but easier to read and formatted differently. Take a look at this code:

    #include <stdio.h>
    int main()
    {
    int i;
    i=1;
    while(i<5)
    {
    printf(\"Helllllooooooooo\n\");
    i++;
    }
    return(0);
    }

    So notice the difference in the formatting. Take a look at it like this:

    begin_statement;
    while(while_condition)
    {
    statement;
    do_something;
    }

    So it's bascially a for loop, but easier to read and easier to write and understand. So let's use the while loop to print "hello world" 10 times

    #include <stdio.h>
    int main()
    {
    int i;
    i = 1;
    while(i<10)
    {
    printf(\"Hello world!\n\");
    i++
    }
    return(0);
    }

    Save, compile and run it. You should see there is no difference other than formatting etc..
    I hope this brought some basic insight to the power of loops in C

    --Chroniccommand
  • DizzY
    Posts: 155
    I'm hoping to learn C / C++ soon. Just not sure which compiler I should use.
  • chroniccommand
    Posts: 1,389
    said:


    I'm hoping to learn C / C++ soon. Just not sure which compiler I should use.



    I'm guessing you're running Winblows -_-
    Run a Unix-type machine like GNU/Linux and use GCC to compile it.
    First, save the .c file in something like Gedit or nano
    Second, enter terminal and type
    gcc -o code code.c

    This will compile code.c to the output of code, which you can then run as
    ./code
  • DizzY
    Posts: 155
    I might run linux on virtual box. But i got school in about 6 mins

    lol talk later dude
  • chroniccommand
    Posts: 1,389
    said:


    I might run linux on virtual box. But i got school in about 6 mins

    lol talk later dude



    I would just dual-boot GNU/Linux and Winblows. Seriously, you wont learn anything from Winblows. It's security is poor, it's unstable and has many vulnerabilities, it doesn't teach you anything, and my 90 year old grandma can use Winblows. If you want to learn, you'll use GNU/Linux.
  • DizzY
    Posts: 155
    Taking control there. Ohhhk i will (:
  • said:


    said:


    I might run linux on virtual box. But i got school in about 6 mins

    lol talk later dude



    I would just dual-boot GNU/Linux and Winblows. Seriously, you wont learn anything from Winblows. It's security is poor, it's unstable and has many vulnerabilities, it doesn't teach you anything, and my 90 year old grandma can use Winblows. If you want to learn, you'll use GNU/Linux.


    Remember that, an improperly configured *nix box is just as bad as Windows or worse.
  • KaeL
    Posts: 16
    said:


    said:


    I might run linux on virtual box. But i got school in about 6 mins

    lol talk later dude



    I would just dual-boot GNU/Linux and Winblows. Seriously, you wont learn anything from Winblows. It's security is poor, it's unstable and has many vulnerabilities, it doesn't teach you anything, and my 90 year old grandma can use Winblows. If you want to learn, you'll use GNU/Linux.


    You can also take the time to actually learn how to use Windows though, beyond all the GUI.
  • chroniccommand
    Posts: 1,389
    said:


    said:


    said:


    I might run linux on virtual box. But i got school in about 6 mins

    lol talk later dude



    I would just dual-boot GNU/Linux and Winblows. Seriously, you wont learn anything from Winblows. It's security is poor, it's unstable and has many vulnerabilities, it doesn't teach you anything, and my 90 year old grandma can use Winblows. If you want to learn, you'll use GNU/Linux.


    You can also take the time to actually learn how to use Windows though, beyond all the GUI.

    It still sucks balls lol.
  • Xin
    Posts: 3,251
    said:


    said:


    said:


    said:


    I might run linux on virtual box. But i got school in about 6 mins

    lol talk later dude



    I would just dual-boot GNU/Linux and Winblows. Seriously, you wont learn anything from Winblows. It's security is poor, it's unstable and has many vulnerabilities, it doesn't teach you anything, and my 90 year old grandma can use Winblows. If you want to learn, you'll use GNU/Linux.


    You can also take the time to actually learn how to use Windows though, beyond all the GUI.

    It still sucks balls lol.


    Even behind the GUI there isnt an awful lot you can do, to be fair the security on Windows 7 is pretty good nowadays, apart from the obvious factor about viruses and stuff, a lot of the usual word/pdf metasploit payload attacks wont work on 7, but yeah i agree linux is better , on topic if you want a windows compiler/ide use codeblocks.
    Xin
  • DizzY
    Posts: 155
    The upside about windows is simplicity and compadibility.
  • chroniccommand
    Posts: 1,389
    said:


    The upside about windows is simplicity and compadibility.



    Simplicity is for noobs, skids and people who can't think for themselves. As for compadability, Linux is comatable with everything.

    Anything Unix > Winblows
    End of story.
  • DizzY
    Posts: 155
    Simplicity is for noobs, skids and people who can't think for themselves.

    Nah, it's just I'm lazy (: Also can you play games on Unix?
  • chroniccommand
    Posts: 1,389
    said:


    Simplicity is for noobs, skids and people who can't think for themselves.

    Nah, it's just I'm lazy (: Also can you play games on Unix?



    Depends on what games..
  • Xin
    Posts: 3,251
    You wouldnt really need to play games on linux anyway
    Xin