Translate

Sunday 13 May 2012

If else Statements in C++


if:- The statement following the if statement is executed when the condition given is true.
                                if(condition)
                                                s1;
                                s2;
                When the condition is true statement s1 and then s2 is executed. If the condition is false only s2 is executed. If we want to execute more than 1 statements when condition is true then we should write all those statements within braces {} after if.
         if(condition)
                                {
                                                s1;
                                                s2;
                                }
        s3;
when the condition is true statement s1, s2 and then s3 is executed. If the condition is false only s3 is executed.
                example:-
                                c=0;
                                if(sales>=20000)
                                                c=0.1*sales;
                                cout<< “commission = ”<<c;
                                                                           


if-else:- Also known as either or. This statement is used to select one statement and ignore the other statements.
                                if(condition)
                                                s1;
                                else
            s2;
                when the condition is true statement s1 is executed. If the condition is false s2 is executed. Thus one of the statements, either s1 or s2 is always executed.
                example:-
                                if(sales>=20000)
                                                c=0.1*sales;
                                else
                                                c=0;
        cout<< “commission = ”<<c;



if-else-if-else:- It is a branching statement which can choose and execute on of the statements available depending upon the condition.
                                if(condition1)
                                                s1;
                                else if(condition2)
            s2;
                                else
                                                s3;
                when the condition1 is true statement s1 is executed and rest are ignored. When condition1 is false condition2 is verified. If condition2 is true statement s2 is executed and other statements are ignored and so on. Thus only one statement is executed from the top, depending upon the condition. The statement following else is executed when all the conditions are false. However else clause is optional.
                example:-
                                if(sales>=20000)
                                                c=0.1*sales;
                                else if(sales>=10000)
                                                c=0.05*sales;
                                else
                                                c=0;
        cout<< “commission = ”<<c;



Lets have some examples
Q. Write a program to check whether the given year is leap year or not.
#include<iostream>
void main()
{
                int y;
                cout<< “Enter the year”;
                cin>>y;
                if((y%4==0) && (y%100!=0 && y%400==0))
                                cout<< “It is a leap year”;
                else
                                cout<< “It is not a leap year”;
}
  
Q.  Write a program to read three numbers and find the least among them.
#include<iostream>
void main()
{
                int a,b,c,s;
                cout<< “Enter three values”;
                cin>>a>>b>>c;
                if(a<b)
                                s=a;
                else
                                s=b;
                if(c<s)
                                s=c;
                cout<< “The least value = ”<<s;
}

Q.  Write a program to display grades.
#include<iostream>
void main()
{
                int m;
                cout<< “Enter marks in computer science”;
                cin>>m;
                if(m>90)
                                cout<< “Distinction”;
                else if(m>=75)
                                cout<< “Grade A”;
                else if(m>60)
                                cout<< “Grade B”;
           else if(m>50)
                                cout<< “Grade C”;
           else
                                cout<< “Fail”;
}


Q. Write a program to find whether the given positive integer is odd or even.
Q. Write a program to check whether giver integer is divisible by 7, 15 and 22.
Q. Write a program to read a, b and c of ax2+bx+c and display roots.
Q. Write a program to read a character and check whether it is digit, alphabet or special character. 

No comments:

Post a Comment

Total Pageviews