0% found this document useful (0 votes)
20 views25 pages

Finals CSharp Lecture 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views25 pages

Finals CSharp Lecture 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

WHILE & DO-WHILE

STATEMENT
WHILE LOOP
• It is used when you want a code
block to run repeatedly WHILE a
CONDITION is met.
WHILE LOOP SYNTAX
condition: This is a boolean
expression. As long as this condition
evaluates to true, the loop will
continue to execute the block of code
inside the curly braces {}.
block of code: This is the code that
will be executed each time the
condition is true.
WHILE LOOP EXAMPLE
WHILE LOOP EXAMPLE
Breakdown of the Example
int counter = 0;
Initializes a counter variable to 0.
while (counter < 5)
The condition checks if counter is less than 5. If it is, the loop
continues.
Console.WriteLine("Counter value: " + counter);
Prints the current value of counter to the console.
counter++;
Increments the value of counter by 1 each time the loop iterates.
ITERATING ARRAYS
• It means, read every element inside
an array and do something with it.
ACCESSING ARRAY SAMPLE
ACCESSING ARRAY SAMPLE
ACCESSING ARRAY SAMPLE
ARRAY LENGTH
• You can find out the size of an array by
calling their length variable (built in
variable in an array)
string [] names = {“David”, “Alenere”, “Jasfer”, “Ace”, “Patrick”};
Console.WriteLine(names.Lenght)
DO-WHILE LOOP
• Same as the While Loop but it
EXECUTES the Code Block first
before checking the CONDITION.
DO-WHILE LOOP SYNTAX
do:
The keyword that starts the do-while loop.
block of code:
The code inside the curly braces {} will be
executed at least once.
while (condition):
After executing the block of code, the
condition is checked. If the condition evaluates to true,
the loop repeats. If false, the loop terminates.
DO-WHILE LOOP EXAMPLE
Breakdown of the Example
int counter = 0;
Initializes a counter variable to 0.
do { ... } while (counter < 5);
Executes the block of code inside the do at least once, then
checks the condition counter < 5. If the condition is true, the loop repeats;
otherwise, it stops.
Console.WriteLine("Counter value: " + counter);
Prints the current value of counter to the console.
counter++;
Increments the value of counter by 1 each time the loop iterates.
BREAK KEYWORD
• Used to BREAK OUT of a LOOP
Statement earlier that it is expected
to end.
BREAK KEYWORD
CONDITION in WHILE LOOP
• You can use CONDITIONS inside a
while loop for different reasons like,
searching inside an array.
CONDITION in WHILE LOOP Example
FOR LOOP
Finals - C# LECTURE
FOR LOOP
• It is used when you want a Code Block to run
repeatedly WHILE the Condition is met.
• It’s a more compact format but more complicated
version of a WHILE Loop. For Loops are
commonly used to iterate through Collections like
Array.
SYNTAX
• Statement 1 is executed (one
time) before the execution of the
code block.
• Statement 2 defines the condition
for executing the code block.
• Statement 3 is executed (every
time) after the code block has
been executed.
Example
OUTPUT:

0
1
2
3
4
Example
OUTPUT:

0
2
4
6
8
10
Problem 1. C# program that finds the sum of
the first 10 natural numbers.
Problem 1. C# program that finds the sum of
the first 10 natural numbers.
Note!
• For more examples click the link
below!
• https://www.w3resource.com/csharp-e
xercises/for-loop/index.php

You might also like