C++ has a huge list of decision making/condition coding statement some of them that we are going to learn are:-
1)if statement
2)if else statement
3)do while statement[will learn while looping]
IF STATEMENT
The if statement may be implemented in different forms depending on the complexity of conditions to be tested.
• Simple if statement
• If ………else statement
• If ………else ….. if ladder statement.
Note: - I have attached 2 MS word file i.e. meant to be downloaded. The 1’st one consist program related to simple programming and the other of if else programming.
You just need to download them and write the program in your compiler and enjoy.
SIMPLE IF STATEMENT
The general form of simple if else statement as already written above is:-
if(Test Condition)
{
}
statement;
For e.g.:-
Code:
if(income>4000)
{
tax = 0.1*income;
income = income – tax;
}
[quote]cout<<”Tax”<< tax;
cout<<”Income”<< income;
……[/quote]
The above part of the program is testing whether the income is greater than 4000 or not. If ‘yes’ the tax @of 10% is deducted from the income and then the final v alue should be printed.
Note:-In the whole programming condition = = must be used because we are comparing/conditioning the values so always use = =. Also && should be use for and. AND || this should be used for or condition.
THE IF….ELSE STATEMENT
The if ….else statement is an extension of simple if statement. The general form is:-
if(Test Condition)
{
< Program Statement>
}
else
{
< Program Statement>
}
If the test condition is true then the program statement under if braces will be executed. In either case if the condition become false then else statement will proceed.
Note: - You need to put braces { } at the starting and at the end if to or more program condition is given otherwise it may cause error. For e.g.:-
For e.g.:-
Code:
[quote]If (code<0)
Cout<<”Code is negative number”;
else
Cout<<”Code is positive number”;
[/quote]
In above program, if code is less than 0 than the code number entered is negative if not then it is positive.
SOME PROGRAMING BASICS
Code:
[quote]
#include
#include
void main()
{
clrscr();
int a,b;
cout<<"Enter the 2 no";
cout<< endl<<"Enter A\n";
cin>>a;
cout<<"Enter B";
cout<<"\n";cin>>b;
if(a>b)
cout<<"A is Greatest\n";
else
cout<<"B is Greatest \n";
getch();
}
[/quote]
This program is absolutely correct but if the same program is written with few more condition then following steps should be taken:-
Code:
[quote]
#include
#include
void main()
{
clrscr();
int a,b;
cout<<"Enter the 2 no";
cout<< endl<<"Enter A\n";
cin>>a;
cout<<"Enter B";
cout<<"\n";cin>>b;
if(a>b)
{
cout<<”A=\t”<< a;
cout<<”B=\t”<< b;
cout<<"A is Greatest\n";
}
else if(a==b)
{
cout<<”A=\t”<< a;
cout<<”B=\t”<< b;
cout<<"Both are equal\n";
}
else
{
cout<<”A=\t”<< a;
cout<<”B=\t”<< b;
cout<<"B is Greatest \n";
cout<<”A=\t”<< a;
cout<<”B=\t”<< b;
}
getch();
}
[/quote]
Here = = is used because comparison is done between 2 or more variable if u want to put character then use single quotes ( ‘ ‘ ) or double quotes. But unfortunately we cannot use more than one alphabet also if u assigned the value of char to 128. The e.g. given here is directly started with if else so don’t get confused:-
For e.g:-
if(day= =’s’&& day= =’S’)
cout<<”The day is Sunday”;
But if u will give more than one alphabet then error will come. In the under written program.
if(day= =’su’ && day= =’Su’)
cout<<”The day is Sunday”;
THE IF…..ELSE….IF LADDER
This is another way of putting it together when multiple decision are involved. A multiple decision is a chain of its in which several statements are attached with each if and else conditions.
The general form Is:-
[quote]
if(condition a)
{
}
else if
{
}
else if
{
}
………[/quote]
This construct is known as if….else…..if ladder . The condition are evaluated from top to down. As soon as a true condition is found the program statement attached to it will be executed otherwise compiler will look for other condition .
This type of program can be used for making results as follows.:-
The program written below shows Simple Interest based on several condition.
Code:
[quote]
#include
#include
void main()
{
clrscr();
int t;
double amt,si;
cout<<"Enter the value of amount\n";
cin>>amt;
cout<<"Enter the total time period\n";
cin>>t;
if(amt>50000 && t>5)
si=(amt*t*11)/100;
else if(amt>=20000 && amt<50000 && t>8)
si=(amt*t*12/100);
else if(amt<20000 && t>=1)
si=(amt*t*6)/100;
cout<<"\nThe amount entered=\t\t"<< amt;
cout<<"\nThe Time entered=\t\t"<< t;
cout<<"\nThe Int. charged=\t\t"<< si;
cout<<"\nThe total amt. to be paid=\t"<< amt+si;
getch();
}[/quote]
No comments:
Post a Comment