0

Following is the insertion sort code i wrote

#include<stdio.h>
#include<conio.h>
void main()
{
    int i, j, a[10], temp, f = 0, k = 0;
    clrscr();
    printf("\n\t\t\t\tINSERTION SORT\nEnter the Array:-\n");
    for (i = 0; i <= 9; i++)
        scanf("%d", &a[i]);
    for (i = 0; i <= 9; i++)                  // 1st for loop
    {
        temp = a[i];
        for (j = 0; j <= i - 1; j++)               // 2nd  for loop
        {
            f = 0;
            if (a[i]<a[j])                      //  if loop
            {
                for (k = i - 1; k >= j; k--)            //   3rd for loop
                    a[k + 1] = a[k];
                a[k + 1] = temp;
                break;                              // break statement
            }
        }
    }
    printf("\nThe sorted array is:-\n");
    for (i = 0; i <= 9; i++)
        printf("%d\n", a[i]);
    getch();
}

I was told that break statement stops that particular instant(nth time of currently running innermost loop & initiates its next instance ( n+1 time). So here I am confused that whether the break statement here will stop the 3rd loop or if condition. I was however told that it will affect the second loop.

Can anyone here please tell me on which for loop it is going to have its effect.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Brijesh Roy
  • 33
  • 2
  • 6

1 Answers1

4

The break will stop the loop, not the if statement.

So here:

for (i = 0; i <= 9; i++)                 // 1st for loop
{
    temp = a[i];
    for (j = 0; j <= i - 1; j++)         // 2nd  for loop
    {
        f = 0;
        if (a[i]<a[j])                   //  if STATEMENT
        {
            for (k = i - 1; k >= j; k--) //   3rd for loop
                a[k + 1] = a[k];
            a[k + 1] = temp;
            break;                      // break statement
        }
    }
}

, assuming that i has the value 0, when the break gets executed, it will prevent the 2nd loop from executing again, and you will exit that loop. As a result, you will go again at the 1st loop, where a new iteration will start, with i equal to 1.

What you describe suggests the continue keyword, which again has to do with loops only, it's not something that affects an if statement directly.

In the code above, if a continue statement was used instead of a break one, then you would skip that iteration (meaning that nothing below continue would be executed), and move on with the next iteration of the 2nd loop (giving j its incremented by one value). However, in your code, there is nothing bellow that line, so it wouldn't make any difference.


PS:

This comment is wrong:

if (a[i]<a[j])  //  if loop  <-- WRONG

An if statement is not a loop.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • actually I dont understand where the reference to `continue` is coming from (i mean I see the comment, but I dont understand it). – 463035818_is_not_an_ai Aug 15 '17 at 12:15
  • @tobi303 from the code structure of OP's code. This would leave the block of the `if` because there's no other statement in the surrounding `for` block after the `if` block. –  Aug 15 '17 at 12:18
  • Hmm @tobi303 might have a point here. It might confuse the OP, should I remove the `continue` part from my answer? – gsamaras Aug 15 '17 at 12:19
  • I would at least explain what continue does, because in general it does not "break" out of an if block. – 463035818_is_not_an_ai Aug 15 '17 at 12:20
  • 2
    Yes, but I think with the explanation now added, it shouldn't be confusing (and actually gives some added value over the dupe I found ;)) –  Aug 15 '17 at 12:21