Pages

Sunday, February 20, 2011

Selection (if statement)

The if statement
-          If statement is used to evaluate something in a program. It is something like making a decision where you have a condition where if it is met do something out of that condition otherwise (if there are no other condition to consider) do something if the condition is not met.

      Forms of if statement:
1.)    Simple form
Syntax:
      if(relational expression)
{
// Block of statement here
//what will the program do
}
                        else
                                    {
                                    //Block of statement here
                                    //what will the program do
                                    }

                        Note: else is an option

2.)    Compound form

Syntax:
      if(relational expression)
{
// Block of statement here
//what will the program do
}
                        else if(relational expression)
                                    {
                                    //Block of statement here
                                    //what will the program do
                                    }
                        ....
                        ....
                        ....
                        else if(relational expression)
                                    {
                                    //Block of statement here
                                    //what will the program do
                                    }
                        else
                                    {
                                    //Block of statement here
                                    //what will the program do
                  }


3.)    Complex form      (Nested)

Syntax:

      if(relational expression)
{
// Block of statement here
//what will the program do
}

                        else if(relational expression)
                                    {
//Nested if statement where there is another if statement within
                                                if(relational expression)
                                                            {
                                                            //Block of statement here
                                                            //what will the program do
                                                            }
                                                else
                                                            {
                                                            //Block of statement here
                                                            //what will the program do
                                                            }
                                    }
                        else
                                    {
                                    //Block of statement here
                                    //what will the program do
                  }

Relational Operators:

            Operator          Description                              Sample relational expression
                >                 Greater than                                        x > y
                <                  Lesser than                                          Balance < 1000
                >=                Greater than or equal to                      Average >=75
                <=                Lesser than or equal to                        a <= b
                = =               Is equal to                                           choice == ‘x’
                !=                 Not equal to                                        answer != ‘d’

            Note:   There is no space between the 2 equal sign in the (equal to) operator
Expression is any valid combination values, variable, operators, functions, etc.

Sample program:
            This program will ask the user to enter his/her age. The program will then evaluate if the age entered is juvenile or not (below 18).

#include<iostream>
using namespace std;

main( )
{
            int age;
            cout<<”Enter your age: “;
            cin>>age;
            if(age<18)
                        {
                        cout<”You are an underage”;
                        }
            else
                        {
                        cout<<”Welcome to Adulthood!”;
                        }
return 0;
}

Here the age entered by the user were evaluated if it is lesser than 18 and then execute the block of statement that follows (statement enclose by { and } ).  If the evaluation of the condition is not true (false) then the else statement will be executed. Thus, what follows is an example of an output of the program:

                Enter your age: 19
                Welcome to Adulthood!

No comments:

Post a Comment