emmi
Posts: 42
Joined: Fri Jan 26, 2018 10:07 am

while loop does not stop [solved]

Mon Mar 05, 2018 6:22 am

Hello,

I put the following loop into my code:

Code: Select all

while((tempAbs = -1000) || (tempAbs = 1000))
{
					
	tempAbs = measure_temp(0);
					
}
The temperature is set to 1000 or -1000 when the temperature is outside the data sheet range of the thermoelement (above 100 or below 0 degrees Celsius respectively). So I those cases I want the temperature to be measured again as in most cases those results will be a measuring mistake.
Now I get absolutely realistic values of around 20 degrees Celsius. However, the loop measures the temperature again and again although the tempAbs is neither 1000 nor -1000.
Can someone spot the mistake? Thanks!
Last edited by emmi on Mon Mar 05, 2018 6:41 am, edited 1 time in total.

piense
Posts: 27
Joined: Thu Jun 09, 2016 4:11 am

Re: while loop does not stop

Mon Mar 05, 2018 6:34 am

use ==

emmi
Posts: 42
Joined: Fri Jan 26, 2018 10:07 am

Re: while loop does not stop

Mon Mar 05, 2018 6:37 am

piense wrote:use ==
Right. One of these easy things you don't see anymore when you have looked for too long on your own code. Thanks!

n67
Posts: 699
Joined: Mon Oct 30, 2017 4:55 pm

Re: while loop does not stop

Mon Mar 05, 2018 9:35 am

emmi wrote:
Mon Mar 05, 2018 6:37 am
piense wrote:use ==
Right. One of these easy things you don't see anymore when you have looked for too long on your own code. Thanks!
The compiler (presumably, gcc) should have caught this for you.

And it would have, had you compiled with the usual warnings flags:

-W -Wall -Werror
"L'enfer, c'est les autres"

jahboater
Posts: 2728
Joined: Wed Feb 04, 2015 6:38 pm

Re: while loop does not stop [solved]

Mon Mar 05, 2018 9:55 am

A common (but horrid) idiom is:

while((-1000 == tempAbs) || (1000 == tempAbs))

It doesn't read well though, so as n67 points out, turn on the compiler warnings - always.

I use :) :)

Code: Select all

WARN = -Wfatal-errors -Wall -Wextra -Wconversion -Wunused -Wundef -Wcast-qual \
       -Wredundant-decls -Wunreachable-code -Wwrite-strings -Warray-bounds \
       -Wstrict-aliasing=3 -Wstrict-overflow=1 -Wstrict-prototypes -Winline \
       -Wshadow -Wswitch -Wmissing-include-dirs -Woverlength-strings -Wpacked \
       -Wdisabled-optimization -Wmissing-prototypes -Wformat=2 -Winit-self \
       -Wmissing-declarations -Wunused-parameter -Wlogical-op -Wuninitialized \
       -Wnested-externs -Wpointer-arith -Wdouble-promotion -Wunused-macros \
       -Wunused-function -Wunsafe-loop-optimizations -Wnull-dereference \
       -Wduplicated-cond -Wshift-overflow=2 -Wnonnull -Wcast-align -Warray-bounds=2
of which -Wall -Wextra -Wconversion are the most useful IHMO.

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 9 guests