ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
The for loop in C# is useful for iterating over arrays and for sequential processing.
That is the statements within the code block of a for loop will execute a series of
statements as long as a specific condition remains true.
Syntax:
for(initialization; condition; step)
statement
initialization : Initialize the value of variable.
condition : Evaluate the condition
step : Step taken for each execution of loop body
The for loop initialize the value before the first step. Then checking the condition
against the current value of variable and execute the loop statement and then perform
the step taken for each execution of loop body.
int count = 4;
for (int i = 1; i < = count; i++)
{
MessageBox.Show("Current value of i is - " + i);
}
The output of the code as follows :
Current value of i is - 1
Current value of i is - 2
Current value of i is - 3
Current value of i is - 4
The loop will execute four times because we set the condition i is less than or equal to
count.
for (int i = 1; i < = count; i++)
initialization : int i = 1
Initialize the variable i as 1, that is when the loop starts the
value of i is set as 1
condition : i < = count
Set the condition i < =count , that is the loop will execute up to
when the value of i < = 4 (four times)
step : i++
Set the step for each execution of loop block as i++ ( i = i +1)
All of the expressions of the for loop statements are optional. The following statement
is used to write an infinite loop.
for (; ; )
{
// statements
}
Here the loop will execute infinite times because there is no initialization , condition
and steps.
Download Source Code
Print Source Code
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int count = 4;
for (int i = 1; i < = count; i++)
{
MessageBox.Show("Current value of i is - " + i);
}
}
}
}

More Related Content

What's hot (19)

For Loops and Nesting in Python
For Loops and Nesting in PythonFor Loops and Nesting in Python
For Loops and Nesting in Python
primeteacher32
Ìý
Chapter 3
Chapter 3Chapter 3
Chapter 3
Amrit Kaur
Ìý
Loop Introduction for Loop while Loop do while Loop Nested Loops Values of...
Loop Introduction for Loop  while Loop do while Loop  Nested Loops  Values of...Loop Introduction for Loop  while Loop do while Loop  Nested Loops  Values of...
Loop Introduction for Loop while Loop do while Loop Nested Loops Values of...
imtiazalijoono
Ìý
queue
queuequeue
queue
umair khan
Ìý
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
Ìý
Looping
LoopingLooping
Looping
Kulachi Hansraj Model School Ashok Vihar
Ìý
Xamarin: Branching and Looping
Xamarin: Branching and LoopingXamarin: Branching and Looping
Xamarin: Branching and Looping
Eng Teong Cheah
Ìý
Different loops in C
Different loops in CDifferent loops in C
Different loops in C
Md. Arif Hossain
Ìý
Illicium: Compiling Pharo to C
Illicium: Compiling Pharo to CIllicium: Compiling Pharo to C
Illicium: Compiling Pharo to C
ESUG
Ìý
Looping in c++
Looping in c++Looping in c++
Looping in c++
deekshagopaliya
Ìý
Nested loops
Nested loopsNested loops
Nested loops
Neeru Mittal
Ìý
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
Ìý
C++ control loops
C++ control loopsC++ control loops
C++ control loops
pratikborsadiya
Ìý
Loops in c language
Loops in c languageLoops in c language
Loops in c language
tanmaymodi4
Ìý
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
Sriman Sawarthia
Ìý
Iteration
IterationIteration
Iteration
Liam Dunphy
Ìý
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
Ìý
Conditional Loops Python
Conditional Loops PythonConditional Loops Python
Conditional Loops Python
primeteacher32
Ìý
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
Huda Alameen
Ìý
For Loops and Nesting in Python
For Loops and Nesting in PythonFor Loops and Nesting in Python
For Loops and Nesting in Python
primeteacher32
Ìý
Chapter 3
Chapter 3Chapter 3
Chapter 3
Amrit Kaur
Ìý
Loop Introduction for Loop while Loop do while Loop Nested Loops Values of...
Loop Introduction for Loop  while Loop do while Loop  Nested Loops  Values of...Loop Introduction for Loop  while Loop do while Loop  Nested Loops  Values of...
Loop Introduction for Loop while Loop do while Loop Nested Loops Values of...
imtiazalijoono
Ìý
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
Ìý
Xamarin: Branching and Looping
Xamarin: Branching and LoopingXamarin: Branching and Looping
Xamarin: Branching and Looping
Eng Teong Cheah
Ìý
Different loops in C
Different loops in CDifferent loops in C
Different loops in C
Md. Arif Hossain
Ìý
Illicium: Compiling Pharo to C
Illicium: Compiling Pharo to CIllicium: Compiling Pharo to C
Illicium: Compiling Pharo to C
ESUG
Ìý
Nested loops
Nested loopsNested loops
Nested loops
Neeru Mittal
Ìý
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
Ìý
Loops in c language
Loops in c languageLoops in c language
Loops in c language
tanmaymodi4
Ìý
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
Ìý
Conditional Loops Python
Conditional Loops PythonConditional Loops Python
Conditional Loops Python
primeteacher32
Ìý
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
Huda Alameen
Ìý

Similar to C# (20)

Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
Ìý
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
Ìý
Looping statements
Looping statementsLooping statements
Looping statements
Jaya Kumari
Ìý
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
JayBhavsar68
Ìý
07 flow control
07   flow control07   flow control
07 flow control
dhrubo kayal
Ìý
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
Ìý
Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05
IIUM
Ìý
06 Loops
06 Loops06 Loops
06 Loops
maznabili
Ìý
CS305PC_C++_UNIT 2.pdf jntuh third semester
CS305PC_C++_UNIT 2.pdf jntuh third semesterCS305PC_C++_UNIT 2.pdf jntuh third semester
CS305PC_C++_UNIT 2.pdf jntuh third semester
VeeraswamyDasari2
Ìý
04a intro while
04a intro while04a intro while
04a intro while
hasfaa1017
Ìý
Operators
OperatorsOperators
Operators
Allah Ditta
Ìý
itretion.docx
itretion.docxitretion.docx
itretion.docx
JavvajiVenkat
Ìý
06.Loops
06.Loops06.Loops
06.Loops
Intro C# Book
Ìý
Introduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptxIntroduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptx
ofeliacanaria1
Ìý
Programming Fundamentals presentation slide
Programming Fundamentals presentation slideProgramming Fundamentals presentation slide
Programming Fundamentals presentation slide
mibrahim020205
Ìý
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
Ìý
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
Ravi Bhadauria
Ìý
Managing input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and loopingManaging input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and looping
letheyabala
Ìý
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptx
VijayKumarLokanadam
Ìý
JAN CARL BRIONES-Writing Programs Using Loops.pptx
JAN CARL BRIONES-Writing Programs Using Loops.pptxJAN CARL BRIONES-Writing Programs Using Loops.pptx
JAN CARL BRIONES-Writing Programs Using Loops.pptx
JanCarlBriones2
Ìý
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
Ìý
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
Ìý
Looping statements
Looping statementsLooping statements
Looping statements
Jaya Kumari
Ìý
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
JayBhavsar68
Ìý
07 flow control
07   flow control07   flow control
07 flow control
dhrubo kayal
Ìý
Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05
IIUM
Ìý
06 Loops
06 Loops06 Loops
06 Loops
maznabili
Ìý
CS305PC_C++_UNIT 2.pdf jntuh third semester
CS305PC_C++_UNIT 2.pdf jntuh third semesterCS305PC_C++_UNIT 2.pdf jntuh third semester
CS305PC_C++_UNIT 2.pdf jntuh third semester
VeeraswamyDasari2
Ìý
04a intro while
04a intro while04a intro while
04a intro while
hasfaa1017
Ìý
Introduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptxIntroduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptx
ofeliacanaria1
Ìý
Programming Fundamentals presentation slide
Programming Fundamentals presentation slideProgramming Fundamentals presentation slide
Programming Fundamentals presentation slide
mibrahim020205
Ìý
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
Ìý
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
Ravi Bhadauria
Ìý
Managing input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and loopingManaging input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and looping
letheyabala
Ìý
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptx
VijayKumarLokanadam
Ìý
JAN CARL BRIONES-Writing Programs Using Loops.pptx
JAN CARL BRIONES-Writing Programs Using Loops.pptxJAN CARL BRIONES-Writing Programs Using Loops.pptx
JAN CARL BRIONES-Writing Programs Using Loops.pptx
JanCarlBriones2
Ìý

C#

  • 1. The for loop in C# is useful for iterating over arrays and for sequential processing. That is the statements within the code block of a for loop will execute a series of statements as long as a specific condition remains true. Syntax: for(initialization; condition; step) statement initialization : Initialize the value of variable. condition : Evaluate the condition step : Step taken for each execution of loop body The for loop initialize the value before the first step. Then checking the condition against the current value of variable and execute the loop statement and then perform the step taken for each execution of loop body. int count = 4; for (int i = 1; i < = count; i++) { MessageBox.Show("Current value of i is - " + i); } The output of the code as follows : Current value of i is - 1 Current value of i is - 2 Current value of i is - 3 Current value of i is - 4 The loop will execute four times because we set the condition i is less than or equal to count. for (int i = 1; i < = count; i++) initialization : int i = 1 Initialize the variable i as 1, that is when the loop starts the value of i is set as 1 condition : i < = count Set the condition i < =count , that is the loop will execute up to
  • 2. when the value of i < = 4 (four times) step : i++ Set the step for each execution of loop block as i++ ( i = i +1) All of the expressions of the for loop statements are optional. The following statement is used to write an infinite loop. for (; ; ) { // statements } Here the loop will execute infinite times because there is no initialization , condition and steps. Download Source Code Print Source Code using System; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int count = 4; for (int i = 1; i < = count; i++) { MessageBox.Show("Current value of i is - " + i); } } } }