[TUT][C] Using the IF statement

Discussion in 'Programming General' started by Affix, Oct 15, 2008.

[TUT][C] Using the IF statement
  1. Unread #1 - Oct 15, 2008 at 4:06 AM
  2. Affix
    Joined:
    Oct 15, 2008
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0

    Affix Newcomer

    [TUT][C] Using the IF statement

    Like many other languages C has an if statement and it is pretty easy to grasp the concept. The if statement in C is very similar to the one you may have seen if you code PHP.

    Ok lets start we will create a little application to ask for a name and compare it.

    Start your C application by adding stdio.h and create your main function

    HTML:
    #include 
    
    int main() {
    
    }
    Now we will create a variable called name the variable will be char.

    HTML:
    char name[1024];
    Now we need to get the user to do some input by asking the name and receiving user input using the printf and sprintf functions. Your code should be similar to the followng.

    HTML:
    printf("Hello, Please enter your name blow!\n");
    scanf("%s", name);
    Now say we want to check if a specific name is entered into the variable we need to use an if statement.

    The if Statement looks like this

    HTML:
    if(name == "Affix") {
    //ENTER THE TRUE FUNCTION
    } else {
    //ENTER THE FALSE FUNCTION
    }
    The if statement above will check if Affix or iHack is entered as the name. Now if they are entered we will pass the TRUE function if the input it not valid we pass the FALSE function. I am simply going to display a message

    Code:
    if(name == "Affix") {
    printf("%s, You Fucking Rock\n", name);
    } else {
    printf("%s, You Suck\n", name);
    }
    Now you have your fully functioning IF statment in C. Congradulations.
     
  3. Unread #2 - Oct 15, 2008 at 6:50 AM
  4. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    [TUT][C] Using the IF statement

    A tutorial is meant to teach people something.

    Did you go in to depth about what conditional statements actually are and what they do, and for the matter, what is more effective in multiple cases of comparison? Perhaps this would be better if you actually went in depth rather than just tell a user "this is an if statement. If you want something to happen under a condition, put this here, otherwise put that there." A tutorial should include the "how" and "why", no matter how simple.
     
< Help with Random Numbers | how long does it take to learn C++ >

Users viewing this thread
1 guest


 
 
Adblock breaks this site