What are you looking for ?
Home » » Lesson-03

Lesson-03

{[['']]}

Control Statements - Loops


Create a console application "Patient"
namespace Patient
{
    class Program
    {
        static void Main(string[] args)
        {
         
  //Get Time
            Console.WriteLine("Time now is " +DateTime.Now.ToString("HH:mm:ss tt \n"));
           #region Get  a part of time "Secounds"
           int Sec;
           Sec = Int32.Parse(DateTime.Now.ToString("ss"));
           #endregion
}
  }
    }

The while Loop

A while loop will check a condition and then continues to execute a block of code as long as the condition evaluates to a boolean value of true. Its syntax is as follows: while (<boolean expression>) { <statements> }.

      #region while Loop
           while (Sec < 50)
           {
               Console.WriteLine("Your number is {0} \n ", Sec);
               Sec++;
               Console.WriteLine("Your will be the next in  {0} Sec \n", Sec);
               break;
           }
           #endregion

The do Loop

A do loop is similar to the while loop, except that it checks its condition at the end of the loop. This means that the do loop is guaranteed to execute at least one time. On the other hand, a while loop evaluates its Boolean expression at the beginning and there is generally no guarantee that the statements inside the loop will be executed, unless you program the code to explicitly do so.

           #region do Loop-1
           do
           {
               Console.WriteLine("No {0} You are Next \n", Sec);
           }
           while (Sec < 1);
           {
               Sec++;
           }
           #endregion
#region Do Loop-2
string visitChoise;
int visitId;
Console.WriteLine("Is This Your First Visit ? \n For Yes press 1 For No press 2 \n");
do
{
Console.WriteLine("Thanks\n");
visitChoise = Console.ReadLine();
visitId = Int32.Parse(visitChoise);
switch (visitId)
{
case 1:
Console.WriteLine("Thanks for your Visit, Your No is {0}", Sec );
break;

case 2:
Console.WriteLine("Please Enter Your Visit No");
visitId++;
string episode ;
episode= Console.ReadLine();
int oldEpisode = Int32.Parse(episode);
Console.WriteLine("Thanks for your Visit, Your No is {0}", Sec+oldEpisode);
break;

default:
Console.WriteLine("Please Enter a valid No");
break;
}
} while (visitId ==2 || visitId ==1);
#endregion
Share this article :

Post a Comment

Flag Counter

Social Profile

 
Copyright x 2011. By Wael Medhat - All Rights Reserved