Skip to main content

WHILE and IF statements

A while loop statement will repeatedly execute a statement as long as the given condition is true. If the while loop's condition is set to true then the program will go into the loop, and will keep looping as long as that condition remains true. For example:
int score = 15;
while (score  <  20)
      {
            Console.WriteLine("value of score: {0}"), score;
             score++;
       }
       Console.ReadLine();
In this example the program will go into the loop for as long as the value of score is less than 20. Once it's in the loop it will increment the value of score by one, then it will check if a is still less than 20. Once the value of score reaches 20, it will no longer go into the loop as the condition will no longer be true.
A IF statement on the other hand can be a bit similar to a while statement, as it will check the condition at first, and if it's true it will execute the block of code inside of it. IF the condition is true it will run the code but it will only do it once as compared to a while statement. One can also use an ELSE statement to run a different set of  code if the condition is false. Example:


C# if...else statement


Both WHILE and IF statements can be used together in programming, as for example one can have a while statement running for as long as the value of quit is set to false. Inside of the while loop you can use an IF statement to check if the user wants to quit the program. For example:
bool quit = false;
string userResponse;


while (quit == false)
    {
           Console.WriteLine("enter Q to quit")
           userResponse = Console.ReadLine();
           If (userResponse == "Q")
                    {
                          Console.WriteLine("Until next time!");
                           Goodbye();
                     }
     }
Sources:
http://www.tutorialspoint.com/csharp/csharp_while_loop.htm
https://msdn.microsoft.com/en-us/library/5011f09h.aspx
http://www.tutorialspoint.com/csharp/if_else_statement_in_csharp.htm

Comments

Popular posts from this blog

Your Online Life, Permanent As a Tattoo

View TED Talk Link Watching this TED talk titled “Your Online Life, Permanent As a tattoo” talks about how the use of social media can be the same as having a personal tattoo. Juan Enriquez, the main speaker, begins the presentation by using visual pictures of what tattoos can represent in a person's life. Using pictures, he shows the audience that tattoos can be beautiful, intriguing, intimate and also builds humor by stating that they can be mistakes. After showing the audience these different types of tattoos, he starts to question them about what if social sites and services were to become personal tattoos. At this point he begins to add more information on how the way we express ourselves on  social media can impact and create our  “Electronic Tattoo”. Juan states how difficult it is to get away from our electronic tattoos, since forms of technology, like facial recognition are getting better and better at recognizing people's faces. He adds up that if someone...

The importance of sharing data between functions/methods, and their scope.

It is important that functions/methods share data between a class because in programming you will be having a lot of methods that will need to share data to function correctly. For example, if you had to calculate different types of data with equations, you would have the calculations being done in one method then transfer only the calculated data to a display method. If data isn't shared between these two classes then only the calculation method will have the data, and no other methods can access it, making it difficult to use that data to perform more tasks. The scope refers to the portion of the program a variable is visible, as well as where it can only be used. It is referred to where a variable can be read from and/or written to, in other words where the variable lives. Visibility compared to scope is where the variables can be used and shared. We can set where a variable can be accessed with three different options, public, private and protected. This is important becau...

How variables and properties work/compare.

Variables are storage locations that data can be stored in. They are are a way of naming data locations that can be later on used in the program. Variables generally have four attributes: an identifier, data location, type and value.  We have used variables in most of our programs in class, for example, storing names as strings and numbers as doubles/ints. Properties on the other hand can be looked as a special type of method that exposes a variable. A property can have can have a Get procedure (read only), a Set procedure (write only), or both (read-write). Properties are effective in a way that they have access to private data inside of a class, and can manipulate the data as it enters or leaves the class. Variables and properties are both important in programming because they help store and manipulate data. Variables can be used to store and protect important user data inside of classes, and the property of those variables help manage what data is only going out into outsid...