correctness

Opinion on "loop invariants", and are these frequently used in the industry?

I was thinking back to my freshman year at college (five years ago) when I took an exam to place-out of intro-level computer science. There was a question about loop invariants, and I was wondering if loop invariants are really necessary in this case or if the question was simply a bad example... the question was to write an iterative de...

What is the best way using multiple lines of <% %> Tag or <% %> Tag with multiple lines?

Sorry if the title is not enough to understand what i am asking about. I am rails developer and i used multiple lines of <% %> in my views but now i realized that it's not best practice so i came here and like to you all excellent guys what is the correct way in ROR? For example if i required to something like following <% user =User....

How does Java handle integer underflows and overflows and how would you check for it?

How does Java handle integer underflows and overflows? Leading on from that, how would you check/test that this is occurring? ...

switch statement with returns -- code correctness

Hi, let's say I have code in C with approximately this structure: switch (something) { case 0: return "blah"; break; case 1: case 4: return "foo"; break; case 2: case 3: return "bar"; break; default: return "foobar"; break; } Now obviously, the "break"s are not necessary for the code to run correctly, but it sort of...

Standard C functions: Check for -1 or 0?

Many standard C and POSIX functions return -1 for error, and 0 on success, for example truncate, fflush, msync, etc. int ret = truncate("/some/file", 42); Is it better practice to check for success with ret != -1 or ret == 0, and why? My Thoughts It's my experience most people check for the error case (ret != -1), as there is typica...

Two different Output...

#include<stdio.h> int main(void) { static int i=i++, j=j++, k=k++; printf("i = %d j = %d k = %d", i, j, k); return 0; } Output in Turbo C 4.5 : i = 0 j = 0 k = 0 In gcc I'm getting the error: Initializer element is not constant Which one is logically correct ? I'm in bit confusion.. ...