LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-03-2009, 02:35 PM   #1
venix
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Rep: Reputation: 0
two different loops, are they doing the same thing ?


Loop 1:
Code:
for (int i =1 ; array[i]!=0; i++) 
		if (&array[i] == block) 
		{
			
			printf("array[i] = %d\n ", array[i]);
			printf("array[i+1]= %d\n", array[i+1]);
			printf("array[i-1]= %d\n", array[i-1]);
			position = i;
			blockSize = *block;
			break;
		}
Loop 2:
Code:
int i =1;
  while(array[i]!=0)
  i++;
	if (&array[i] == block) 
	{
		printf("array[i] = %d\n ", array[i]);
		printf("array[i+1]= %d\n", array[i+1]);
		printf("array[i-1]= %d\n", array[i-1]);
		position = i;
		blockSize = *block;
	}
are this achieve the same thing ? if not what are the difference in the output?

Last edited by venix; 12-03-2009 at 07:02 PM.
 
Old 12-03-2009, 02:41 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Why not run them both and find out????

What is the context of this?
 
1 members found this post helpful.
Old 12-03-2009, 02:43 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,234

Rep: Reputation: 184Reputation: 184
Quote:
Originally Posted by pixellany View Post
Why not run them both and find out????
LOL Yeah.
Quote:
What is the context of this?
Homework, i'm sure.
 
Old 12-03-2009, 02:54 PM   #4
jaydot
LQ Newbie
 
Registered: Jan 2007
Distribution: PCLinuxOS 2009.2
Posts: 5

Rep: Reputation: 0
i'm no programmer, but it looks to me like one is for a one-off print and the second for more than one print.

with 'while' in the second, so long as the stated condition is true, churn out another print.

if it is homework, i can help lead you far, far astray.
 
Old 12-03-2009, 03:02 PM   #5
venix
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by smeezekitty View Post
LOL Yeah.

Homework, i'm sure.

Do you want my code to check it out ? it will take you 1 week and 3 days to find out
 
Old 12-03-2009, 05:14 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by venix View Post
Do you want my code to check it out ? it will take you 1 week and 3 days to find out
Now that you have exchanged your little jabs, how about answering my question: Run the 2 programs and see what they do.
 
Old 12-03-2009, 05:28 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,526

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Wink

Quote:
Originally Posted by venix View Post
are this achieve the same thing ?
No, Loop 2 won't compile since it is missing some punctuation.
Quote:
if not what are the difference in the output?
Loop 2 can't give output since it can't run.
 
Old 12-03-2009, 07:03 PM   #8
venix
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ntubski View Post
No, Loop 2 won't compile since it is missing some punctuation.

Loop 2 can't give output since it can't run.
this is not a homework. Is one part of homework that i came up while trying to make mymalloc() function , where did you see the missing punctuation ?
 
Old 12-03-2009, 08:37 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
There is quite a bit of difference between the two, but it is impossible to tell if the differences are intentional or not. Maybe you could explain a little more about why you are asking your question and what happened when you ran the code.
 
Old 12-03-2009, 09:30 PM   #10
GazL
Senior Member
 
Registered: May 2008
Posts: 3,480

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
Quote:
Originally Posted by graemef View Post
There is quite a bit of difference between the two, but it is impossible to tell if the differences are intentional or not.
fixed his indentation and it looks something like this...
Code:
int i =1;
while(array[i]!=0)   /*  1  */
   i++;

if (&array[i] == block)  /*  2  */
{
   printf("array[i] = %d\n ", array[i]);
   printf("array[i+1]= %d\n", array[i+1]);
   printf("array[i-1]= %d\n", array[i-1]);
   position = i;
   blockSize = *block;   /*  3  */
}
  1. increase element index number 'i' until array element contents is zero
  2. compare pointer 'block' to address of array element i
  3. if equal, set blockSize from contents of array element pointed to by pointer block, which is pointing to array element i (*2*), which contains 0 (*1*)
Unless I'm reading it wrong (which is possible as I'm a bit rusty with C), blockSize will only ever get set to 0.

I suspect the differences are not intentional and it's just plain broken.
 
Old 12-03-2009, 09:40 PM   #11
bamboo789
LQ Newbie
 
Registered: Jun 2008
Posts: 4

Rep: Reputation: 0
I think they are missing { and } ,is right?
They are have some differece:
code1:
print array[i],array[i-1] and array[i+1] first,then i++

code2:
i++ first and then print array,so the results of printting is not same. And the value of positon are different too.The first one is less than the second one,I think.
 
Old 12-03-2009, 10:24 PM   #12
GazL
Senior Member
 
Registered: May 2008
Posts: 3,480

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
Yep, I think you're right bamboo, it makes a little more sense if he's missed out a whole { } block on that while, but there are still logic difference in there.

increasing i at the start means that &array[1] is never checked against block, so that'd need fixing for starters.

Last edited by GazL; 12-03-2009 at 10:30 PM.
 
Old 12-03-2009, 10:50 PM   #13
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Reading the code the indentation implies that the while is missing the braces, but the break in the for loop could imply that the looping should stop when the if condition is met, meaning that the while condition should have consisted of two conditions!

Yes the incrementing of the loop counter in the while loop is wrong, if the braces are missing but again are the braces really meant to be there?

As I said without knowing the intent of the code it is not possible to be any more precise.
 
2 members found this post helpful.
Old 12-03-2009, 11:29 PM   #14
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Rep: Reputation: 25
Awesome.. I can tell that reading The C Programming Language has done something.. I saw the issue(s) before I read the the rest of the thread..

I dunno why no one has answered the question yet, but far be it for me to break the silence.
 
Old 12-04-2009, 07:57 AM   #15
pintodragon
LQ Newbie
 
Registered: Dec 2009
Location: New York Mills, NY
Distribution: Slackware, Gentoo, Ubuntu
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by venix View Post
Loop 1:
Code:
for (int i =1 ; array[i]!=0; i++) 
		if (&array[i] == block) 
		{
			
			printf("array[i] = %d\n ", array[i]);
			printf("array[i+1]= %d\n", array[i+1]);
			printf("array[i-1]= %d\n", array[i-1]);
			position = i;
			blockSize = *block;
			break;
		}
Loop 2:
Code:
int i =1;
  while(array[i]!=0)
  i++;
	if (&array[i] == block) 
	{
		printf("array[i] = %d\n ", array[i]);
		printf("array[i+1]= %d\n", array[i+1]);
		printf("array[i-1]= %d\n", array[i-1]);
		position = i;
		blockSize = *block;
	}
are this achieve the same thing ? if not what are the difference in the output?
If you did indeed want Loop 2 to do the if and printf's inside the while loop then no the two loops do not do the same thing.

Loop 2 will start looking at &array[2] and check if that is == to block. If you are going to use a while loop to iterate through an array in place of a for loop then you should put the i++ after your last usage of it. So an updated Loop 2 would be:

Code:
  int i =1;
  while(array[i]!=0)
  {
    if (&array[i] == block) /* First time through i is now 1 not 2 */
    {
        printf("array[i] = %d\n ", array[i]);
        printf("array[i+1]= %d\n", array[i+1]);
        printf("array[i-1]= %d\n", array[i-1]);
        position = i;
        blockSize = *block;
    }	
    i++;
  }
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using of arrays in loops relikwie Programming 1 09-03-2009 12:23 PM
for loops, if statements PAvandal Linux - Newbie 13 07-20-2009 10:31 PM
STP loops eth777 Linux - Networking 3 10-31-2007 08:52 AM
while loops + while : blizunt7 Linux - General 3 12-04-2004 06:27 PM
loops JMK Linux - Newbie 11 04-09-2004 06:30 PM


All times are GMT -5. The time now is 05:06 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration