Monday 20 July 2015

Control instructions - for, do-while loops in C !

The concept of loop is important for both programmer and hackers.
For programmer, you want to execute some code 50 times then instead of writing that code 50 times, you can do it  by writing that code only one time and repeat the execution 50 times with the help of loop.
Simply we can say, A loop allow us to execute a statement multiple times in our program.
For hacker, you want to crack Facebook password ;-) and you have a word-list of 100 passwords, you can do it by attempt manually. The other way of cracking password is by writing a loop program which simply attempt password 100 times:

for(int i=0;i<100, i++);
The most widely use of loops in hackers worlds is to password cracking, network cracking, brute force attack.

There are mainly 3 types of loops in C programming:
for loop
while loop
do...while loop

for loop: for loop is used when you want to loop for a certain number of iterations. for loops start counting at a beginning value, test the value for some condition,execute the statement, and increment the value for the next iteration.

                                         
                           

The Basic format is as follows:
for(<initialization>; <test>; <change/Increment>){
<statement>;
}
For example,
for(int i=0;i<100, i++);
In the first part, initialization is done first.The second part is the test or condition that controls the loop. Here our loop will run 100 times. Then next is the increment step. Here i is increment by 1 after execute the statement.
Remember, If the test evaluated is false, the loop is never executed and terminates.

if the test expression in not present, then it is taken as permanently true, so
for( ; ;){

}
is an "infinite loop". To exit from infinite loop, use break or return statement.

while loop: if you use while loop in your program that means you want to execute a set of instructions in a loop while a condition is true. It tests the conditions before executing the loop body.

                                   
                             

The format is as follows:
while(<conditional test>){
<statement>;
}
The while loop checks whether the  conditional test is true or not. If it is true, statement or code inside the body of while loop is executed,that is, code inside the braces { } are executed. Then again the test expression is checked whether test expression is true or not. This process continues until the test expression becomes false.

#include <stdio.h>

int main()
{
  int i = 0;

  while ( i < 10 )
   {
      printf( "%d\n", i );
      i++;           
  }
  system("PAUSE");  
  return 0;
}

Now compile this code and see the output!

do...while: It is almost similar to while and for loop. But the difference is in do...while loop, code is executed at first then the condition is checked whereas in for and while loop, loop test expression is checked at first the beginning of code.

                                                     


The general syntax of do..while is:
do{
    statement
}
while(expression)
{

}

For Example:
#include <stdio.h>

int main()
{
    int sum=0, i;
    do
    { printf("Enter a number\n");
    scanf("%d",&i);
    sum+=i;
    }
    while(i!=0);
     printf("sum=%d",sum);
     system("PAUSE");  
  return 0;
}

when you compile this code and run it:

Enter a number
4
Enter a number
3
Enter a number
4
Enter a number
0
sum=11

As you can see that it first execute your code and then test the expression.

break and continue:
break statement: break is used in terminating the loop immediately after it is encountered.The break statement provides an early exit from for, while, and do..while loops.

basic syntax of break statement
break;

In C, break statements are also used in switch...case statement.

continue statement:It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements are used. It is related to break, but are less often used. In the while and do loop, this mean that the test part is executed immediately while in for loop, control passes to the increment step.
Let's see:
int main()
{
    int i,num,sum;
    for(i=1,sum=1;i<=4;++i)
    {
        printf("Enter number%d:",i);
        scanf("%d",&number);
        if(number==0)
            continue;
        sum+=number;
    }
    printf("sum=%d",sum);
    return 0;
}

In this program, when number equals to zero, it skips the statement sum+=number;  and continue the loop.
The continue statement applies only to the loops, not to switch. A continue inside a switch inside a loop causes the next loop iteration.

 if you like this post or have any question, please feel free to comment !

No comments:

Post a Comment

Blogger Widget