Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

difference between loop and conditional statement in c

Status
Not open for further replies.

Parth86

Member
Hi all
generally I understand what is while loop and conditional statement (if else ). but I am confused on when do we use while loop and conditional statement in c programming.

C:
while (condition)
{
   statements;
}
if the condition is true loop will run infinite and condition is false loop will never run


C:
if (/* condition goes here */)
{
   /* if the condition is true, this code will execute */
 }

 else {
   /* if the condition is false, this code will execute */
 }

what is use of this type of structure

C:
do nothing

while (condition1)
  {

  }
while (condition2)                               

  {

  }
 
In Microcontrollers you never want to return from main and so an infinite while(1) loop is the best solution. Depending on the application I may run the main code 50 times a second by waiting for a timer to time out like so,
Code:
    while(1){            //loop forever
        while(!TMR2IF);    //wait for timer
        TMR2IF=0;
  
        //main code would be here
  
    }//end main loop
The bit TMR2IF is set automatically by the hardware.

The if(condition) construct is used to only do something when something is true. An example would be if a byte has been received by the RS232 module,
Code:
        if(RCIF){
      
            //code here to handle the received byte
      
        }//end if

Again, RCIF is set by the hardware when a byte is received.

Mike.
 
An if statement checks if an expression is true or false, and then runs the code inside the statement only if it is true. The code inside the loop is only run once...

Code:
if (x > y)
{
   // this will only happen once
}

A while statement is a loop. Basically, it continues to execute the code in the while statement for however long the expression is true.

Code:
while (x > y)
{
  // this will keep happening until the condition is false.
}

When to use a while loop:
While loops are best used when you don't know exactly how many times you may have to loop through a condition - if you know exactly how many times you want to test a condition (e.g. 10), then you'd use a for loop instead.
 
In an if statement if the stuff in the brackets is true the code runs, if its not true the code in the else runs.
If 'this' is true do this, else do that.
A while loop usually has an effect on the statement, in other words if you said while (i<10) and then you incremented i in the while loop it would iterate 10 times.
Do can be used ahead of while to ensure the code within the while loop runs at least once.
My explanation might not be so good, its getting late.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top