Learning C/C++ Part II: IF statements and variables

Submitted on: 1/1/2015 7:22:00 AM
By: Alexander of (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: C++ (general)
Views: 619
     Hello, this is Alexander. Since I finally got an email from someone who liked my previous lesson, I am going to make the second installment. This one will be about variables, and stuff like 'if' statements.


'IF' is the most important word in programming for many programs. Without it there is no conditional statements. This means that there can be only one way a program can execute. It would almost impossible to make a program without this one simple word.

     There are many things to understand when using IF statements. First, you must understand stuff like OR NOT etc. This are the most important, so I will describe how to use them in C and C++ programming below: (NOTE: ZERO IS FALSE! ONE IS TRUE!)

NOT: This just says that the program should reverse the value...for example NOT(1) would be 0. NOT(0) would be 1. NOT(any number but zero) would be 0. In C and C++ NOT is written as - ! - just one simple little character. It is very useful and can save lots of time.

AND: This is another important command, and it is used to say that if this AND this is true... for example (1)AND(0) would come out as 0. (1)AND(1) would come out as 1. (ANY REAL NUMBER BUT ZERO)AND(0) would be 0. (ANY REAL NUMBER BUT ZERO)AND(ANY REAL NUMBER BUT ZERO) would be 1. The AND is written as - && - in C++. It is just two simple characters.

OR: Very useful is the OR statement! For example (1)OR(0) would be 1! (0)OR(0) would be 0. (ANY REAL NUMBER)OR(ANY REAL NUMBER BUT ZERO) would be 1! It is simple, either one can be true and make the whole thing true. The OR is written as - || - in C++. It is also two simple characters.

     The next thing to learn is to combine them... What is !(1 && 0)? Of course, it would be 1. This is because 1 && 0 evaluates two 0 and ! 0 equals 1.

     Try some of these...they are not hard. If you have questions about them, you can email me at

A. !(1 || 0) ANSWER: 0

B. !(1 || 1 && 0) ANSWER: 0 (AND is evaluated before OR)

C. !((1 || 0) && 0) ANSWER: 1 (Parenthesis are useful)

     If you find you enjoy this you might want to look more at Boolean Algebra, which is also very helpful to programmers as it can be good for helping program conditional statements.

IF is used like this IF(TRUE)


ELSE is basically ELSE


Let's look at a simple program for you to try out on your own...

#include <iostream.h> //For output

#include <conio.h> //For getch()

void main() //Most important part of the program!


int age; //Need a variable...


cout<<"Please input your age: "; //Asks for age

cin>>age; //The input is put in age


if(age<100) //If the age is less than 100


cout<<"You are pretty young!"; //Just to show you the output


if(age==100) //Remember, if the age equals 100 needs two =


cout<<"You are old"; //Just to show you it works...




cout<<"You are really old"; //Proof that it works for all conditions



     Now, this program did not use && || ! or anything in it. This is because it didn't need too. I think you should probably be able to make your own if statements with them without having to worry too much about problems.

Other 10 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.