Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C# Lesson 2 Loops
08-17-2009, 09:08 AM
Post: #1
C# Lesson 2 Loops
Code:
using System;
using System.Collections.Generic;
using System.Text;


/*Lesson 2 : Loops,
* Loops are a great way to make things easier for your application for example
* if you want to increment a number (1) to 100 you normally would have to either do this
* lets say i is our integer and i = 1
* you would have to do this    i++ or i + 1; to increment it by one
* instead you could use a loop!
* here is a basic do- while loop i don't like them too much but heres an Example:
* int i = 1;
* do {
* i++;
* }while( i != 100);
*
* now what i generally use a for loop
* for(int i =1; i != 100; i++)
* {
* //code here to either display whats happening or do something a certain amountof times
* }
*
*
* Loops that you can break out of:
* These are useful loops i shouldn't have to explain how they are useful if you have the mind set of a programmer
* For this loop i will use it to determine what numbers are even
*/
namespace Loops // namespace declaration
{
    class Program   //class declaration
    {
        static void Main(string[] args)//iniates the program
        {

            Console.WriteLine("Enter in your number:");// Prompts the user
            int n_ = Convert.ToInt32(Console.ReadLine());//iniates a variable named _n makes it able to input text by converting string to int


            for (double e = 0; e != Convert.ToInt64(n_); e++)// the start of your for loop! declares your variable there was no need to convert i just wanted it to hold bigger numbers then increments by one for each loop
            {// what is inside your loop


                if (e % 2 == 1)// if (variable e modulus 2 = 1 ) continue with operation
                    continue;
                if (e == n_)//if(variable e reaches the stopping point n_ break;
                    break ;

                Console.Write("{0} ", e);//writes your even variables
            }//closes loop
            Console.ReadLine();//pauses it


        }


    }





}


I also included everything in the .rar file this tutorial shows you along with loops I/O and how to see if a number is even or odd using the Modulus operator Feedback questions post below also what should my next tutorial be?


Attached File(s)
.rar  Loops.rar (Size: 16.06 KB / Downloads: 1)

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
08-20-2009, 05:39 AM
Post: #2
RE: C# Lesson 2 Loops
This is good tut, it is interesting how while loops in pascal and c# are the same but c# has "do while" and pascal "repeat until" !
You could make a comparation between pascal and c# . . . but it is okay this way.
And another suggestion, make something with 2 loops (one inside of another) !
And break & continue operations are good . . . and also declaring variables inside a for loop !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
08-20-2009, 07:38 AM
Post: #3
RE: C# Lesson 2 Loops
(08-20-2009 05:39 AM)drdebcol Wrote:  This is good tut, it is interesting how while loops in pascal and c# are the same but c# has "do while" and pascal "repeat until" !
You could make a comparation between pascal and c# . . . but it is okay this way.
And another suggestion, make something with 2 loops (one inside of another) !
And break & continue operations are good . . . and also declaring variables inside a for loop !

haha can do lesson 2
More advanced loops i think they're called nested loops though

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: