Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Using ternary and summing array
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Ben Bacarisse, post: 4393209"] Yes, but it won't be any clearer. You don't use total. It will just confuse people. You can write sum += (i % 3 == 0 || i % 5 == 0) ? i : 0; Since logical expressions are either 0 or 1, you can also write sum += (i % 3 == 0 || i % 5 == 0) * i; but both are, in my opinion, inferior to a simple 'if'. What would make it clearer is to use a 'for' loop and to avoid having "global" (technically file scope) variales. Simple counted loops are best expressed using 'for'. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Using ternary and summing array
Top