• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Programming Question

Status
Not open for further replies.

Wjs

New Member
  1. Explain the three statements needed to to formulate a for loop.
Answer:

  1. Something to do before starting

  2. A test to perform as long as it’s true, keep looping

  3. Something to do after each loop (increase a variable)
I have answer the above question but don't know is correct anot. Please advise
 

Pommie

Well-Known Member
Most Helpful Member
That is correct for C.
I.E. for (i=1;i<8;i++)
OR for (i=1;i<128 && !done;i*=2)

However for Basic the test (2) is automatic.
I.E. for 1=1 to 10

Mike.
 

Daniel Wood

Member
The first answer seems a little vague to me.
If you're doing this as part of a test, I think you would need to talk about the initialisation or setting the variable to be used in the second or third section to get the marks.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
The first answer seems a little vague to me.
If you're doing this as part of a test, I think you would need to talk about the initialisation or setting the variable to be used in the second or third section to get the marks.
I would imagine it's a multiple choice question?, so 2 would be correct.
 

Daniel Wood

Member
Who knows.. Maybe Wjs can enlighten us :p

The question asks to explain, so I think the question requires three separate answers.
If this is the case, they are all correct, but they could be worded better.
 

Dr_Doggy

Well-Known Member
I think thats right.... Explain the three statements!! .... " for( 1 ; 2 ; 3 ) "

but whats wrong with answer 1&3
1 happens prior to loop (before starting)
and
3 happens at the end of each loop
 

KeepItSimpleStupid

Well-Known Member
Most Helpful Member
I think the point is to know the difference between a FOR and a WHILE loop. the HINT is the WHILE loop will always execute once.

Consider
x=4
While x<=3
Print x
x=x+1
Next

and

For x = 0 to -1 step 1
print x
Next
 

NorthGuy

Well-Known Member
C:
for (a; b; c) {
  d;
}
is equivalent to

C:
a;
while (b) {
  d;
  c;
}
There are some differences, e.g. the use of "continue", but it's probably not important here.
 

Wade_Hassler

New Member
(I think the OP should clarify the question, but, til then...)
It seems that OP gave the three statements as answer to the question "what are the three etc?" and that his post is just (unwarranted) self-doubt
 

DerStrom8

Super Moderator
Most Helpful Member
His question was very poorly worded. I think he should re-ask but make his question MUCH more clear.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
The question is right...

Question : - Explain the three statements needed to to formulate a for loop.

WJS has answered..
  1. Something to do before starting

  2. A test to perform as long as it’s true, keep looping

  3. Something to do after each loop (increase a variable)
WJS asks : -- Are my answers near the mark??

We need to agree or disagree with WJS's answers....
 

DerStrom8

Super Moderator
Most Helpful Member
Hmm, I must have misread it. Yes, I agree that the three answers provided are correct.
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top