Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,326 Categories: 12

Total Tutorial Views: 41,381,467

C Programming Tutorial 5, If Statement Part 1

Tutorial Name: C Programming Tutorial 5, If Statement Part 1  

Category: PC Tutorials

Submitted By: Nissan

Date Added:

Comments: 2

Views: 1,089

Related Forum: PC Building Forum

Share:

C Programming Tutorial 5, If Statement Part 1


The program we are going to be using to learn C programming language is "CodeBlocks" and here is a link to the download its a free program.
Link : [ Register or Signin to view external links. ]
All of these tutorials are going to be done in an console application.
in this tutorial i will be showing you how to get input from user

So lets get started :

In this tutorial we are going to begin looking into if statements and an if statement will allow you to test a condition for example lets just say we wanted to check if i is equal to 6 we only want something to happen if i is equal to 6 so in order to test that what you are going to want to do is just type out :


if( i == 6){

}


now the reason why we want double equals is because the single equals will just assign an variable value so the single equals will just assign i to 6 how ever the double equals simply checks if i equal to 6

now lets just say we wanted to print out hello if i equal to 6 that would look like :


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i = 6;
    if(i == 6){
        printf("Hello");
    }
    return 0;
}


once again what this is going to do right here is create a new integer variable called i and set it equal to 6 then ifs going to check if i is equal to 6 then if i is equal to 6 its going to print out hello on the screen and if i is not equal to 6 then it will just exit the program so now lets go ahead and run the program and make sure it works and we should get hello on the screen because i is equal to 6.

[ Register or Signin to view external links. ]

and as you can see it works we do get hello on the screen now let change the value of i to 7 now your code will look like :

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i = 7;
    if(i == 6){
        printf("Hello");
    }
    return 0;
}


now you can see we do not get anything on the screen because i is not equal to 6:

[ Register or Signin to view external links. ]

I Hope You Have Learned Something In This Tutorial

Ratings

Current rating: 5.60 by 5 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"C Programming Tutorial 5, If Statement Part 1" :: Login/Create an Account :: 2 comments

If you would like to post a comment please signin to your account or register for an account.

NissanPosted:

Thanks for the feedback.

KatsumiPosted:

Quite a nice tutorial. Highly recommending for anyone wanting to get into C programming. IF statements are quite easy to grasp in my opinion