Latest Post
Showing posts with label Decision And Loop Control. Show all posts
Showing posts with label Decision And Loop Control. Show all posts

Loop Control Statement in C Programming - " do....while "

Written By Unknown on Thursday, October 10, 2013 | 3:49 AM

Do While Statement :
The value of condition for " do.....while loop " is placed at the end of the loop.Like :

                     do
                          {
                               statement;
                           } while (condition);

Watch the program carefully : 
                            #include<stdio.h>
                            #include<conio.h>
                             main()
                                      {
                                       int i=0;
                                       do
                                      {
                                          printf(" %d ",i);
                                          i++
                                        } while (i<5);
                                        getch();
                                        }
                                           
OUTPUT :

                                               0,1,2,3,4

Program Analysis :
->                                      do
                                          {
                                          printf(" %d ",i);
                                          ................................
                                          ................................
Here the value of i will be shown first.

-> i++;
     }while(i<5);

In this step,the value of i will be increased by 1 and value will be 0 to 1 and in the mean time condition will also be checked.As i<5 so condition will be true and printf function will be executed and 1 will be shown.

-> In this way when the value of i will be 5 than the condition will be false because i can't be equal to 5 according to the condition(i<5).So the loop will end.

Loop Control Statement in C Programming - " while "

While Statement :
While statement is also called while loop.The way to write while statement is :
                                                       
               while (condition)
                        statement;

Here condition is any of True C- Programming Expression.Usually relational expressions used here.While statement calculate the value of condition at first.

If the calculated value of condition become true than while statement will be executed but if the calculated value of condition become false than the statement will not be executed....

Watch the program very carefully :

                                                                 #include<stdio.h>
                                                                 #include<conio.h>
                                                                  main()
                                                                  {
                                                                                     int i;
                                                                                     i=1;
                                                                                     while(i<=10)
                                                                                     {
                                                                                                 printf("%d",i);
                                                                                                 i++;
                                                                                      }
                                                                    getch();
                                                                    }


OUTPUT :
                                         1,2,3,4,5,6,7,8,9,10



Program Analysis :
->i is initialized first of all.i=1

->while(i<=10)
So the first expression is true and body of while will execute.

->Value of i will be printed and i will be increased by 1 so the value of i is now 2.

->again for the value of i is true according to the condition(i<=10)

->So when the value of i will be 11,the condition will be false and while loop will end.

Loop Control Statement in C Programming - " for loop " 2

Written By Unknown on Wednesday, October 9, 2013 | 11:36 PM

Nested for loop :
We can use one or more for loops in between one for loop.This is called nested for loops.Sometime it become very very important to use nested for loop.


For loop variations :

We can initialize more than one variable in to for lop.Like :
                                                           for( i=0, j=0; i<20; i++ )
                                                           for( p=1,q=1; p+q<20; p++,q++ )


We can also initialize variables outside of for loop.Like :
                                                           i=0  //initialization
                                                           for( ; i<20; i++ )


If we need to use increment/decrements outside of for loop than we can do like this :
                                                           i=0
                                                           for(;i<20;)
                                                           {
                                                                 printf("%d",i);
                                                                 i++;    //increment
                                                            }


We can use logical operators too in between conditions like :
                                                           j=0;
                                                           for ( i=0; i<10 && j!90; i++ )

Infinite for loop :
If we need to create an infinite loop we can use for.To create this infinite loop we cannot use initialization,condition and increment into for.Like :
                                                            for( ; ; )
                                                            {
                                                                  printf("\n finite loop \n");
                                                             }

After running this program press Ctrl - Break at a time.

Loop Control Statement - " for Loop " 1

Loop Control :
Sometime we need to do a specific task more than one time.In that case,we don't write that statement again and again.We use loop controlling statement.

In " C " there are three loop controlling statement :
                                               
                                                  1. for
                                               
                                                  2.while
                                               
                                                  3.do.....while

for loop :
To use one or more statement for a selected time in " C " we use " for " loop.

Ex :
I need to print " SHUVO " for 50 times on computer screen.Here,no need to write "printf" for 50 times.We can do it more easily by using " for " loop.

                                                   for(i=1; i<50; i++)
                                                   printf("SHUVO");

The structure of "for loop" is as follow:
                     
                          for(initialization;                    condition;               increment/decrements)
                                     |                                                               |                                         |               |
          Variable and Assignment Operator             <, >, >=, <=, <<, ||                          ++           - -


Initialization :
The starting value of a loop is declared here.Usually,variables and assignment operators( = ) are used here.

Condition :
Here usually used relational expressions.When loop will end,that depends on this conditional section.If the condition become false in this section the program will also be terminated.

Increment :
Declared value of variables in the initialization section turned  incremented or decremented in this section.

Watch The Program :
                                                       #include<stdio.h>
                                                       #include<conio.h>
                                                       main()
                                                       {
                                                        int i=1;
                                                        for(i=1; i<=15; i++)
                                                        Printf("The Value is ?%d",i);
                                                        getch();
                                                        }

OUTPUT:

1,2,3,4,5,6,7,8,9,10,11,12,1315


Program Analysis:
->  for ( i=1; .....; ..... )
This is initialization part in for loop.Here the value of i is declared 1.

-> This is the conditional part.If the condition become satisfied printf() function will execute.So when i=1 and 1 is less than 15,condition is true.

-> for ( .......; .......; i++ )  
After executing printf(), i++ will execute.The loop will be working and value of i will be increasing by one every time until the condition become false.

Decision Control Statement In C Programming - " Ifelse and elseif "

If Else :
We can use “else” with “if” but it is up to us that will we use or not.The way to use else with is as below:

If(expression)
        statement  1;
else
        statement 2;


If the value of “if” expression become true than statement 1 will be execute otherwise statement 2 will execute.

Watch the program carefully :
                                                                    #include<stdio.h>
                                                                    #include<conio.h>
                                                                     main()
                                                                     {
                                                                     int numb;
                                                                     printf("Enter How Much Money do You Have? : ");
                                                                     scanf("%d",&numb);
                                                                     if(numb>50)
                                                                     printf("You are rich");
                                                                     else
                                                                     printf("You are poor");
                                                                     getch();
                                                                     }

Program Analysis:
Here two simple statement and condition are working that if the entered  number  become grater than 50,First statement will excuete otherwise if the number remain under 50 second statement will be published.



Elseif:
We can create “if else if” chain through else if in C Program.The structure is as following:
                                                          If(expression 1)
                                                                   Statement 1;
                                                          Else
                                                                  If(expression 2)
                                                                   Statement 2;

                                                           Else

                                                                   Statement3; 

Decision Control Statement In C Programming - " If "

Written By Unknown on Tuesday, October 8, 2013 | 10:02 AM

If Statement :
If is very important decision control statement in C - Programming.Usual way to write if statement is :

                              If ( expression )
                              statement;

OR :

                              If ( expression )
                              {
                               statement1;
                               statement2;
                               }

First of all "if" evaluate the value of expression.If the value of expression become true than computer will work as the statement says.

If the value of expression become false than statement will not be executed.Usually if - expression works with relational operator ( > , < , >= , <= , == )

Ex :                                           if(p>q)
                                                  printf("p is greater than q");

Watch the program Carefully :
                                                            #include<stdio.h>
                                                            #include<conio.h>
                                                            main()
                                                            {
                                                             int number;
                                                             printf("Enter Your Number :");
                                                             scanf("%d",&number);
                                                             if(number>=33)
                                                             printf("\nYou Pass In The Exam");
                                                             getch();
                                                             }

Program Analysis :
Here if will analyze the value of expression is true or false.If the value of inputted number is greater than or equal 33,the expression will be true and program will run otherwise program will be terminated.

We can use logical operator too with "if" expression.Like:
                                     

                                        #include<stdio.h>
                                        #include<conio.h>
                                        main()
                                        {
                                        int numb;
                                        printf("Enter Your Number :");
                                        scanf("%d",&numb);
                                         if(numb>=61&&numb<=80)
                                        printf("You Got First Division");
                                        if(numb>=45&&numb<=60)
                                         printf("You Got Second Division");
                                         if(numb>=33&&numb<=44)
                                         printf("You Got Third Division");
                                        if(numb>=81&&numb<=100)
                                        printf("You Got Letter Marks");
                                        getch();
                                        }

CONFUSE??

CONFUSE??
 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Learn Programming - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger| Distributed by Rocking Templates