Kal Kal

Kal Kal

  • NA
  • 10
  • 526

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");  
  2.   
  3.             string line = "";  
  4.   
  5.            while (line != null)  
  6.            {  
  7.               line = sr.ReadLine();  
  8.   
  9.                Console.WriteLine(line);  
  10.   
  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)