I made this widget at MyFlashFetish.com.

C++ : If statement

The ability to control the flow of your program, letting it make decisions on what code to execute, is valuable to the programmer. The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false. One of the important functions of the if statement is that it allows the program to select an action based upon the user's input. For example, by using an if statement to check a user entered password, your program can decide whether a user is allowed access to the program. 


Without a conditional statement such as the if statement, programs would run almost the exact same way every time. If statements allow the flow of the program to be changed, and so they allow algorithms and more interesting code.


Before discussing the actual structure of the if statement, let us examine the meaning of TRUE and FALSE in computer terminology. A true statement is one that evaluates to a nonzero number. A false statement evaluates to zero. When you perform comparison with the relational operators, the operator will return 1 if the comparison is true, or 0 if the comparison is false. For example, the check 0 == 2 evaluates to 0. The check 2 == 2 evaluates to a 1.


Here are the relational operators, as they are known, along with examples:


Basic IF statement :


the structure of an if statement is as follows;
--------------------------------------------------------
if ( TRUE )
{
    //execute the next statement.
}
--------------------------------------------------------


here is a simple example that shows the syntax.
--------------------------------------------------------
if ( 5 < 10)
{
    cout << " five is less than ten" ;
}
--------------------------------------------------------




Else statement :


sometimes when the condition in an if statement evaluates to false, it would be nice to execute some code instead of the code executed when the statement evaluates to true. The "else" statement effectively says that whatever code after it (whether a single line or code between brackets) is executed if the if statement is FALSE.


example;
-------------------------------------------------
if ( TRUE)
{
   //execute these statement if TRUE
}


else 
{
   //execute these statement if FALSE
}
-------------------------------------------------




Else If Statement :


another use of else is when there are multiple conditional statements that may all evaluate to true, yet you want only one if statement's body to execute. You can use an "else if" statement following an if statement and its body; that way, if the first statement is true, the "else if" will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else if statements to ensure that only one block of code is executed.


-----------------------------------------------------------------------------------

if ( <condition> ) 
{
  // execute these statements if <condition> is TRUE
}


else if ( <another condition> ) {
  // execute these statements if <another condition> is TRUE and
  // <condition> is FALSE
}


-----------------------------------------------------------------------------------

No comments:

Post a Comment