Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

How to work with the symbol EndOfFile? I want to use the EndOfFile symbol to determine when my control structure reaches the end of a file.

Case 1: suppose we reach the end of file while continuously reading numbers from it. Which correctly returns "end".

num=EndOfFile;
Which[
  num==EndOfFile, res="end",
  num==1,         res="1",
  num==2,         res="2",
  True,           res="non"
]

(* ==> "end" *)

Case 2: suppose we read a 3 (that is, num=3): in this case Which returns unevaluated.

share|improve this question
1  
When you compare nonnumerical values, you should always use SameQ (===) to test for structural identity instead of numerical equation (Equal = ==). EndOfFile does not have a numerical value thus if num is 1, 1 == EndOfFile returns unevaluated. –  István Zachar Feb 27 '13 at 3:21
add comment

marked as duplicate by rm -rf Mar 7 '13 at 15:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

Which[num === EndOfFile, res = "end", 
      num == 1,          res = "1", 
      num == 2,          res = "2", 
      True,              res = "non"]

Edit

Perhaps you may want to experiment a little with SameQ[]. For example:

ClearAll[h, j]
h == j
h === j
(*
 h == j
 False
*)
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.