c++ If else statement

If else in c++

Sometimes we prefer to choose one thing over another thing. This is not difficult if we want to execute one block of code over another block.

c++ also give choice to programmer that if he wants to execute code on some condition then he can do.
if-else is a conditional statement that allows to program to execute code on some condition.

if-else statement is very important when we use conditional programming and menu-driven programming.

if is used to execute the specific block of the code, if the specified condition is true
else is used to execute the block of the code, if the same condition written for the if become false.
  

what is the syntax of if statement?

syntax of if
(condition) {
 //block of code which is executed when if the condition is true.
}

Example:

if(-1>-2)
{
cout<<" -1 is the greatest number ";
}

The if statement mostly uses with the else but we can also use a single if statement if needs in the program.
Following is the syntax of the if-else statement.

What is syntax of the if-else statement?

syntax:


if( )
{


}
else
{

}

For more understanding:
if else

For more understanding some examples here to clarify how to code this concept in the program. The following example is a very handful to understand, how to use the else with if statement.

How to use if else in the cpp?

Example1:

#include<iostream>
using namespace std;
int main()
{
//it is a program to check the number is positive number or negative number
int num;
cout<<" Enter the number "<<endl;
cin>>num;
if(num>=0)
{
cout<<" it is a positive number"<<endl;
}
else
{
cout<<" it is a negative number"<<endl;
}
return 0;
}

output

enter the number 5
it is a positive number


In the above program, a condition is written in the if statement(num>0) check the number is greater than zero. If the condition is true then display it is a positive number. If condition is false then else block will be executed and print it is a negative number


Example 3

#include<iostream>
using namespace std;
int main()
{
int a,b,num;
cout<<" press 1 for addition "<<endl;
cout<<" press 2 for subtract "<<endl;
cout<<" press 3 for multiplication "<<endl;
cout<<" press 4 for division "<<endl;
cout<<" enter the first number "<<endl;
cin>>a;
cout<<" enter the 2nd number "<<endl;
cin>>b;
cout<<" Enter the choice ";
cin>>num;
if(num==1)            //check the condition for num=1
            {
cout<<" addition ="<<a+b<<endl;
                }
else if(num==2)                //check the condition for num=2
                                {
cout<<" subtract ="<<a-b<<endl;
                                        }
else if(num==3)            //check the condition for num=3
                                            {
cout<<" multiplication ="<<a*b<<endl;
                                                }
else if(num==4)                //check the condition for num=4
                                                    {
cout<<" division ="<<a/b<<endl;
                                                         }
else//check the condition for num other than 1,2,3,4
                                                            {
cout<<"invalid input"<<endl;
                                                            }
return 0;
}

Output:
press 1 for addition
press 2 for subtract
press 3 for multiplication
press 4 for division

enter the first number 4
enter the 2nd number 4

Enter the choice 3
multiplication =16


In the above example, if-else ladder use for menu-driven programming. First take input from the user, after giving the choice to the user which operation he wants to perform. In code, all the cases cover which will make users.

If any problem in the understanding of else statement then comments your problem.








Comments

  1. what is difference between else if and if else statement

    ReplyDelete

Post a Comment

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

Flutter layout