Programming Language Geek

June 10, 2008

if.. else if.. statement

Filed under: C / C++

Suppose you are trying to set conditions that if ever your condition will be met, then it does the first step, Plan A. If the condition will not be met, then it will execute Plan B. These are just some of the functionalities of the "if.. else if.. statement".

The syntax:

if (condition){
some statements…
}

Note that the statements to be executed should be enclosed within two brackets (open bracket { and close bracket }). The characters above in bold should be strictly followed since this is the actual syntax of the statement. The condition is boolean. If it returns no value, then it’s considered false, while otherwise, it’s true.

Some statements represents the statements to be executed whenever the condition is true. If not, then the statements will be skipped. To handle conditions much more effectively using if statements, you can add the else or else if statement.

if (condition){
some statements…
}
else if (another condition){
some statements…
}
else{
some statements…
}

Now how does the compiler handle these statements? First, it checks the first condition it encounters. If the condition is true, then it will execute the statement enclosed within it. If not, then it searches for an else if statement until it meets the condition which returns true. If not still, then it will look for the else statement. When it reaches the else statement, the codes included within it will right away be executed. Notice that in the else statement, there is no condition required.

Later on, we’ll be discussing some variations of the if.. else if.. statement. Just take note of the concepts since you might be applying this in the future. 

Get free blog up and running in minutes with Blogsome
Theme designed by Jay of onefinejay.com