Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie: for loop question

Status
Not open for further replies.

psandekian

Programmer
Oct 12, 2000
48
0
0
US
Hi,

I'm new to C and I'm reviewing some code that's written in C in a UNIX environment. I was wondering if someone might be able to explain this statement:
Code:
for (y = 1970, Y += (Y < 70) ? 2000 : 1900; y < Y; y++)
I understand the general principles of the for loop.

for y = 1970 while y < Y increment y+1

But I do not understand the middle part.

I hope someone can help.
Thanks.
Patty
 
The following is from help in the C compiler and seems to explain it pretty well....

Syntax

logical-OR-expr ? expr : conditional-expr

Remarks

The conditional operator ?: is a ternary operator.
In the expression E1 ? E2 : E3, E1 evaluates first. If its value is nonzero (true), then E2 evaluates and E3 is ignored. If E1 evaluates to zero(false), then E3 evaluates and E2 is ignored.
The result of E1 ? E2 : E3 will be the value of either E2 or E3 depending upon which evaluates.
E1 must be a scalar expression. E2 and E3 must obey one of the following rules:

1.Both of arithmetic type. E2 and E3 are subject to the usual arithmetic conversions, which determines the resulting type.
2.Both of compatible structure or union types. The resulting type is the structure or union type of E2 and E3.
3.Both of void type. The resulting type is void.
4.Both of type pointer to qualified or unqualified versions of compatible types. The resulting type is a pointer to a type qualified with all the type qualifiers of the types pointed to by both operands.
5.One operand is a pointer, and the other is a null pointer constant. The resulting type is a pointer to a type qualified with all the type qualifiers of the types pointed to by both operands.
6.One operand is a pointer to an object or incomplete type, and the other is a pointer to a qualified or unqualified version of void. The resulting type is that of the non-pointer-to-void operand. Michelle Fulton

 
The &quot;middle part&quot; means: add 2000 to Y if Y is less than 70, otherwise add 1900 to it.

Although this is just a snippet of the code, the purpose of this is probably to implement date windowing, where all dates (expressed in terms of the last 2 digits of the year) that are between 70 and 99 inclusive are treated as 1970-1999, otherwise they belong to the 21st century and later.

HTH,

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top