Use do while loop to read and check password - CSharp Language Basics

CSharp examples for Language Basics:do while

Description

Use do while loop to read and check password

Demo Code

using System;/* w  ww  .j av a 2  s. c o m*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
   static void Main(string[] args)
   {
      string correctPassword = "friend";
      bool ok; // the variable must be declared outside of the loop!
      do
      {
         // Input
         Console.Write("Enter password: ");
         string enteredPassword = Console.ReadLine();
         // Evaluating
         ok = enteredPassword == correctPassword;
      } while (!ok); // loop repeats when the condition holds
      Console.WriteLine("Come inside, please...");
   }
}

Result


Related Tutorials