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 (4)

Powered by Vanilla. Made with Bootstrap.
C for newcomers, Part 5
  • chroniccommand
    Posts: 1,389
    In this section of my "C for newcomers" guide, I will be going over logic and the "if" statement.
    -----------------------------------------------------------
    So, what is logic? Logic, in my opinion, is basically using your brain. If more people used logic there would be nobody losing $20,000 in Vegas. What I will teach you today is how to use logic in C. Using logic will help your program make decisions and carry them out. Lets say we wanted a program that asked for a users input. Lets make one of those stupid fake viruses that you usually see in batch files(Note: I will use a command called "system" in this program. All you need to know about this for now is it lets you carry out system commands and will be used to simply scare somebody).

    #include <stdio.h>

    int main()
    {
    char s;
    printf(\"There is a virus 0n j00r system! Would you like to get rid of it?[Y/N]\n\");
    s = getchar();
    if(s == 'Y')
    {
    printf(\"Ok, Virus is gone!\n\");
    }
    else(s == 'N')
    {
    printf(\"Fine, say goodbye to your computer!\n\");
    system('ls /etc\");
    system('ls /etc\");
    system('ls /etc\");
    }
    return(0);
    }

    If you've ever coded in another language before you should know what if and else is. If not, I will not explain it:
    So we are declaring character "s" first, then we are asking the user for input. If the user presses Y it prints the virus is gone and terminates the program. If the user presses "N" it will say goodbye to the computer then use the system command to use "ls /etc" 3 times. This will just list everything in the /etc folder 3 times then terminate, making a retard shit there pants.

    The if command in C is used to compare and contrast things and if they equal either true or false, they are carried out(Executed). Notice how the if and else statements don't need a colon after, just a new set of brackets. In C it is good to use the brackets when using IF and ELSE statements. Now suppose we want to make a hidden function, like an easter egg in the program, So if the user presses the T key, it prints something. This is done using the ElseIf command. Take a look:

    #include <stdio.h>

    int main()
    {
    char s;
    printf(\"There is a virus 0n j00r system! Would you like to get rid of it?[Y/N]\n\");
    s = getchar();
    if(s == 'Y')
    {
    printf(\"Ok, Virus is gone!\n\");
    }
    else if(s == 'T')
    {
    printf(\"You found teh easter egg! Good job!\n\");
    }
    else(s == 'N')
    {
    printf(\"Fine, say goodbye to your computer!\n\");
    system('ls /etc\");
    system('ls /etc\");
    system('ls /etc\");
    }
    return(0);
    }

    See what I did there? The else if command is usually used when comparing more than 2 things.

    || and &&
    Now for a lesson on the OR and AND operators. These are used in C to compare different things. Lets say we wanted a program to compare temperature. Lets take a look at using AND

    #include <stdio.h>

    int main()
    {
    char s;
    printf(\"What is your favorite number?\n\");
    s=getchar();
    if(s == '1' || '2' || '3' || '4' || '5')
    {
    printf(\"Your favorite number is either 5 or less!\n\");
    }
    else
    {
    printf(\"Your number is greater than 5!\n\");
    }
    return(0);
    }

    Now see how our if statement has the || 's? That means if its 1 OR 2 OR 3 OR 4 OR 5 it prints that statement. There is an easier way to do this in C, try this code:

    #include <stdio.h>

    int main()
    {
    char s;
    printf(\"What is your favorite number?\n\");
    s = getchar();
    if(s<5)
    {
    printf(\"The number is less than 5!\n\");
    }
    else
    {
    printf(\"The number is greater than 5!\n\");
    }
    return(0);
    }

    This program uses the less than operator which is "<" But this program could pick up some errors, because we are comparing a number. The "getchar()" function compares a string. So what if we wanted to compare numbers? Try adding this line after the getchar() function
    b = atoi(s);

    Now use that and change the if statement to:
    if(b&lt;5)

    So now we are using atoi, which is changing the string to an integer. Don't forget to declare "b" as an integer though! Now take a look at this program that uses that.

    #include &lt;stdio&#46;h&gt;

    int main()
    {
    char s;
    int b;
    printf(\"What is your favorite number?\n\");
    s = getchar();
    b = atoi(s);
    if(b&lt;5)
    {
    printf(\"The number is less than five!\n\");
    }
    else
    {
    printf(\"The number is greater than five!\n\");
    }
    return(0);
    }

    See that? We used some simple logic using the if operator to ask for a number. We stuck in the atoi function to convert "s" which is originally a character to an integer.

    I hope this helped quite a bit. This is simple logic in C using the if and else operators in C.

    --Chroniccommand
  • Xin
    Posts: 3,251
    Good job! Another great tutorial for C learners ;)
    Xin
  • Bursihido
    Posts: 406
    first wwhat is && oprater do ?

    thanks :)
  • r0ck
    Posts: 4
    nice post but what about the AND operator it is missing.
    and what "s=getchar();" mean?!
  • chroniccommand
    Posts: 1,389
    said:


    nice post but what about the AND operator it is missing.
    and what "s=getchar();" mean?!



    s=getchar();

    Basically sets the "s" variable to user input with the getchar() function.
  • r0ck
    Posts: 4
    Thanks for the replay, what if the input was any other key and i want it to say "Error" what is the if statement input will be, i mean this part:
    "if(s == '?')"
    thanks
  • sangf
    Posts: 203
    i think you're looking for the != (not equal to) operator.


    if (s != '?') printf(\"this is not a question mark!\");


    you could do it with logic, too; but that was already mentioned in this guide (see: else/else if).
  • r0ck
    Posts: 4
    thanks sangf,
    sorry for asking many question but what is the kind of variable that accept both characters and numbers, char for charachter and int or float for numbers what is for both
  • sangf
    Posts: 203
    c is not dynamically typed, so you can't really mix and match; you might have to do a little parsing work to read your data. however, if you don't need to do numerical computations (comparrisons against int/float types, math operations, etc.), you could just use a string or a char type (see chronic's favourite number example):


    char input&#91;80&#93;;
    scanf(\"%80s\", input); // needs #include &lt;stdio&#46;h&gt;
    if (strcmp(input, \"1337\") == 0) printf(\"you entered the number 1337\"); // needs #include &lt;string&#46;h&gt;


    scanf is used as a more flexible way of reading from stdin (user input), it supports the reading of user input into multiple variable types. in this example, it is used to read in a string with a maximum of 80 characters to the variable input. you can read more about this function here.

    strcmp is used to compare strings, it returns 0 if the compared strings are equal to each other. more here.

    i'm not sure if that answers your question, what are you trying to do exactly?
  • r0ck
    Posts: 4
    Thanks sangf,
    I was trying to make more exercises just to check if i understand the tutorial so i tried to make a small password check program so if the password is letters and numbers i couldn't do it,
  • sangf
    Posts: 203
    ah~~ then you would definitely want to use a string type of variable (char*), although checking if said string contains letters and numbers needs some additional logic. i'm not sure of the 'best' way of doing this, but looping through each character of the string should suffice:


    char input&#91;80&#93;;
    scanf(\"%80s\", input);
    for (int i = 0; i &lt; strlen(input); i++)
    if (input&#91;i&#93; &lt; 48 || input&#91;i&#93; &gt; 57)
    printf(\"current character is NOT a number\");


    this (untested) example uses a for loop to iterate from 0 (the first index of the character array (string)), up until strlen(input) (which will return the length of the string, ie. how many characters)). the character indexed from the string using the iteration counter (i) is used to determine if that character is a numeric character - fyi. the numbers 48 and 57 are the range of ASCII values whose characters represent numbers 0 through 9.

    strlen / loops / ascii lookup

    it's probably worth noting that not every i/o system uses an ascii character system by default. also, if you are interested in learning more c: i would recommend using a more complete tutorial.. i don't know of a decent one, but i can recommend a good book: K&R / 'The C Programming Language'.