Hot File

How to C# Nested For Loops

View: 140    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: C / C++ / MFC   Fields: Other

9 point/1 review File has been tested

Declaration of one or more for loops inside other for loop’s body is called nested loops

Introduction

Declaration of one or more for loops inside other for loop’s body is called nested loops

The for loop which is declared inside other for loop is called an inner for loop.

Nested for loops are very useful for creating programs which has more than one dimensions such as rows and columns, a basic calculator layout, different shapes etc

Using nested for loops we can access multidimensional array’s elements easily

Nested loops are used for complex programs

Example:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // initialize an int type variable sum with 0.
            int sum = 0;
 
            /* Following is for loop declaration, 
               this loop is called an outer loop,
               execute this loop 3 times according to its condition part.
             */
            for (int outerNumber = 1; outerNumber <= 3; outerNumber++)
            {   // starting outer loop scope.
 
                /* Following loop is called inner loop 
                   which is declared in outer loop’s body and 
                   according to condition this is also executed 3 times. 
                 */
                for (int innerNumber = 1; innerNumber <= 3; innerNumber++)
                {   // start inner loop scope
 
                    /* At every execution of inner for loop, 
                       1 will be added to sum variable. 
                       NOTE: this process continuous as is until inner loop’s condition becomes false 
                       and this inner loop depends on outer loop, 
                       so this inner loop always executed until outer loop becomes false.
                     */
                    sum += 1;
                     
                    // Print sum variable.
                    Console.Write(" {0}", sum);
 
                }   // End inner loop scope.
 
                /* When inner loop becomes false, 
                   than a new line will be created every time.
                 */
                Console.WriteLine();
 
            }   // End outer loop scope.
            Console.ReadKey();
 
        } // End of main method definition. 
    } // End of class. 
 
    /*
   The Output will be:
    1 2 3
    4 5 6
    7 8 9
  */
}

How to C# Nested For Loops

How to C# Nested For Loops Posted on 08-09-2016  Declaration of one or more for loops inside other for loop’s body is called nested loops 9/10 140

Comment:

To comment you must be logged in members.

Files with category

  • How to Swap Two Numbers using Call by Reference in C

    View: 260    Download: 0   Comment: 0   Author: none  

    How to Swap Two Numbers using Call by Reference in C

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

  • How to Print Fibonacci Series using Recursion in C, C++

    View: 262    Download: 0   Comment: 0   Author: none  

    How to Print Fibonacci Series using Recursion in C, C++

    Category: C / C++ / MFC
    Fields: Other

    4.5/1 review
    Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

  • How to Implicitly Typed Arrays In C#

    View: 291    Download: 0   Comment: 0   Author: none  

    How to Implicitly Typed Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    View: 190    Download: 0   Comment: 0   Author: none  

    How to C# Static vs Instance Methods As Event Handlers

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    View: 339    Download: 0   Comment: 0   Author: none  

    Build Pass By Reference To Method In C#

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    View: 178    Download: 0   Comment: 0   Author: none  

    How to C# Continue Statement

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    View: 131    Download: 0   Comment: 0   Author: none  

    How to Single Dimensional Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/1 review
    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

  • Build C# Multilevel Inheritance

    View: 145    Download: 0   Comment: 0   Author: none  

    Build C# Multilevel Inheritance

    Category: C / C++ / MFC
    Fields: Other

    2.5/2 review
    C# supports multilevel inheritance, suppose we have four classes A, B, C and D. Class A can inherit to B, B to C and C can inherit to D and so on now class D has all public members of class A, B and C include its own.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd