Ads 468x60px

Tuesday, 1 January 2013

if else

if
It is a conditional branching statement, it evaluates a statement or a group of statement if the condition is true.

Syntax
if (condition)
   statement; 

or

if (condition)
{
    statement 1;
    statement 2;
    .
    .
    .
    statement n;
}

if condition executes the statement next to if, for more than one statements to be executed, we need to put the statement in a particular block ({ }).

if else
It is also a conditional branching statement. It is having two parts one is if and other is else. If the if part is true then the statements under if will executed or if the if part is false then statements under else part will be executed.

Syntax
if (condition)
    statement;
else
    statement;


or

if (condition)
{
    statement 1;
    statement 2;
    ...
    statement n;
}

else

{
    statement 1;
    statement 2;
    ...
    statement n;
}

Same as if , if we have more than one statements to be executed we need to place them in a block.

if else if 
Third and last part, in it we can create as many conditions as we want.

Syntax
if (condition)
    statement;
else if (condition)
    statement; 
else if (condition)
.
.
else 
    statement; 


or 



if (condition)
{
    statement 1;
    statement 2;
    statement n;
}

else if (condition)

{
    statement 1;
    statement 2;
    statement n;
}


else if (condition)

{
    statement 1;
    statement 2;
    statement n;
}

.
.
.
else 

{
    statement 1;
    statement 2;
    statement n;
}

Example 1
Write a program to input a number and find whether the number is even, odd, positive or negative.
#include<iostream.h>
#include<conio.h>

void main()
{
  int a;
  clrscr();
  cout<<"Enter a Number:\t";
  cin>>a;
  
  if(a==0)
    cout<<"Neutral";
  
  else if(a>0)
  {
    cout<<"Positive and ";
    if(a%2==0)
    cout<<"Even";
    else
    cout<<"Odd";
  }

  else 
  {

    cout<<"Negative and ";
    if(a%2==0)
    cout<<"Even";
    else
    cout<<"Odd";
  }
  getch();
}
Explanation of the above program
At first control comes to main, screen cleared with clrscr, and then a variable a of integer type declared.
A message for entering number is displayed by the cout function and then the input of that number is taken by cin.

now lets suppose we entered 3
control will come to the first condition that is 3 (value of a) equals to 0, the condition is false 3 is not equal to zero, the statement next to if will not executed.

Control will come to the next condition (else if) and will check that is 3 greater than zero, the condition is true   (3 is greater than zero) now the control will come inside the condition and will execute the first line
and with the output function Number is positive line will be printed to the output screen.

Now, control is inside the next if condition placed inside else if, using if under an if condition is called as nested if.

Control will check that is a%2 equal to zero, the condition is false because 3 % 2 is not equal to 0, statement next to this if  not executed and the else part will be executed that is even.
Output of the Above Given Program
Share if you liked this Post using your favorite sharing service:

Subscribe Updates, Its FREE!

0 comments:

Post a Comment

 
Related Posts Plugin for WordPress, Blogger...