while 1 and fget1 loop.

52 vues (au cours des 30 derniers jours)
sermet
sermet le 22 Jan 2015
fide = fopen('data.13o')
head_lines = 0
while 1
head_lines = head_lines+1
line = fgetl(fide)
answer = findstr(line,'END OF HEADER')
if ~isempty(answer), break; end
end
% the header of the data.13o file is;
OBSERVATION DATA M RINEX VERSION / TYPE
LEICA GEO OFFICE 5.0 10-5-13 10:45 PGM / RUN BY / DATE
OBSERVER / AGENCY
O2930689 MARKER NAME
O2930689 MARKER NUMBER
P8UO0W21QTC -Unknown- -Unknown- REC # / TYPE / VERS
-Unknown- TPSGR3 NONE ANT # / TYPE
4303634.7047 2746069.9471 3812585.4149 APPROX POSITION XYZ
1.3289 0.0000 0.0000 ANTENNA: DELTA H/E/N
L1PhaOff: 0.2338 L2PhaOff: 0.2252 COMMENT
1 1 WAVELENGTH FACT L1/2
7 C1 P1 L1 D1 P2 L2 D2 # / TYPES OF OBSERV
2013 4 13 4 56 30.000000 TIME OF FIRST OBS
2013 4 13 7 59 0.000000 TIME OF LAST OBS
16 LEAP SECONDS
27 # OF SATELLITES
END OF HEADER
%this loop works what it supposed to be but I couldn't understand how "while 1" works for this loop and what's the difference between "while 1" and while for the loop.

Réponse acceptée

Guillaume
Guillaume le 22 Jan 2015
while 1 is the same as while true. It means loop forever. The only way to stop the loop is to use a break statement, which is what you're doing with the
if ~isempty(answer), break; end
Another way to write the loop would have been:
answer = '~'; %anything not empty so the loop starts
while ~isempty(answer)
head_lines = head_lines+1
line = fgetl(fide)
answer = findstr(line,'END OF HEADER')
end
  3 commentaires
Guillaume
Guillaume le 11 Sep 2017
@sanjeet singh
Clearly, you haven't understand what while constant do. There are only two possibilities:
while 0 which is equivalent to while false. The while loop will never execute and,
while any_number_not_0, which is equivalent to while true, the loop will execute forever unless stop with a break. while 1, while 2, while pi, while inf, while 5e10 are all the same.
Ozan Akyildiz
Ozan Akyildiz le 12 Fév 2019
@Guillame, I think stating that 0 resolves to false and any other number (or char) will resolve to true is enough. No need to berate.

Connectez-vous pour commenter.

Plus de réponses (1)

d bhaskar
d bhaskar le 17 Fév 2023
a=1;
while 1
a=a+1;
if(a>100) break
end
end

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by