C program while loop problem !

Can the following code ever lead to an infinite loop?

Page 1 of 1

6 Replies - 2177 Views - Last Post: 08 March 2010 - 04:20 PM Rate Topic: -----

#1 rmitboy   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-August 09

C program while loop problem !

Posted 07 March 2010 - 01:51 AM

values of i and j are not changed in the body of the loop. We should not use computer for checking!
     #include <stdio.h>
     int main(void){

	int i, j;
	printf("Input two integers: ");
	scanf("%d%d", &i, &j);
	while(i*j <0 && ++i != 7 && j++ != 9){
		printf("Huy\n");
	}	
     }


Regard

This post has been edited by no2pencil: 07 March 2010 - 01:54 AM
Reason for edit:: Corrected code tags

Is This A Good Question/Topic? 0
  • +

Replies To: C program while loop problem !

#2 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: C program while loop problem !

Posted 07 March 2010 - 01:55 AM

What are you trying to do? That while loop seems to be pointless, but it can never be infinite though.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6841
  • View blog
  • Posts: 31,532
  • Joined: 10-May 07

Re: C program while loop problem !

Posted 07 March 2010 - 01:56 AM

Your while loop condition syntax is incorrect.

The for loop uses the incremental in the evaluation, it's the 3rd parameter.

The while, on the other hand, must have the values changed within the loop. Not in the conditional comparison evaluation.

     #include <stdio.h>
     int main(void){

        int i, j;
        printf("Input two integers: ");
        scanf("%d%d", &i, &j);
        while(i*j <0 && i != 7 && j != 9){
//        while(i*j <0 && ++i != 7 && j++ != 9){
                printf("Huy\n");
                i++;
                j++;
        }       
     }


Was This Post Helpful? 0
  • +
  • -

#4 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: C program while loop problem !

Posted 07 March 2010 - 01:58 AM

no2pencil, you are incorrect. Run this:
while (i * j < 0 && ++i != 7 && j++ != 9)
{
	printf("%d\n", i);
	printf("%d\n", j);
        printf("Huy\n");
}


and you will see that i and j changes their values directly from the loop condition.

This post has been edited by sarmanu: 07 March 2010 - 01:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 Munawwar   User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: C program while loop problem !

Posted 07 March 2010 - 02:08 AM

Quote

Your while loop condition syntax is incorrect.

Nope. You are wrong. It compiles.

This post has been edited by Munawwar: 07 March 2010 - 02:08 AM

Was This Post Helpful? 0
  • +
  • -

#6 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6841
  • View blog
  • Posts: 31,532
  • Joined: 10-May 07

Re: C program while loop problem !

Posted 07 March 2010 - 02:11 AM

I didn't mean that it won't compile...

I just think (imho) it's being used wrong.

Since you are checking for less than zero, you'll need to input negative numbers to match all 3 conditions. Since you are using AND logic, all three conditions will need to be met to exit the loop.
Was This Post Helpful? 0
  • +
  • -

#7 rmitboy   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-August 09

Re: C program while loop problem !

Posted 08 March 2010 - 04:20 PM

I am appreciate for your excite discussion.
The question simply challenge us as I mentions in the requirement "Can The Following Code Ever Lead To An Infinite Loop?". That's all.
Thanks you guy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1