LOOPS MIKROC

on Wednesday, 25 September 2013
In this post we will look at the 2 types of control handling statements and they are
  • Selection Statements
  • Iteration Statements
Now let us see one after the other, but note that this is just an overview of C but not in depth explanation which i have said you in my previous post.  

SELECTION STATEMENTS:

There are two types of Selection statements, they are
  • IF
  • SWITCH 
IF Statement:
General format of the the statement is 

if(expression)
 {
    Statements;
 }
else
{
   Statements; 
 }

If there is only one Statement in the flower brackets { , }  then they can be neglected. 'Else' is optional. 
Example:
If 'x' is greater than 10 then increment 'y' or else increment 'z' 

if(x>10)
{
  y++;
}
else
{
  z++;
}

we can write this as below
if(x>10) y++;
else
z++; 

SWITCH Statement:
If we want to compare many conditions then we will use "Switch statement".

Switch(condition)
{
              case condition1;
                      statements;
                      break;
              case condition2;
                      statements;
                      break;
              case condition3;
                      statements;
                      break;
              .............................
             .............................
              default:
                     Statements;
}

Example:
Take the decision depends on the value stored in the variable 'xyz' , if it is 1 then store A=20 , if it is 6 then store A=50 , if it is 8 then store A=60 , if non of these then store A=0.

Switch(xyz)
{
                   Case 1:
                         A=20; break;
                   Case 6: 
                         A=50;break;
                   Case 8:
                         A=60;break;
                    default:
                          A=0;
}






ITERATION STATEMENTS:

They are also called as loop statements used for repeating the same statement for predefined number of times. We will look at these statements which are supported by MikroC.
  • For Statement
  • while Statement
  • do Statement
  • goto Statement
FOR Statement:
The syntax of FOR statement is 
for(initial expression;condition;increment expression)
{
    statements;
}

Example:  Repeating a loop for 10 times 
for(i=1;i<=10;i++)
{
   statements;
}

If we want to repeat the loop for infinite number of times then 
for(;;)
{
     statements;
}

WHILE STATEMENT:
This is the simplest form of 'for' statement. Look at the syntax below
While(condition)
{
      Statements;
}

For example if we want to repeat the loop for 10 times then 
x=0
While(x<10)
{
       statements;
       x++;
}

If we want infinite loop using 'while' then 
While(1)
{
    statements;
}

DO WHILE STATEMENT:
It is similar to that of 'While' statement but here the condition is checked after the execution of the statements.
do
{
    statements;
}while(condition)

GOTO STATEMENT:
First let us look at the syntax 

goto label;

The control will be shifted to the 'label' in the program. 
Let us look at a simple example

Loop:
          statements;
           x+ +;
if(x<100) goto Loop;
If  x<100 is satisfied then control will be shifted to the label "Loop" . 

AUTHOR: YOUSTRON SIC

8 comments:

Anonymous said...

awesome bro.....keep it up .... highly precise and informative

baltee

YOUSTRON SIC said...

Thanq very much :-)

Unknown said...

while and for both are entry controlled loop and also both are same.for loop preffered over while because all loop controlling statements are in same line so it is easier than while loop.

YOUSTRON SIC said...

I totally agree with the above statement said by some "unknown person" , for is preferred over while, but while is little simple(but it depends on the point of view of the person). I'm thankful for your comments :-)

Anonymous said...

Hello, how to break an infinite loop?
while(1) //Infinite Loop
{
statement;
}
For an example, the statement is LED blinking, so how to stop the infinite LED Blinking? So far, I have to use the reset button to stop it.
Thank you.

Aman Rustagi said...

Thank you very much. Can you please share some sample C programs for using for loop, while loop, do while loop, if else statement stc.

Sowpath das said...

What's the hardest part about obtaining a college degree? Is it passing your subjects? Well, some would possibly think so. Most college students could attest that the most challenging part about getting a college education is, as you may have guessed, paying for your college tuition fees.http://www.how-todo.xyz/

Unknown said...

Good

Post a Comment