Welcome
This is Pirzada Rashid here
This video is about for loop in C#.
Before you start
I want you to open up this URL
Whatever code examples, I use in this video are written step by step here
Code examples have a link to quickly test it
and see the results in an online editor without writing a single word
I will also leave all the links at the end and in the description of this video
Let's get started
for loop executes a block of code repeatedly until the condition is false
Actually, this type of loop is useful when the number of loop iterations is fixed
or you can say predetermined
and the counter variable that determines the iterations is needed in the loop
like how many times the loop executed
Now, this is the basic structure of a loop
Please pay attention
for loop starts with the for keyword
followed by 3 expressions
separated by semicolons ";"
within these parentheses
The first parameter declares and initializes a counter variable before the loop executes
and is always evaluated once at the start
It's called initializer
The second parameter is a condition
(a Boolean expression) that returns true or false
and is evaluated before each new iteration of the loop
This must be true in order for the next iteration to be performed
The iterations end when the condition is false
The third parameter is an iterator
which prepares the loop for next iteration
This is incremented after each iteration at the bottom of the loop
Statements are the block of code that you want to execute multiple times in a loop
Let me show you the execution order of a Loop
First of all
variable declared and assigned initial value at this stage
then the condition is checked
If it's true
the body will execute
Here, variable value is incremented
This whole process repeats again except number 1
because variable is initialized only once at the beginning
Loop ends when number 2 returns false
This is the structure of a nested for loop.
A loop within another loop is a nested loop
The innermost loop executes more times than the outermost
There is no limitation on the number of loops into each other
That means you can combine different types of loops in your program
I am using this online editor for the demo.
Let me quickly code for loop than I explain it to you
This loop writes out all the integers from 0 to 4
Well, this loop consists of 3 expressions
One
Two
and three
This is the initialization part
When the loop begins
C# initializes the counter variable i with the int type
and assigns an initial value of 1
A Boolean condition
i less then equal to 4
which returns true or false
will repeatedly execute until the counter, which is this
is less than or equal to 4
This is expression for updating the counter
which adds 1 at the end of each repetition of the loop
This increases the counter variable by 1
Click RUN
See the output here
If you have more than one statement within the loop
you must enclose the statements in opening and closing braces like this
break keyword exit a loop prematurely, before it has completed the execution
The loop's body is skipped
and the control is passed to the first expression immediately following the loop
Let me show you
Click RUN
Notice that the loop ends prematurely at break statement when the counter reaches 3
and this condition becomes 3 equal to 3, which evaluates to true
Without the break keyword, the loop should run from 1 to 4
This break keyword then causes the loop to exit
and resumes execution immediately after the loop
You can see the result here
Skipped the value 3 and 4
Another commonly used jump statement is the continue keyword
When the loop hits continue
the rest of the current iteration is skipped
and immediately initiates the execution of the next iteration
Let's change this with continue keyword
Click RUN
First, we initialized the counter variable to run from 1 to 4
When the counter reaches 3
this condition evaluates to true because 3 equal to 3
This causes for loop to skip rest of the current iteration and initiates the execution of the next iteration
You can see the result here
Number 3 skipped
Let's see
how nested loops look like
Let me explain it to you
After initialization of the first for loop
the execution of its body will start
which contains the second for loop
Here, initialization of first for loop means
i variable is initialized with the value 1
and then i less then equal to 4 condition is checked, meaning 1 less then equal to 4
This expression is true
as you can see 1 is less than 4
Now, the inner loop variable will be initialized meaning j is initialized to 1
condition will be checked which is j less then equal to 2 meaning 1 less than equal to 2
and then the code within its body will be executed
then the j variable will be updated
and execution will continue until the condition returns false
That means, when j is equal to 3
After that
the second iteration of the first for loop will continue
its variable will be updated
now i will be 2
and the whole second loop will be performed once again
This inner loop will fully execute as many times as the body of the outer loop
Meaning with each iteration of outer loop
the inner loop iterates 2 times
Because of this value
If I change it to 3
then the inner loop iterates 3 times
This statement
will print out the value of i and j
i is the variable of outer loop
and j is the variable of inner loop
Click RUN
This is the value of i
and this one is j
Whenever you don't provide a condition to the loop
it automatically becomes an infinite loop
meaning the loop block executes endlessly
Use the double semicolon (;;) in the for loop
to make it run infinite times
Let me show you how
This loop will run infinite times
Unfortunately
this editor don't allow infinite looping
so try to use Visual Studio Code
or another editor.
Ok, let me try VSCode
Remember, to stop the infinite loop
press CTRL + C on the keyboard

For more infomation >> A 2 Minute Guide To Your Membership PDF - Duration: 2:50. 


For more infomation >> DOTA AUTO CHESS GUIDE: Basic Items - Duration: 5:35. 


For more infomation >> Tibia Beginners Guide | Choosing Vocation, Venore, Daily Reward Shrine, Prey, Ultimate Booze Quest - Duration: 12:55.
For more infomation >> Så byter du fjädrar fram på VW TOURAN 1 (1T3) [GUIDE AUTODOC] - Duration: 9:21. 
Không có nhận xét nào:
Đăng nhận xét