C Programming Tutorial 6, If Statement Pt.1

So in order to test that what you’re gonna want to do is just type out if4 if and then put two parentheses after it and then inside of these parentheses I’m just going to type if I double equals six now the reason that we need double equals is because the single equals we’ll just assign a variable value so.

This single equals right here we’ll just assign I the value of six however the double equals will check for equality so we’ll check to see whether or not I is in fact equal to six now let’s just say that we want it to print out hello if I.

Is equal to six so I’m just gonna press ENTER right here press tab and say print F and then inside of parentheses right here I’m just gonna say hello and you don’t need to actually tab in right here.

On the new line you could put this line of code that you want to happen if the if statement is true on assignment same line as the if statement however I just prefer to have it down here and tabbed in.

Right so once again what this is going to do right here is it’s just going to create a new integer variable called I and set it equal to six then it’s going to check to see if I is equal to six and remember we need to double equals because it’s checking for.

Equality it’s not assigning a value like the single equals does then if I is equal to six is just going to print out hello if I is not equal to six then it will not execute this code right here it will just exit the program all right so let’s just go ahead and run this and make sure that it works and.

We should get hello on the screen because I does equal six yep we get hello right there and let’s just go ahead and change the value of I to something else to make sure that it actually does work I’ll just change it.

To nine and now we should get nothing on our screen since I does not equal six yeah we got nothing now right now after our if statement it’s only.

Executing one line of code let’s say that we want to execute multiple lines of code well what you’re gonna have to do is you’re gonna have to put those.

Lines of code inside of curly braces so if we want it to do only one line of code we do not eat these curly braces but if we wanted to do more than one line of code we’re going to have.

To put these curly braces after the if statement right here so now let’s just say that I wanted to create a new character variable called C and I’ll just go ahead and set C equal to C I guess and then.

Let’s just go ahead and print out that variable C so I’m gonna say percent C because we’re printing out a character and then the name of my variable which is C alright so since I is equal to 9 it should print out nothing so let’s just go ahead and run this yeah we get nothing and now let’s go ahead and change.
And go ahead and run this again and as you can see we.

Got C right there because what it did is it created a new integer variable.

Called I instead of equal to 6 then it check to see if I is equal to 6 which it is so it came down here and created a new character variable called C and set it equal.

The character C and then went ahead and just printed the value of C out onto the screen then it came down here and exited the program so on our screen we got C right there now let’s just say that we wanted this to do something if I is not equal to 6 well in order to check and see.
If I is not equal to 6 you’re.

Gonna want to use this you’re gonna want to do exclamation point equals so now what it’s doing is it’s saying if I does not equal six then execute this.

Code right here so now let’s go ahead and run it and when we run it we should get nothing on our screen since I is equal to six so if I is not equal to six while I is equal to.

Gonna go ahead and exit the program it’ll never get to this code right here let’s go ahead and run it yep we get nothing on our screen now let’s go ahead and change the value of I to anything the side six so I’ll just change it to seven and run.
It and we should get see on our screen..