ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

For loop in C#

.Net C# about 7 years ago || 10/12/2018 || 1.7 K View

  • Hide

For loop is control statement. It is a keyword used for repetitive execution of statement. For loop executes a block of statements repeatedly until the specified condition returns true/false value. It is one of the easiest loop and mostly  used.

The syntax of for loop is

for(variable declaration/initialization; condition; steps(increment/decrement))

{

//statements

}

Flowchart of  for loop

  1. Variable declaration= integer type. Example: int i; int j; Initialization = the beginning value of the variable. Example: i=0; i=20 etc.
  2. Condition:  The condition in loop is Boolean expression which returns either True or False. Example:  i<10; i<=20; I > 5; I >=10 ;
  3. Increment / decrement : It defines increment or decrement part: It executes only when the condition is satisfied.

Example: 

i++ , i-- ,i=i+2, i=i-2

Example of for loop

for ( int i=0; i<=10;i++) 

for (i=2;i<=20; i=i+2)

for (i=10; i>=0 ; i--)

Question: How to display pyramid using for loop in C# ?

Solution:

using System;
namespace Star_Pyramid
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j, k, n;
            Console.WriteLine("how many number of pyramid do you want");
            n = int.Parse(Console.ReadLine());
            for (i = 1; i <= n; i++)
            {
                for ( j = 1; j <=(n - i) / 2; j++)
                {
                    Console.Write("   ");
                }
                for ( k = 1; k <= i-1; k++)
                {
                    Console.Write(" * ");
                    Console.Write(" ");
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

Output of  above program is shown below

 

  • 0
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Manish Pandit

Manish Pandit

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey