d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C# Return To Beginning Of Loop Prematurely ?
Prev12
Add Reply New Topic New Poll
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 6 2014 11:09pm
Quote (Minkomonster @ Mar 6 2014 11:43pm)
Seems very backwards to me. Iterative loops are just special types of conditional loops. Logical flow of learning would be from conditional loops, to conditional loops that iterate over a collection, to iterative loops

Code
int i = 0, n = 5;
while(i < n)
{
    do_stuff();
    i++;
}


becomes

Code
for(int i = 0, n = 5; i < n; i++)
{
    do_stuff();
}


While leads to more in depth discussion on when and where you use each loop, why a while is better vs a for loop or vice versa.

I would be curious to know your professor's answer as to why he feels for loops should be taught first. Seems like you are moving backwards in that case, doesn't it?


sometimes teachers teach to the test, so to speak, and don't really teach students what's really happening. IIRC the intro class i had in college taught for loops first because it was easier for them. memorize this syntax and it'll just work, vs pay attention to your while condition, increment some variable, and understand what's really happening.

on a tangent, i visited my high school when i was a college student. i remember most of the math class in high school seemed to think induction could only be used to prove formulas like sum(1 to n) = n*(n+1) /2. the teacher never gave them any other kind of example.
Member
Posts: 11,183
Joined: Aug 23 2008
Gold: 1,890.00
Mar 7 2014 01:03am
The for loop is in preparation of the next assignment where I will be utilizing the k.

Although I'd like to say I planned for that. I chose for loop because It was simple to make an infinite loop out of.

As for the order of the way they were taugght. All loops were taught on the same day in the same chapter.

The order they were introduced in the chapter was;

for -> while -> do while


e/ sorry for the messy code. I understand what each loop is doing. and I know when to use a for/while loop over a do while loop, but I don't think I know when to use while over for and vice versa. the way I have been doing is:

use while when the test condition is based on user input

use for loop when the test condition is some kind of count.

This post was edited by kasey21 on Mar 7 2014 01:07am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 7 2014 01:13am
Quote (kasey21 @ Mar 7 2014 02:03am)

Although I'd like to say I planned for that. I chose for loop because It was simple to make an infinite loop out of.


how is this:
Code
for (k=1;; k++)


easier or more readable than this:
Code
while (true)


?
Member
Posts: 11,183
Joined: Aug 23 2008
Gold: 1,890.00
Mar 7 2014 01:18am
Quote (carteblanche @ Mar 7 2014 08:13am)
how is this:
Code
for (k=1;; k++)


easier or more readable than this:
Code
while (true)


?


never learned while (true) before.

book actually taught how to make infinite loops with the for. Never mentioned while(true) before.

so my ignorance made it more readable :P .
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 7 2014 01:24am
Quote (kasey21 @ Mar 7 2014 02:18am)
never learned while (true) before.

book actually taught how to make infinite loops with the for. Never mentioned while(true) before.

so my ignorance made it more readable :P .


the first looks like a mistake at best (Eg: forgot condition, forgot to use k), terrible code at worse. if you insist on using infinite for loop, drop the unused variable k altogether:

Code
for (;;)
Member
Posts: 11,183
Joined: Aug 23 2008
Gold: 1,890.00
Mar 7 2014 01:35am
Quote (carteblanche @ Mar 7 2014 08:24am)
the first looks like a mistake at best (Eg: forgot condition, forgot to use k), terrible code at worse. if you insist on using infinite for loop, drop the unused variable k altogether:

Code
for (;;)


Thanks.

was actually taught the for(;;)

Totally slipped my mind when writing. Will do that for this assignment then put the k back in for the next since Ill actually be using the count.


Just wondering. Is there a reason while(true) is preferred over for(;;) ?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 7 2014 01:41am
Quote (kasey21 @ Mar 7 2014 02:35am)
Just wondering. Is there a reason while(true) is preferred over for(;;) ?


Let me ask you. Suppose the "for" syntax used 20 params instead of just 3. which would you prefer?

Code
while (true)

or
Code
for (;;;;;;;;;;;;;;;;;;;)


now think about the reasons you used to come up with your answer, and ask yourself how/why does your answer change from 20 params to 3?

This post was edited by carteblanche on Mar 7 2014 01:47am
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 7 2014 04:13am
Quote (carteblanche @ Mar 7 2014 02:41am)
Let me ask you. Suppose the "for" syntax used 20 params instead of just 3. which would you prefer?

Code
while (true)

or
Code
for (;;;;;;;;;;;;;;;;;;;)


now think about the reasons you used to come up with your answer, and ask yourself how/why does your answer change from 20 params to 3?


I feel like you just Monty Hall'd that explanation. Well done lol.

For loops are called iterative loops. They are used when you want to iterate through a collection or count something. Keeping track of the current iteration is normally beneficial for them.

While/do while are conditional loops. They just loop while a condition is met. While is the pre test loop do while is the post test. Depending on when you want the condition checked is when you use them. The reason while(true) seems foreign to you stems from the typical delimma students have when it comes to Boolean expressions. They can't visualize that a Boolean expression evaluates to a Boolean. A lot of times you will see

Code
if(x == true)


In student code because they fail to grasp that since x is a Boolean you could just say

Code
if(x)
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll