If else condition

Conditions give a huge amount of flexibility to a program; they allow the program to choose for itself. Well not choose entirely for themselves, but rather for the options that you give it. Giving the program the ability to choose for itself gives a tremendous amount of versatility. Without conditions the same program would run the same over and over again, and what fun is that? The first condition we will discuss is the most common, the if statement. Here is the syntax.

if (condition)
{
       //execute whatever is inside these brackets
}

The condtion inside the parenthesis is what has the power to choose whether or not to execute. Let us look at a situation where we have three variable x,y,z. First we will initialize them and assign them values. In this case if x is greater than y then the condtions will execute.

int x,y,z;

cin>>x;
cin>>y;
 
 if(x>y)
  {
                 z = x+y;
                 cout<<z;       
   }


Once we enter in the values of x and y the condtion will determine whether or not x is greater than y if so then z will be given the value of the sum of x and y and then print out the result. At this point if x is not greater than z than nothing will happen but we can fix this by simply giving the program another option. This is the else if.

int x,y,z;

cin>>x;
cin>>y;
         
        if(x>y)
              {
                z=x+y;
                cout<<z;
              }
       
        else if (x<y)
              {
                z=x-y;
                cout<<z;
              }

As you can probably see else if just gives the program another option and in this case if x is less than y then z will given the value of x minus y and then be printed to the screen. You are given the power to give a program as many conditions as you want simply by adding as much else if statements as you wish. So in this case what if x isn't greater or less than y? If we still wanted something to happpen in this situation then we would just add and else statement to the program.

//Full Working Program

#include <iostream>

using namespace std;
int main()
{
int x,y,z;

cin>>x;
cin>>y;
 
  if(x>y)
              {
                z=x+y;
                cout<<z;
              }

  else if (x<y)
              {
                z=x-y;
                cout<<z;
              }

  else
              {
                cout<<"x is nor greater or less than y so they must be equal"<<endl;
              }

return 0;
}

If x is not greater or less than y then the first two condtions will not execute leaving the last condtion. The else can be thought of as a last resort, notice how it doesn't even have a condition, this is because the condition for the else statement is if nothing else executes it will. There are a few different operations that can be inside a condition here are a few.

<  //less than
>  //greater than
<= //less than or equal to
>= //greater than or equal to
== //checks to see if variable is equal to value
=! //checks to see if variable is not equal to value

Now that we have these new found tools lets try something new that we couldn't of done before. Lets build a program that as soon as it starts up is asks you whether or not you would like to calculate the volume of a right circular cone or the area of a circle.

//Full Working Program

#include <iostream>

using namespace std;

int main()
{

int x; //integer x to take in either 1 or 2
double r,h,V,A; //r for radius, h for height, V for volume, and A for area

       cout<<"To calculate the volume of a right circular cone press 1"<<endl;
       cout<<"To calcualte the area of a circle press 2"<<endl;
       cin>>x;
   
//bring in the condtions to determine what calcualtion to execute

if (x==1) //checks to determine if x is equal to 1
{
cout<<"Please enter a radius"<<endl;
cin>>r;
cout<<"Please enter a height"<<endl;
cin>>h;

V=1.0/3.0*3.14159*r*r*h; //equation for the volume of a right circular cone

       cout<<"The radius is "<<r<<" and the height is "<<h<<endl;
       cout<<"so the volume of the right circular cone is "<<V<<endl;
}

if (x==2) //checks to determine if x is equal to 2
{
cout<<"Please enter a radius"<<endl;
cin>>r;

A=3.14159*r*r; //equation for the area of a circle

       cout<<"The radius is "<<r<<" so the area is "<<A<<endl;
}

else
{
       cout<<"Invalid input "<<endl;
}

return 0;
}

This program is very straight forward first it decides what condtition to execute given by the users input. If 1 is entered the program will ask for a radius and height and then give you the volume of right circular cone. If the user enters 2 then the program will calculate the area of circle once the radius is given. If the user doesn't input 1 or 2 then the program will tell the user they have entered invalid input.




0 comments:

Post a Comment