Wednesday 20 November 2013

If else if Statements

# include<iostream>
using namespace std;
void main()
{
cout<<"Enter any number";
cin>>a;
if(a==0)
cout<<"value of a is o"<<endl;
else if(a==2)
cout<<"Value of a is 2"<<endl;
else if(a==3)
cout<<"Value of a is 3"<<endl;
else
 cout<<"Value of a is not 0,2 and 3"<<endl;

}

In this program,
we will take value from user,the value user gives is store in a(data type int)
If the user give inout o(zero),then the program says that:-
"Value of a is o".......!!
How is this possible,Actually if statement is a check which will check either value of a is zero nor not..!
If it is zero then Program says that,Valus is zero.....!
But if value of a is 2,then the command transfers to thie statement and this statement is printed:-
"Value of a is 2"...!
and if the user gives value of  a is 3,then the command transfers to thie statement and this statement is printed:-
"Value of a is 3"...!
but if it is not zero then the command transfers to the else,and the program says,
"Value of a is not zero"..!
Note:-
If you want one statement to be executed with if and else,then no need to place braces(show in program),but if two or more statements are,then you will put braces,(Explain in this link:-http://jdrana.blogspot.com/2013/11/if-else-statement.html)
I hope you wil got this..!
Feel free to give suggestions in comment box.......!

If else Statement

# include<iostream>
using namespace std;
void main()
{
cout<<"Enter any number";
cin>>a;
if(a==0)
cout<<"value of a is o"<<endl;
else
cout<<"Value of a is not zero"<<endl;

}

In this program,
we will take value from user,the value user gives is store in a(data type int)
If the user give inout o(zero),then the program says that:-
"Value of a is o".......!!
How is this possible,Actually if statement is a check which will check either value of a is zero nor not..!
If it is zero then Program says that,Valus is zero.....!
but if it is not zero then the command transfers to the else,and the program says,
"Value of a is not zero"..!
Note:-
If you want one statement to be executed with if and else,then no need to place braces(show in program),but if two or more statements are,then you will put braces like this:-
if(a==0)
{
cout<<"I am Junaid"<<endl;
cout<<"Value of a is o"<<endl;
}
else
{
cout<<"I am Rana"<<endl;
cout<<"Value of a is not zero"<<endl;
}
I hope you wil got this..!
Feel free to give suggestions in comment box.......!

If statement

# include<iostream>
using namespace std;
void main()
{
cout<<"Enter any number";
cin>>a;
if(a==0)
cout<<"value of a is o"<<endl;

}

In this program,
we will take value from user,the value user gives is store in a(data type int)
If the user give inout o(zero),then the program says that:-
"Value of a is o".......!!
How is this possible,Actually if statement is a check which will check either value of a is zero nor not..!
If it is zero then Program says that,Valus is zero.....!
Note:-
If you want one statement to be executed with if,then no need to place braces(show in program),but if two or more statements are,then you will put braces like this:-
if(a==0)
{
cout<<"I am Junaid"<<endl;
cout<<"Value of a is o"<<endl;
}
I hope you wil got this..!
Feel free to give suggestions in comment box.......!

Thursday 7 November 2013

Do While Loop

Practise Programe of  Do While Loop.
 
# include<iostream>
using namespace std;
void main()
{
int a,b;
char ch;
do
{
cout<<"Enter a";
cin>>a;
cout<<"Enter b";
cin>>b;
a=a+b;
cout<<"output is"<<a<<endl;
cout<<"Are you want to continue"<<endl;
cin>>ch;
}
while(ch=='Y'||ch=='y');
}

The basic difference b/w while and do while loop is,do while loop enters into its body first time if its condition is wrong.........!
Syntax:-
"do",will come at the start.....!
"do" means,Commands in loop body will be executed whatever the condition is(either it is right or wrong).
and while at the end.......!
so,the program first ask user to enter two numbers and then display the result.......!
Then,after it will ask whether you want to continue the program or not?
if you enter Y or small y then it will again ask you to enter two numbers and so on.....!
but if you enter any other character(rather then Y or y),then the program stops(mean loop end)...!
Note that,in do while loop semi colon is place after while......!

While Loop

# include<iostream>
using namespace std;
void main()
{
int a;
a=0;
while(a<=10)
{
cout<<a<<endl;
a++;
}
}

This program is same as http://www.jdrana.blogspot.com/2013/10/include-include-void-main-int-i-fori0i.html......!
but this is done with the help of while loop
The while condition if tress control enter into while loop body,otherwise not.......!
As,For and while loop dones same thing,but While loop has its own space in c++ which we will discuss later........!
Output is:-
0
1
2
3
4
5
6
7
8
9
10
Note that 10 will also be printed because a is equal to 10....!
Feel free to give suggestions in comment box.......!
F