3 Replies - 1981 Views - Last Post: 07 April 2010 - 10:48 PM Rate Topic: -----

#1 ihype   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 10

Regarding sentinel controlled loop

Posted 07 April 2010 - 10:13 PM

Dear all,

May i know which one is a better sentinel controlled loop and why?

scanf("%d",&sum);
while(num!=SENT){
/* process num */
scanf("%d",&num);
}


OR

do{
scanf("%d",&num);
if(num!=SENT)
{/* process num */}
}while(num!=SENT);


What's the main difference actually?

Your help is much appreciated. =)

This post has been edited by JackOfAllTrades: 08 April 2010 - 05:44 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Regarding sentinel controlled loop

#2 taiku   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 92
  • Joined: 28-October 09

Re: Regarding sentinel controlled loop

Posted 07 April 2010 - 10:31 PM

first of all, please include your code inside code tags!

im changing my answer, the second one is technically "better" but both will work. the second could be considered "better" because you dont repeat a line of code.

This post has been edited by taiku: 07 April 2010 - 10:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 taiku   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 92
  • Joined: 28-October 09

Re: Regarding sentinel controlled loop

Posted 07 April 2010 - 10:42 PM

And the difference lies in the type of loop you are using.

a while() loops until the condition is untrue, thus if the condition is untrue before the while is entered, the while will be passed.

a do-while() loops until the condition is untrue, but will always enter the loop the first go around.
Was This Post Helpful? 1
  • +
  • -

#4 ihype   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 10

Re: Regarding sentinel controlled loop

Posted 07 April 2010 - 10:48 PM

View Posttaiku, on 07 April 2010 - 09:42 PM, said:

And the difference lies in the type of loop you are using.

a while() loops until the condition is untrue, thus if the condition is untrue before the while is entered, the while will be passed.

a do-while() loops until the condition is untrue, but will always enter the loop the first go around.


hmmm... ok.

Thank you very much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1