4/05/2012

How to use if else while and for conditional statements in c c++ and java


This was a guest post by D.srinath reddy.

This article clearly explaind the use of if if-else while and for conditional statements.

if statement:-

syntax:-
if(condition)
{
block of statements;}
if statement is a conditional statement used to check whether the given condition is true or false and used to execute the block of statements with the result generated by if statement.
for eample if we give
if(a==5)
{
printf("equal");
}
in the above example if the value a is equal to 5 then the control enters into if block and executes the printf() statement and prints the message equal.
if the value a is not equal to five then the control wont enter into if block and the statements are not executed under if block.
so if statement follows only one rule. if the condition is true, enter into block and execute statements. if the condition is false, then skip the block.

if else statement:-

syntax:-
if(condition)
{
block of statements;
}
else
{
block of statements;
}
if else is very much similar to the if statement. else statement is used to execute certain block of statements if the given condition is false.
for example if we give
if(a==5)
{
printf("equal");
}
else
{
printf("un equal");
}
in the above example if the value a is not equal to five then the control enters into else block and the printf() statement is executed and the message un equal is printed
as in if statement if the condition is failed then it skips the if block and continues the execution. but in if else statement if the condition is failed then it skips the if block and executes the else block. here both if and else blocks are not executed simultaneously. only either if or else is executed.

while() statement:-

syntax:-
while(condition)
{
block of statements;
}
while statement is used to execute the block of statements continously untill the given condition is going to be false. this is  used to reduce code length. if we declare some statements under while() block  then the statemnts are executed continously untill the given condition is failed or not satisfied. lets take an example
while(i<=10)
{
printf("\n  value of i is %d",i);
i++;
}
in the above example the printf() statement and the i++ statements are executed continously untill the given conditin(i<=10) is false or not satisfied. it means it will execute the block of statemnets for every value of i that is less than 10. when the i value becomes more than 10 then it skips from the while() block.

for() statement:-

syntax:-
for(initialization;condition;increment/decrement)
{
block of statements;
}

This is very much similar to the while() statement. this is used to execute the block of statements continously untill the given condition is going to be false by incrementing or decrementing a particular value at every revolution or loop.  if we declare some statements under for() block  then the statements are executed continously untill the given condition is failed or not satisfied. lets take an example
for(i=1;i<=10;i++)
{
printf("\n  value of i is %d",i);
}in the above example the printf() statement is executed continously untill the given condition(i<=10) is false or not satisfied and it increments the value of i by 1 at every regualr revolution or loop. it means it will execute the block of statements for every value of i that is less than 10. when the i value becomes more than 10 then it skips from the for() block.

About the guest author:

srinath reddy is an engineering student, part time blogger,software developer & web designer. he would like to keep you updated with latest programming tips,tricks and tutorials. he mainly writes on CODERS HUNT. you can follow his page on facebook. and you can follow him on facebook   google+   twitter   linked in.

No comments:

Post a Comment

If you're having issues, Please leave an email address I can contact you on -
I advise you to also "subscribe to the comment feed" and get email updates when I respond to your question.

Hyperlinks are not allowed, Spam/advertising comments will NEVER BE TOLERATED and will be deleted immediately!

Thanks for reading,
Admin