Part 14 – C# Tutorial – do while loop in c#

40
1



Text version of the video

Slides

All C# Text Articles

All C# Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In this part of the c sharp tutorial we will learn about
1. do while loop
2. difference between while and do while loop

Nguồn:https://wijstaanvooronzegrondrechten.org/

40 COMMENTS

  1. Thank you Venkat.
    I have learned so much from watching your videos.
    Can you make a video on string APIs please.
    It would be greatly appreciated.
    Thanks 🙏

  2. using System;
    class Program
    {
    static void Main()
    {
    Int32 totalcoffecost = 0;
    int userchoice = 0;
    string userdesicion = "";
    do
    {
    do
    {
    Console.WriteLine("Please Choose Coffe size: 1 – small, 2 – medium, 3 – large");
    userchoice = Int32.Parse(Console.ReadLine());
    switch (userchoice)
    {
    case 1:
    totalcoffecost += 1;
    break;
    case 2:
    totalcoffecost += 2;
    break;
    case 3:
    totalcoffecost += 3;
    break;
    default:
    Console.WriteLine("Please Choose Valid Size");
    break;
    }
    } while (userchoice != 1 && userchoice != 2 && userchoice != 3);
    do
    {
    Console.WriteLine("Do You Want To Contionue shoping..");
    Console.WriteLine("Please Choose "Y" for Yes "N" for No");
    userdesicion = Console.ReadLine().ToUpper();
    if (userdesicion != "Y" && userdesicion != "N")
    {
    Console.WriteLine("Please Choose Valid Input");
    }
    } while (userdesicion != "Y" && userdesicion != "N");
    } while ( userdesicion == "Y");

    Console.WriteLine("Your Billing Amount is {0}", totalcoffecost);
    Console.WriteLine("Thanking For Purchasing With Us");
    }
    }

  3. It may be me, but I find the GOTO statement more cleaner then nested do-while loop. In a more complex program if you have dozens of do whiles, I feel like getting lost in t he nested loops. But then again I heard so many times to avoid the GOTO :(. But, I will try to follow it. Btw, I have another option in presenting the Yes or NO… I want for lazy people's who really need coffee to also type in Y in stead of YES. so in the Do While loop the condition has to be something like
    while( (Userchoice !==YES || Y) && (Userchoice!==No || N) ) is this the way to go?

  4. Code of Do while loop Coffe :
    using System;

    namespace dowhile
    {
    class Program
    {
    static void Main(string[] args)
    {
    int userchoice = -1;
    int totalbill = 0 ;
    string userdec = string.Empty;

    do
    {
    do
    {
    Console.WriteLine("Enter your choice 1-Cofee 2-Chai 3-softdrink");
    userchoice = int.Parse(Console.ReadLine());

    switch (userchoice)
    {
    case 1:
    totalbill += 1;
    Console.WriteLine("Thankyou Your Bill is {0}", totalbill);
    break;
    case 2:
    totalbill += 2;
    Console.WriteLine("Thankyou Your Bill is {0}", totalbill);
    break;
    case 3:
    totalbill += 3;
    Console.WriteLine("Thankyou Your Bill is {0}", totalbill);
    break;
    default:
    Console.WriteLine("Invalid Choice");
    break;

    }
    } while (userchoice != 1 && userchoice != 2 && userchoice != 3);
    do
    {
    Console.WriteLine("Do you want to continue and buy again");
    userdec = Console.ReadLine().ToLower();

    if (userdec != "yes" && userdec != "no")
    {

    Console.WriteLine("Invalid key Try again");
    }

    } while (userdec.ToLower() != "yes" && userdec.ToLower() != "no");

    } while (userdec.ToLower() == "yes");
    Console.WriteLine("Thankyou your total bill is {0}", totalbill);

    }
    }
    }

  5. i have a small dought
    as you mention in
    if (userchoice != "YES " && userchoice !="NO")

    in this statement, if you give NO then is a program going to move forward? like if you mention && the program will going to check only first condition but not next.

    as & mentioning will go?

  6. Where can i view the Do While coffee code? Its hard to follow when paused. I would like to download it and compare it in visual studio.

  7. First of all, thank you very much for this great tutorial series! The videos are greatly structured and really easy to follow though I hope the audio will somewhat improve in future videos!

    I have some questions regarding the do while loop for the coffee program:
    1.
    Whenever I enter symbols other than numbers I receive a FormatException Error pointing at int.Parse(Console.ReadLine());
    I'm usung Visual Studio 2015 – did windows change anything there or am I missing something else?

    2.
    The question has been raised a couple of times already but I didn't find any explanation for it yet:
    Why is int UserChoice = -1;

    Thanks in advance for your help!

  8. When I run this on the first attempt it works fine, but when I input some garbage it's started giving me errors after giving correct input on "Do you want to buy Another coffee Yes Or No". When I press yes, it asks me for input again

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    namespace Practicing
    {
    class Program
    {
    static void Main(string[] args)
    {
    try
    {
    int TotalCoffieCost = 0;
    string UserDecision = string.Empty;
    do
    {
    int UserChoice = -1;
    do
    {
    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine("Please Enter Your Choice: 1-Small,2-Medium,3-Large");
    UserChoice = int.Parse(Console.ReadLine());
    switch (UserChoice)
    {
    case 1:
    TotalCoffieCost += 50;
    break;
    case 2:
    TotalCoffieCost += 70;
    break;
    case 3:
    TotalCoffieCost += 100;
    break;
    default:
    Console.WriteLine("Your choise {0} Is Invalid", UserChoice);
    break;
    }
    } while (UserChoice != 1 && UserChoice != 2 && UserChoice != 3);
    do
    {
    Console.WriteLine("Do You want to buy another coffie Yes or No");
    UserDecision = Console.ReadLine().ToUpper();
    if (UserDecision != "YES" && UserDecision != "NO") ;
    {
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("Your Choise {0} is invalid ", UserDecision);
    Console.ForegroundColor = ConsoleColor.White;
    }
    }
    while (UserDecision != "YES" && UserDecision != "NO");
    }
    while (UserDecision.ToUpper() != "NO");
    Console.WriteLine("ThankYou For shopping With Us n Your Bill Is Genrating Please Wait");
    Thread.Sleep(3000);
    Console.ForegroundColor = ConsoleColor.Magenta;
    Console.WriteLine("Your bill {0}", TotalCoffieCost);
    }
    catch (Exception e)
    {
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine(e.Message);
    }
    }

    }
    }

  9. First of all, I would say that you r really remarkable teacher,I use your videos to improve myself after University lessons,and I have question, why u always use Parse instead of converting toint32 and what is the basic various converting to parse and toint 32?

  10. thanks what you teach I make it like this:

    public void Do_While_loop ()
    {
    //do while make at leat the first time what you ask to the program
    bool option = false;
    int num = 0;

    do {

    //this will add 1 every time you repeat the action
    num = num + 1;
    Console.WriteLine (num);

    //conditional for keep going or quit
    Console.WriteLine ("again? press: yes or press any key for quit");
    string answer = Console.ReadLine ().ToUpper ();
    option = answer == "YES" ? true : false;

    } while (option == true);

    }

  11. thank you sir …u made csharp so much interesting….ur a great teacher bt I wonder who would b your teacher who molded u to sch a great programmer….god bless u with loads of happiness

  12. There are few inspiring people in the world after my parents your are the one that inspired me to something great thank you very much for your wonderful videos. I will never get bore listening you.You are amazing teacher.

  13. Sometimes the goto statement make it simpler than a lot of do while nested more in a big program, in this case I prefer to use the goto statement:

    start:

    Console.WriteLine("Enter a target: ");
    int target = 0;
    int.TryParse(Console.ReadLine(), out target);
    int start = 0;
    do
    {
    Console.WriteLine(start);
    start += 1;

    } while (start <= target);

    end:
    Console.WriteLine("Do you wanna try again?");
    string UserChoice = Console.ReadLine();
    switch (UserChoice.ToUpperInvariant())
    {
    case "YES" :
    goto start;
    case "NO" :
    break;

    default:
    Console.WriteLine("You enter an invalid answer, try again");
    goto end;
    }

  14. Thank you for sharing these videos with us. You are greatly appreciated by all who encounter your channel and want to learn .NET and C#
    God bless you!

  15. Easy to understand Indian assent…. 😀
    Download this video, install the gom player, listen over there with extend volume for more clearly.

  16. yeah well, if you write letter in first question, it bugs cuz it expect an integer..
    is there any way to say " if UserTarget is not an integer – do this ?"

    like if (usertarget != int)
    {
    Console.WriteLine("Invalid target");
    }
    but it doesnt work like that

LEAVE A REPLY

Please enter your comment!
Please enter your name here