SlideShare a Scribd company logo
1 of 2
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

Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 

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
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
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...
 
queue
queuequeue
queue
 
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
 
Looping
LoopingLooping
Looping
 
Xamarin: Branching and Looping
Xamarin: Branching and LoopingXamarin: Branching and Looping
Xamarin: Branching and Looping
 
Different loops in C
Different loops in CDifferent loops in C
Different loops in C
 
Illicium: Compiling Pharo to C
Illicium: Compiling Pharo to CIllicium: Compiling Pharo to C
Illicium: Compiling Pharo to C
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Nested loops
Nested loopsNested loops
Nested loops
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Iteration
IterationIteration
Iteration
 
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
 
Conditional Loops Python
Conditional Loops PythonConditional Loops Python
Conditional Loops Python
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 

Similar to C#

Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05
IIUM
 
04a intro while
04a intro while04a intro while
04a intro while
hasfaa1017
 
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
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
bluejayjunior
 

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
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
07 flow control
07   flow control07   flow control
07 flow control
 
Loops c++
Loops c++Loops c++
Loops c++
 
Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05
 
06 Loops
06 Loops06 Loops
06 Loops
 
04a intro while
04a intro while04a intro while
04a intro while
 
Operators
OperatorsOperators
Operators
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
06.Loops
06.Loops06.Loops
06.Loops
 
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
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
 
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptx
 
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
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
Control structures
Control structuresControl structures
Control structures
 
PYTHON.pptx
PYTHON.pptxPYTHON.pptx
PYTHON.pptx
 

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); } } } }