Robots.txt file is used to provide indexing instructions to search engine crawlers. It is always placed in the top-level directory of the server and this file is always checked in the first place whenever a search engine crawler visits the website in order to index it. So, you can use robots.txt file to prevent search engines from indexing some sections of your website. You should also remember that anyone can see your robots.txt file and so, you should not use it to hide the content on your website.
In short, if you want to prevent search engines from indexing some sections of your website then you should use robots.txt file.
