Learn How To Program In C# Part 6 – While Loop


Hey, everybody. Today we’ll be learning about the looping
statements. There are different kinds of looping statements. However, in this video, we’ll be going over
the while loop. A loop is a statement or a series of statements
that you want to repeat some number of times. It can be only one time or n number of times. Usually, a loop is run until a certain condition
is met. Let’s create a project so I can show you
what I mean. It’s better to explain an example. Let’s call this LoopingWhile. This is our Main method where the execution
of our program begins. Let’s write our first while loop. while ()
{ }
Let’s check out the structure. while is a reserved keyword. Then you have these parentheses and you have
a block. When I hover over the cursor between the parentheses,
it says, “Expression expected.” This is not just an expression. It’s a Boolean expression. As you know from the previous tutorials, Boolean
expression is an expression that evaluates down to a Boolean value. And a Boolean value is either true or false. So while the expression between these parentheses
evaluates to true, code that is in this block will get executed. Once the condition here evaluates to false,
then this will no longer be executed, and the program will just keep on going with the
rest of the statements. Let’s go ahead and create a variable. int index=0;
And we can say while (index

8 Replies to “Learn How To Program In C# Part 6 – While Loop”

  1. I'm completely new to learning code and I've been finding these videos really helpful! I wasn't sure how you were "Step" ing through your code until after I'd completed this exercise, and I wanted to see an output line for line in the console. I was rearranging the order of some conditional blocks to see how the output / flow would change.

    I'm trying to push myself a little bit after each exercise. I'm so new I don't trust myself to know exactly what my code is doing so I thought I'd try to make the output show it a little better, and to use some stuff I've learned from your videos thus far. I've learned more about coding in one night via your videos than any other attempts combined.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace LoopingWhile
    {
    class Program
    {
    static void Main(string[] args)
    {
    int index = 0;

    while (index < 10)
    {
    Console.WriteLine("The number is {0}", index);
    index = index + 1;

    Console.WriteLine("Program Running.");
    }

    if (index.Equals(10))
    {
    Console.WriteLine("The final number is {0}", index);
    }

    switch (index)
    {
    case 10:
    Console.WriteLine("Program End.");
    Console.WriteLine("");
    break;
    }
    }
    }
    }

  2. Been having lots of fun with your tutorials so far, and I think im gonna grab your course from Udemy. I just have one question, though. What does {0} do? I mean I kind of understand it, is it like a placeholder of some sort? Would appreciate a brief explanation. Thank you so much, again your tutorials are the best!

  3. What does the word "index" stand for and were does it come from? Also can the "index" equal to something other than "0"? And when you do "index = index + 1" can it be another number instead of "1" i guessing it can right? Thanks

  4. I have done exactly what you did but no numbers show up in the terminal. All that is happening is the terminal is opened up and then it says to press any key to continue then it says process complete. I am using mac. Terminal is mac version of console.
    My code looks like this:

    using System;

    namespace WhileTest
    {
    class Program
    {
    static void Main(string[] args)
    {
    int index = 0;

    while (index > 10)
    {
    Console.WriteLine("The number is {0}", index);
    index = index + 1;
    }
    }
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *