# Thread: if loop only goes through once

1. ## if loop only goes through once

Hey guys! I'm writing a program that reads a starting time and an ending time for delivery that it normally takes. There is construction with a 25% delay on deliveries. The program must take the two times entered, incorporate the 25 percent delay and output the new ending time.
It reads times using a 24 hour time period (so 2345, 0615, etc.) converts the time to minutes by multiplying by .6. But if I try to divide by .6 to put it back into hour time, it goes to base 10 not base 60 so it comes up with times like 0697. So I made an if loop to figure out number of hours and then the left over put into minutes. Anyway, my problem is the if loop only goes through once. Any way you can see why it's doing this? (P.S. I'm pretty proficient in C++ and C# is a new language for a class in college, so I understand terms pretty well)

Code:
```using System;

class Program
{
static void Main(string[] args)
{
Double _startTimeMinutes, _endTimeMinutes, _totalTime, _timeAddedMinutes, _percent, _newEndTimeMinutes, _startTime, _endTime;
_percent = .25;
int _hour = 0;

Console.WriteLine("Please input the start time in 24 hour mode (no colon or decimals): ");
Console.WriteLine("Please input the end time in 24 hour mode (no colon or decimals): ");
_startTimeMinutes = _startTime * .6;
_endTimeMinutes = _endTime * .6;
_totalTime = _endTimeMinutes - _startTimeMinutes;

Double _newEndTime = _newEndTimeMinutes;
if (_newEndTime > 60)
{
_newEndTime = _newEndTimeMinutes - 60;
_hour++;
}
char _space = ' ';
if (_hour < 10)
{
_space = ' ';
}
else { _space = '0'; }
Console.WriteLine("New end time because of construction is: {0}{1}:{2}",_space, _hour, _newEndTime);

}
}```

2. There are no such things as if loops. There are while loops, do while, for loops, foreach loops.

3. I see no loop in the code. If you are as proficient in C++ as you claim then C# syntax and keywords should be a cinch for you. The C# loop constructs, save foreach, are all natively supported in C++. STL has a foreach although it is not nearly as simple to use as a C# foreach.

Another type of loop in C# is the enumerator or iterator loop. C++ has this as well but you use the iterators inside of while or for loops. C# actually has a type of iterator loop that will iterate over collections and it is a part of the language.

4. Yeah i saw that right after I posted haha I feel dumb sorry guys my mistake!

5. .NET has a lot of classes to help you do this kind of stuff. For instance:
Code:
```        static void Main(string[] args)
{
string input;
Console.WriteLine("Please input the start time in 24 hour mode (no colon or decimals): ");
DateTime startTime = DateTime.Parse(input.Substring(0, 2) + ":" + input.Substring(2, 2));

Console.WriteLine("Please input the end time in 24 hour mode (no colon or decimals): ");
DateTime endTime = DateTime.Parse(input.Substring(0, 2) + ":" + input.Substring(2, 2));

TimeSpan totalTime = endTime - startTime;
TimeSpan adjustedTotalTime = new TimeSpan((long)Math.Ceiling(totalTime.Ticks * 1.25));

Console.WriteLine("Original total time: " + totalTime.ToString());
Console.WriteLine("Total time with construction: " + adjustedTotalTime.ToString());
}```
Also, stuff like:
Code:
```       char _space = ' ';
if (_hour < 10)
{
_space = ' ';
}
else { _space = '0'; }```
...can be shortened to just:
Code:
`char _space = _hour < 10 ? ' ' : '0';`

6. I didn't understand a lot of the first code you posted, but I did under stand the ? : statement since that's in c++.
I did run into a problem using an actual loop though... its become an infinate loop and I don't know why...

Code:
``` double _newEndTime = 61;
for (_newEndTime = 61; _newEndTime > 60; _newEndTime = _newEndTimeMinutes - 60)
{
_hour++;
}```
before the for loop I used the while loop:

Code:
```double _newEndTime = 61;
while (_newEndTime > 60)
{
_newEndTime = _newEndTimeMinutes - 60;
_hour++;
}```

7. Originally Posted by treesap526
I didn't understand a lot of the first code you posted, but I did under stand the ? : statement since that's in c++.
I did run into a problem using an actual loop though... its become an infinate loop and I don't know why...

Code:
``` double _newEndTime = 61;
for (_newEndTime = 61; _newEndTime > 60; _newEndTime = _newEndTimeMinutes - 60)
{
_hour++;
}```
before the for loop I used the while loop:

Code:
```double _newEndTime = 61;
while (_newEndTime > 60)
{
_newEndTime = _newEndTimeMinutes - 60;
_hour++;
}```
When I say before the for loop I meant I've tried it both ways and cant get it to work

8. I think you're using the wrong variable or something. You have:
Code:
`_newEndTime = _newEndTimeMinutes - 60;`
But I think it should be:
Code:
`_newEndTime = _newEndTime - 60;`
This kind of problem can be avoided by using the -= operator (_newEndTime -= 60).

9. Because you can't ever change what happens inside your loop. Suppose _newEndTimeMinutes is 135. Then _newEndTime gets set to 75, and hour is incremented. We do it again: _newEndTime gets set to 75 (_newEndTimeMinutes didn't change, after all), and hour is incremented. We do the loop again: _newEndTime gets set to 75 (_newEndTimeMinutes didn't change, after all), and hour is incremented. We do the loop again....

10. Originally Posted by itsme86
I think you're using the wrong variable or something. You have:
Code:
`_newEndTime = _newEndTimeMinutes - 60;`
But I think it should be:
Code:
`_newEndTime = _newEndTime - 60;`
This kind of problem can be avoided by using the -= operator (_newEndTime -= 60).
If you look above at my original code, _newEndTimeMinutes is from taking the total time of the trip, taking 25 percent of that, adding that onto the end time so the trip takes longer. The variable _newEndTimeMinutes holds that end time (so say 6:15 pm) but it's in minutes and I'm trying to convert it to hours and minutes, but if i just divide by .6, it gives me like .97 when i want it to give me base 60 or what it's called.
TECHNICALLY I could do this instead, and this is what I mean, but i dont know if it works:
Code:
```while (_newEndTimeMinutes > 60)
{ _newEndTimeMinutes = _newEndTimeMinutes - 60;
_hour++
}
// so just take away 60 from the value and add one to the hour mark while //_newEndTimeMinutes is over 60```

11. Just save yourself headaches by using DateTime and TimeSpan like I showed:
Code:
```Please input the start time in 24 hour mode (no colon or decimals):
0125
Please input the end time in 24 hour mode (no colon or decimals):
0848
Original total time: 07:23:00
Total time with construction: 09:13:45```

12. Originally Posted by itsme86
Just save yourself headaches by using DateTime and TimeSpan like I showed:
Code:
```Please input the start time in 24 hour mode (no colon or decimals):
0125
Please input the end time in 24 hour mode (no colon or decimals):
0848
Original total time: 07:23:00
Total time with construction: 09:13:45```
I would if we had covered classes and objects and such. I'm in a major beginner level c# class and we only JUST learned about loops and so we have to use loops and variables and output and input (which is all we've learned) in order to write this program or we dont get the points. It's lame and holding me back, but i have to ughhh

13. wow. solved it. haha all I did was retype the while loop. I don't even know what I did different... but hey, it works!
for anyone else who had the same question and came here for answers, here was my final code:
Code:
```using System;

class Program
{
static void Main(string[] args)
{
Double _startTimeMinutes, _endTimeMinutes, _totalTime, _timeAddedMinutes, _percent, _newEndTimeMinutes, _startTime, _endTime;
_percent = .25;
int _hour = 0;

Console.WriteLine("Please input the start time in 24 hour mode (no colon or decimals): ");
Console.WriteLine("Please input the end time in 24 hour mode (no colon or decimals): ");
_startTimeMinutes = _startTime * .6;
_endTimeMinutes = _endTime * .6;
_totalTime = _endTimeMinutes - _startTimeMinutes;
while (_newEndTimeMinutes > 60)
{
_newEndTimeMinutes = _newEndTimeMinutes - 60;
_hour++;
}
char _space = ' ';
if (_hour > 10)
{
_space = ' ';
}
else { _space = '0'; }
Console.WriteLine("New end time because of construction is: {0}{1}:{2}",_space, _hour, _newEndTimeMinutes);

}
}```

14. Originally Posted by treesap526
but i dont know if it works:
This is really a very problematic statement. You're not in the business of creating mysteries for yourself -- you should be able to sit down with a few lines of code (especially when it doesn't have any "new" syntax), and work out whether the code will "work" (as in, do what you want to do) or not. If it does, then fine; if it doesn't, then seeing how it fails will often give a clue as to what to do instead. But there's no substitute for thinking about the problem; and the method for how to do the work isn't really very language-specific.

15. Originally Posted by tabstop
This is really a very problematic statement. You're not in the business of creating mysteries for yourself -- you should be able to sit down with a few lines of code (especially when it doesn't have any "new" syntax), and work out whether the code will "work" (as in, do what you want to do) or not. If it does, then fine; if it doesn't, then seeing how it fails will often give a clue as to what to do instead. But there's no substitute for thinking about the problem; and the method for how to do the work isn't really very language-specific.
Let me rephrase haha: I don't know if that's what is causing the problems, I haven't worked it out yet. But I did get it to work and that method did work. But for some reason when I tried it earlier it didn't work, even though to me it looked like it should. Hence the "dont know" part. But thanks for the philosophy, it hit home haha very nice tabstop!