Tuesday 22 March 2011

Boolean logic and if statements

Without the need to make "decisions", your program will just be a linear stream of code that always outputs the same type of data. To check conditions, we use the if statement.

Before even talking about the statement itself, it is necessary to cover certain aspects of boolean logic. There are certain operators that you must know when dealing with logic.

Greater than >
Less than <
Negation !
Equality check ==
Inequality check !=
Greater than or equal to >=
Less than of equal to <=
And &&
Or ||

Standard parentheses will also come in handy when dealing with large conditions.

This seems like an eyesore right now, but I'm sure you'll soon find this natural. The basic structure of the if statement is as follows:

if(condition)
{
    //result
}

Seems pretty simple, right?
Let's say I have some integer x, and I want to print a line of text only if the value of x is currently greater than 5. We would have:

if(x>5)
{
    printf("This will get printed!!");
}

Easy!
Likewise, if we wanted to check if it was strictly equal to 5:

if(x==5)
{
    printf("It is equal to 5!");
}

The basic idea behind this boolean logic is that if the statement contained within the parentheses is true, then the condition returns a value of 1. If false, a value of 0 is returned.
This is very important, as new programmers often make a silly mistake, I will show you:

//This is a very common programming mistake
if(x=5)
{
    printf("What's wrong here?");
}

Can you see the difference? Rather than using the comparison operator, I used the assignment operator! Here, we are actually assigning a value to x, not comparing one! Although it is just a one character difference, it completely changes the meaning of the statement. Instead of having the condition return true or false (1 or 0), the condition is returning the assigned value of x, 5! The if statement also interprets this as a "true" value. In other words, no matter what the value of x was before the if statement ran, it will now be 5 when it does run, and hence, it will always return true, defeating the purpose of writing the statement altogether!

The beauty of if statements is that they don't simply end if your condition is false. There's also the option of passing the flow of control to an "else" which runs if the first condition is not true. For example:

if(8<5)
{
    //this will always be false
}
else
{
    //run some code!
}

This way, you can use the else as a "default" execution.
But wait, there's more! You can combine ifs and elses to create a string of conditions

if(condition)
{
}
else if(another condition)
{
}
else if(yet another condition)
{
}
...this can go on for a while
else
{
}

Notice how each consecutive else if will be run if the previous condition is not true. And that in this case, if none of them are true, then the else block will run. Note that it is not necessary to terminate with an else block. Hope this has been useful! Next time, I'll talk more about logic and how to create more complex conditions.

10 comments:

  1. good to know, nice post

    + follower:)

    ReplyDelete
  2. This really does make a world of difference.

    ReplyDelete
  3. this is some stuff that I had trouble with myself at the start of learning Java. Again, I'm so glad C++ is so similar! I'll be learning C++ next year, at my new college.

    ReplyDelete
  4. Yes the base for all programming languages.

    ReplyDelete
  5. I gotta learn C..... maybe one of these days....

    ReplyDelete
  6. Good thing you toned down the difficulty. At first the posts were a bit like wat.

    ReplyDelete
  7. At first i was "waat"ing but then I went ohhh. Good stuff.

    ReplyDelete
  8. All I work with is Boolean Logic for programming PLDs. I love it.

    ReplyDelete
  9. my son is having this stuff at school! following it for him :)

    ReplyDelete
  10. looks like i can use this almost the same as java and C# wich is good actually.

    ReplyDelete