Kal Kal

Kal Kal

  • 1.6k
  • 7
  • 152

What is the logic behind while loop behavior in this case?

Jan 24 2018 2:20 AM
My code simply Creates a refrence variable of StreamReader object that reads a TextFile, After that using a while loop to retrive data from the TextFile. 
  1. StreamReader sr = new StreamReader("File.txt");  
  3.             string line = "";  
  5.            while (line != null)  
  6.            {  
  7.               line = sr.ReadLine();  
  9.                Console.WriteLine(line);  
  11.            }  
So, forgive my innocent question : The condition in the While loop is TRUE, so why it doesn't return an infinite loop ? instead it retrives the data from "File.txt" then it breaks the loop?
All what I know as a newbie programmer is that if the condition of the loop is TRUE keep it going until it turns to FALSE then the loop will break; itself.
Could somebody explain the logic behind this scenario please!

Answers (2)