Topic : Programming in C/C++
Author : Alexander Allain
Page : << Previous 2  Next >>
Go to page :


check if the password is correct or not.

Without a conditional statement such as if programs would run almost the exact same way every time. If statements allow the flow of the program to be changed.

Please note that all Boolean operators or values will be capitalized to differentiate them from normal English. Note, as well, that the actual C++ symbols to the operators are described later, but the C++ symbols are not OR, NOT, and AND. The symbols simply mean the same thing.

There are many things to understand when using if statements. You must understand Boolean operators such as OR, NOT, and AND. You must also understand comparisons between numbers and most especially, the idea of true and false in the same way computers do.

True and false in C++ are denoted by a non-zero number, and zero. Therefore, 1 is TRUE and 8.3 is TRUE but 0 is FALSE. No other number is interpreted as being false.

When programming, the program often will require the checking of one value stored by a variable against another value, to determine which is larger, smaller, or if the two are equal.

To do so, there are a number of operators used.

The relational operators, as they are known, along with examples:

>     greater than              5>4 is TRUE
<     less than                 4<5 is TRUE
>=    greater than or equal     4>=4 is TRUE
<=    less than or equal        3<=4 is TRUE

It is highly probable that you have seen these before in some mathematical manifestation, probably with slightly different symbols. They should not present any hindrance understanding.
More interesting are the Boolean operators. They return 0 for FALSE, and a nonzero number for 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 !.

AND: This is another important command, it returns a one if 'this' AND 'this' are true. (1) AND (0) would come out as 0 because both are not true, only 1 is true. (1) AND (1) would come out as 1. (ANYREAL 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++. Do not be confused and think that it checks equality between numbers. It does not. Keep in mind that AND will be evaluated before OR.

OR: Very useful is the OR statement! If either (or both) of the two values it checks are TRUE then it returns TRUE. For example, (1) OR (0) would be 1! (0)OR(0) would be0. (ANY REAL NUMBER) OR (ANY REAL NUMBER BUT ZERO) would be 1! The OR is written as || in C++. Those are the pipe characters. On your keyboard, they may look like a stretched colon. On my computer it shares its key with \. Keep in mind that OR will be evaluated after AND.

The next thing to learn is to combine them... What is !(1 && 0)? Of course, it would be TRUE (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.

 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 is useful for helping program conditional statements.

The structure of an if statement is essentially:

if (TRUE)
  Do whatever follows on the next line.

To have more than one statement execute after an if statement (when it evaluates to true) use brackets.

For example,

if (TRUE)
{

  Do everything between the brackets.
}
There is also the else statement. The code after it (whether a single line or code between brackets) is executed if the IF statement is FALSE.

It can look like this:

if(FALSE)
{

  Not executed if its false
}
else
{
  

do all of this
}

One use for else is if there are two conditional statements that will both evalueate to true with the same value(but only at times. For example, x<30 and x<50 will both return true if x is less than 30 but not otherwise), but you wish only one of them to be checked. You can use else after the if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else statements followed by if statements.

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

#include <iostream.h>   
int 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 it works
  }
  else if(age==100)  //I use else just to show an example
  {
     cout<<"You are old";  //Just to show you it works...
  }
  else if(age>100)
  {
    cout<<"You are really old"; //Proof that it works for any condition
  }
  return 0;
}


This program did not use && || ! or in it. This is because it did not need too. Its purpose was to demonstrate if statements.


Lesson 3: Loops


Loops are used to repeat a block of code. You should understand the concept of C++'s true and false, because it will be necessary when working with loops.There are three types of loops: for, while, (and) do while. Each of them has their specific uses. They are all outlined below.

FOR - for loops are the most useful type. The layout is for(variable initialization, conditional expression, modification of variable) The variable initialization allows you to either declare a variable and give it a value or give a value to an already declared variable. Second, the conditional expression tells the program that while the conditional expression is true the loop should continue to repeat itself. The variable modification section is the easiest way for a for loop to handle changing of the variable. It is possible to do things like x++, x=x+10, or even x=random(5);, and if you really wanted to, you could call other functions that do nothing to the variable. That would be something ridiculous probably.

Example:
#include <iostream.h>       //We only need one header file   
int main()        //We always need this
{                 //The loop goes while x<100, and x increases by one every loop
  for(int x=0;x<100;x++) //Keep in mind that the loop condition checks
  {    //the conditional statement before it loops again.
    //consequently, when x equals 100 the loop breaks    
    cout<<x<<endl;           //Outputting x
}
  return 0;  
}

This program is a very simple example of a for loop. x is set to zero, while x is less than 100 it calls cout<<x<<endl; and it adds 1 to x until the loop ends. Keep in mind also that the variable is incremented after the code in the loop is run for the first time. WHILE - WHILE loops are very simple. The basic structure is...WHILE(true) then execute all the code in the loop. The true represents a boolean expression which could be x==1 or while(x!=7) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x==5 || v==7) which says execute the code while x equals five or while v equals 7..

Example:

#include <iostream.h>   //We only need this header file
int main()        //Of course...
{
int x=0;    //Don't forget to declare variables
while(x<100)  //While x is less than 100 do  
{
    cout<<x<<endl; //Same output as the above loop
    x++;     //Adds 1 to x every time it repeats, in for loops the  
           //loop structure allows this to be done in the structure  
  }


Page : << Previous 2  Next >>