1

I am trying to find a substring in a string. While doing this, I understood that to check for multiple conditions in for if we write

for(i=0;condition1,condition2;i++)

the value of condition1 is found and then discarded. Then, the value of condition2 is found and returned. To get around this we need to use && in case of multiple conditions to be checked by for loop in here.

for(i=0;condition1&&condition2;i++)

So far I believe I understood right, but the following for loop is not working.

for(i=0;(a[i]!='\0')&&(a[i]==b[0]);i++)
  {

      j=i+1;
      k=1;
      while(a[j]==b[k]&&a[j]!='\0'&&b[k]!='\0')
      {

        j++;
        k++; 
      }
      if(b[k]=='\0')
      {
        return i;
      }

  }
  return -1;

When i wrote the above for loop, the control is entering the loop and performing the operations, whereas in the following code, control is not even entering the for loop, and it is working correctly.

for(i=0;a[i]!='\0';i++)
  {
    if(a[i]==b[0])
    {
      j=i+1;
      k=1;
      while(a[j]==b[k]&&a[j]!='\0'&&b[k]!='\0')
      {
        j++;
        k++;
      }
      if(b[k]=='\0')
      {
        return i;
      }
    }
  }

Is there something i am missing about internal processing in for loops? I believe the way they both should work is the same. Any help in this would be appreciated.

Community
  • 1
  • 1
yash
  • 1,277
  • 2
  • 22
  • 33
  • Please answer a simple question: How is `&&` called? What does it actually do? If you cannot instantly answer, please read a C book to get the basics first. You will not get the whole picture needed to understand what you are writing. – too honest for this site Apr 12 '16 at 17:54
  • `&&` checks for multiple conditions and returns a value whether true or false @Olaf – yash Apr 12 '16 at 17:56
  • You did not answer the questions. Seriously (and not offending): Please learn. – too honest for this site Apr 12 '16 at 18:11
  • @Olaf yes i am still learning the basics of C (the above question is related to an exercise in that book) that's why i am posting the things i didn't understand on stackoverflow. that might be a simple question for you and yes now i understood where i went wrong, thank you for your advice though – yash Apr 12 '16 at 18:32

2 Answers2

0

These blocks of code don't do the same thing. In the first one, if either condition isn't met then the for loop will stop iterating. In the second block if the first condition isn't met then the for loop will stop iterating, whereas if the second one isn't met the internal code isn't executed but the for loop will continue.

As for the && operator.... It will evaluate all of the conditions IN ORDER and it will stop and evaluate to false as soon as it hits an individual component that evaluates to false. If it doesn't run into any false condition then it will evaluate to true.

PandaRaid
  • 608
  • 1
  • 9
  • 22
0

In the first code snippet your for loop has two conditions. remember when one of them is not true, the loop will terminate immediatly.

In the second code snipped you just have one condition in the for loop. The second condition is inside an if case. So if condition2 is wrong the if case won't be executed, but the for loop will still continue.

RomCoo
  • 1,848
  • 2
  • 21
  • 34