## 12 replies

Last post Nov 10, 2014 12:35 AM by Paul Linton

Participant

1347 Points

2439 Posts

### Need a specific logic for handling situation

<p>i have dropdown which is populated with minutes having interval of 5 like 05,10,15,20,25 upto 55</p>
<p>i need to pickup current minute and if current minute is more than 1 then 5 will be selected in dropdown</p>
<p>if current minute is more than 5 then 10 will be selected in dropdown</p>
<p>if current minute is more than 10 then 15 will be selected in dropdown</p>
<p>if if current minute is more than 50 then 55 will be selected in dropdown</p>
<p>without writing too many if else how could i achieve it. share the idea. thanks</p>

Star

14297 Points

5882 Posts

### Re: Need a specific logic for handling situation

@mou_inn      do not forget minute zero;  minutes range is 0:59

along with if statements, i'd use a c# switch statement.  http://msdn.microsoft.com/en-us/library/06tc147t.aspx

if minutes < 5 choice = 5; // for zero to 4
else if ...
else if ...
// ...
else choice = 60;

switch(choice)
{
case 5:
// ...
break;
case 10:
// ...
break;

// ...

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it

Participant

1347 Points

2439 Posts

### Re: Need a specific logic for handling situation

i want to achieve it without using if else or switch statement.

• ### Mikesdotnett...

All-Star

184243 Points

26625 Posts

Moderator

MVP

### Re: Need a specific logic for handling situation

Do this then:

var minutes = DateTime.Now.Minute;
var selectedValue = minutes % 5 == 0 ? minutes : ((minutes / 5) + 1) * 5;
Console.WriteLine(selectedValue);

Or even shorter:

var selectedValue = (int)Math.Ceiling((double)DateTime.Now.Minute / 5) * 5;

All-Star

50490 Points

14820 Posts

### Re: Need a specific logic for handling situation

Try something like this :
ddl1.SelectedIndex=ConvertToInt
32(minute/5) or use math.floor or
ceiling.
Programming to simplify, don't look for hardway ...

Star

14297 Points

5882 Posts

### Re: Need a specific logic for handling situation

@Mikesdotnett...  actually, Mike, your solution has a flaw despite its apparent elegance.

Here's a better solution imho using only integer arithmetic:

for (int i = 0; i < 60; i++)
{
Console.WriteLine ("{0} {1}", ((((i)/5)+1)*5), i);
}
((((i)/5)+1)*5) // produces NOT the desired result BUT a useful result.

compare:

for (int i = 0; i < 60; i++)
{
Console.WriteLine ((int)Math.Ceiling((double)i / 5) * 5);
}
(int)Math.Ceiling((double)i / 5) * 5  // fails for DateTime.Now.Minute == zero

edit:

my solution is also flawed in that 0 to 4 are 5 ... et cetera.

end edit.

edit2:

zero obviously belongs in the first group, but to include zero in the first group, 0..5 causes the first group to have 6 items and the last group to have only 4 items, when the ideal solution has 5 items in each of 12 groups.

end edit2.

edit3:

given that there are edge cases, to date, it is possible for me to minimize the use of if:

Int32 j;
for (int i = 0; i < 60; i++)
{
if (i != 0)
{
if (i%5 == 0) j = (i/5)*5;
else          j = (((i)/5)+1)*5;
}
else j = 5; // special range 0 to 5
Console.WriteLine ("{0} \t {1}", i, j);
}

output:

i     j
0     5   // zero is treated as special ... first group has 6 items
1     5
2     5
3     5
4     5
5     5

6    10
7    10
8    10
9    10
10    10

11    15
12    15
13    15
14    15
15    15

...

51    55
52    55
53    55
54    55
55    55

56    60  // only 4 items in the last group
57    60
58    60
59    60

The above solution, while not perfect, uses only integer arithmetic and deals with the special case of zero.

end edit3.

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• ### Mikesdotnett...

All-Star

184243 Points

26625 Posts

Moderator

MVP

### Re: Need a specific logic for handling situation

#### gerrylowry

Mike, your solution has a flaw

That depends on the options available in the select list.  However, assuming that the options range from 0 to 55 in increments of 5, the following will work without an (explicit) if:

var selectedValue = (int)Math.Ceiling((double)DateTime.Now.Minute / 5) * 5 < 60 ?
(int)Math.Ceiling((double)DateTime.Now.Minute / 5) * 5 :
((int)Math.Ceiling((double)DateTime.Now.Minute / 5) * 5)-60;

Star

9555 Points

2785 Posts

### Re: Need a specific logic for handling situation

Nov 09, 2014 06:33 PM|Paul Linton|LINK

Since you don't say what you want done with 0,1, 56, 57, 58, or 59 (or, for that matter, 60, 61, ... or -1, -2, ...) - this will satisfy your request as stated.

int roundedMinute = ((minute+4)/5)*5;

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.

Star

9555 Points

2785 Posts

### Re: Need a specific logic for handling situation

Nov 09, 2014 06:37 PM|Paul Linton|LINK

My bad.  I misread your spec - you did say what should happen with 60, 61, ...

here is a modified answer which meets that requirement

int roundedMinute = ((Math.Min(54, minute)+4)/5)*5;

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.

Star

14297 Points

5882 Posts

### Re: Need a specific logic for handling situation

#### Paul Linton

My bad.  I misread your spec - you did say what should happen with 60, 61, ...

60 and up are not possible because we are dealing with minutes; on the good side, you've eliminated if and switch however you've sacrifice accuracy for the fringe conditions.

for (Int32 minute = 0; minute < 60; minute++)
{
Int32 roundedMinute = ((Math.Min(54, minute)+4)/5)*5;
Console.WriteLine ("minute {0, 2}  \t roundedMinute {1, 2}", minute, roundedMinute);
}

0 and 56 to 59 produce erroneous results:

minute  0     roundedMinute  0

minute 56     roundedMinute 55
minute 57     roundedMinute 55
minute 58     roundedMinute 55
minute 59     roundedMinute 55

accuracy improves, expect for 0, if we change 54 to 59:

Int32 roundedMinute = ((Math.Min(59, minute)+4)/5)*5;

of course Math.Min is not required:

Int32 roundedMinute = ((minute+4)/5)*5;

now to take care of zero:

Int32 roundedMinute = minute != 0 ? ((minute+4)/5)*5 : 5;

so we end having to use a single if disguised in the behaviour of the conditional operator ?: http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

edit:

Mike, your code fails for 0 and 56 to 59:

for (Int32 minute = 0; minute < 60; minute++)
{
var selectedValue = (int)Math.Ceiling((double)minute / 5) * 5 < 60 ?
(int)Math.Ceiling((double)minute / 5) * 5 :
((int)Math.Ceiling((double)minute / 5) * 5)-60;
Console.WriteLine ("minute {0, 2}  \t selectedValue {1, 2}", minute, selectedValue);
}
minute  0     selectedValue  0

minute 56     selectedValue  0
minute 57     selectedValue  0
minute 58     selectedValue  0
minute 59     selectedValue  0

end edit.

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it

Star

9555 Points

2785 Posts

### Re: Need a specific logic for handling situation

Nov 09, 2014 11:08 PM|Paul Linton|LINK

There is no spec for zero.  For large numbers the spec says the result should be 55 ('If current minute is more than 50 then 55 will be selected in the dropdown").   I see nowhere in the spec where it says values greater than 60 are not possible.

Which element in the spec did my code need it's 'accuracy' improved for?  (Note that the spec specifically says that 55 should be returned for anything greater than 55, exactly as my code does)

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.

Star

14297 Points

5882 Posts

### Re: Need a specific logic for handling situation

#### Paul Linton

There is no spec for zero.  For large numbers the spec says the result should be 55 ('If current minute is more than 50 then 55 will be selected in the dropdown").   I see nowhere in the spec where it says values greater than 60 are not possible.

Paul, you\re a very professional programmer, as such, you do not only read the spec, you interpret it.   Let me help you.

#### mou_inn

have dropdown which is populated with minutes

http://msdn.microsoft.com/en-us/library/system.datetime.minute(v=vs.110).aspx "DateTime.Minute Property"

"The minute component, expressed as a value between 0 and 59."

#### mou_inn

having interval of 5 like 05,10,15,20,25 upto 55

Note:  the O.P. has written "like" rather than an alternative word such as "only".

#### Paul Linton

Which element in the spec did my code need it's 'accuracy' improved for?

0, 56, 57, 58, and 59

#### Paul Linton

(Note that the spec specifically says that 55 should be returned for anything greater than 55,

Paul, read it again here:  http://forums.asp.net/post/5806136.aspx ~~ the O.P. fails to address the values 0, 56, 57, 58, and 59.

Paul, you have misinterpreted the O.P.'s obvious intention expressed as "if current minute is more than 50 then 55 will be selected in dropdown" because your took it out of context.

FWIW, the O.P.s statements lack preciseness, hence common sense needs to prevail.

#### mou_inn

... if current minute is more than   1 then   5 will be selected in dropdown
if current minute is more than   5 then 10 will be selected in dropdown
if current minute is more than 10 then 15 will be selected in dropdown

.
.
.

if if current minute is more than 50 then 55 will be selected in dropdown

Paul, if you were to inspect the O.P.'s faulty specification more carefully, you'll notice the (a) the O.P. did not tell you what to do for minute == 1 ... , and (b) that since the values 2, 3, 4, ... , 57, 58, 59 are all more than 1, you could satisfied the O.P.s specification by using 5 for any value > 1 ... you did not do that because you applied a degree of common sense.

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it

Star

9555 Points

2785 Posts

### Re: Need a specific logic for handling situation

Nov 10, 2014 12:35 AM|Paul Linton|LINK

There is no need to get personal.  I am not a professional programmer.  Never have been, never will be.

I waited on hold for the tax office this morning for 74 minutes.  I don't care what MSDN says, it is very possible to have more than 60 minutes.  Where did the OP say they were talking about the DateTime.Minute property?  Please don't invent things.

The OP specifically excluded 0 and 1.  I refuse to attempt to put words into the mouth of the OP.

The OP specifically lumped everything greater than 55 into the final category.  I refuse to tell them that what they asked for is wrong.

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.