SlideShare a Scribd company logo
Visual Programing
Lecture No 07
Ameer Hamza
C# While Loop
 The while loop loops through a block of code as long as a specified condition
is True:
 Syntax
while (condition)
{
// code block to be executed
}
The Do/While Loop
 The do/while loop is a variant of the while loop. This loop will execute the
code block once, before checking if the condition is true, then it will repeat
the loop as long as the condition is true.
Syntax
do
{
// code block to be executed
} while(condition);
C# For Loop
 When you know exactly how many times you want to loop through a block of
code, use the for loop instead of a while loop:
Syntax
for (statement 1; statement 2; statement 3)
{
// code block to be executed
}
 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:
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
Nested Loops
 It is also possible to place a loop inside another loop. This is called a nested
loop.
 The "inner loop" will be executed one time for each iteration of the "outer
loop":
Example
// Outer loop
for (int i = 1; i <= 2; ++i)
{
Console.WriteLine("Outer: " + i); // Executes 2 times
// Inner loop
for (int j = 1; j <= 3; j++)
{
Console.WriteLine(" Inner: " + j); // Executes 6 times (2 * 3)
}
}
Task 1:
 Write a C# Sharp program to find the sum of the first 10 natural numbers.
Expected Output :
The first 10 natural number is :
1 2 3 4 5 6 7 8 9 10
The Sum is : 55
Task 2:
 Write a c# program input starting and ending number from the user and
generate even number In the given range
Example:
Enter starting number : 1
Enter Ending Number: 10
Even Number: 2 4 6 8 10
Task 3:
 Write a program in C# Sharp to display a right angle triangle with an asterisk.
Sample Output:
*
**
***
****
Task 4:
 Write a program in C# Sharp to print Floyd's Triangle. The Floyd's triangle is as
below :
1
01
101
0101
10101
C# Foreach Loop
 What is the foreach loop?
 A foreach loop is a famous structure in programming languages like PHP, Java,
and C#. It is used to iterate through an array or collection of elements and
perform specific actions on each element.
 Syntax
foreach (type variableName in arrayName)
{
// code block to be executed
}
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
foreach (string i in cars)
{
Console.WriteLine(i);
}
}
}
}
Continue statement
 We can use the "continue" statement within the loop in such cases. The continue statement tells the
loop to skip the current iteration and move on to the next one.
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
if (number == 3)
{
continue;
}
Console.WriteLine(number);
}
Visual Programing basic lectures  7.pptx
For each loop break
 When placed inside a foreach loop, the break statement immediately exits the loop and continues
with the following line of code outside the loop. It is a valuable tool in controlling the program flow
and can be used to optimize performance by avoiding unnecessary iterations.
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
if (number == 3)
{
break;
}
Console.WriteLine(number);
}
Visual Programing basic lectures  7.pptx
Foreach in Collection
 The C# foreach loop can be implemented to read the number of occurrences
of a certain number or character in the collection.
string str = "education is free";
char[] chars = str.ToCharArray();
int ecount = 0;
// Loop through chars and find all 'n' and count them
foreach (char ch in chars)
{
if (ch == 'e')
ecount++;
}
Console.WriteLine($"Total e found {ecount}");
}
Visual Programing basic lectures  7.pptx
Find Character in string and Remove it
C# Arrays
 Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
 To declare an array, define the variable type with square brackets:
string[] cars;
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int[] myNum = {10, 20, 30, 40};
Visual Programing basic lectures  7.pptx
Visual Programing basic lectures  7.pptx
Visual Programing basic lectures  7.pptx
Visual Programing basic lectures  7.pptx
Visual Programing basic lectures  7.pptx
Visual Programing basic lectures  7.pptx
Visual Programing basic lectures  7.pptx
Visual Programing basic lectures  7.pptx
Visual Programing basic lectures  7.pptx
C# Sort Arrays
 There are many array methods available, for example Sort(), which sorts an
array alphabetically or in an ascending order:
int[] myNumbers = {5, 1, 8, 9};
Array.Sort(myNumbers);
foreach (int i in myNumbers)
{
Console.WriteLine(i);
}
Visual Programing basic lectures  7.pptx
Array
 Single-Dimensional Arrays
 A single-dimensional array is a linear data structure with elements stored
sequentially in memory. These arrays can be thought of as a list of elements,
where each element has a unique index starting from zero.
 To create a single-dimensional array in C#, you need to declare the type of
the elements, followed by square brackets [], the name of the array, and then
use the new` keyword to create an instance of the array with a specified
size. Here’s an example:
int[] numbers = new int[5];
Array
 Multi-Dimensional Arrays
 Multi-dimensional arrays are arrays with more than one dimension. A two-
dimensional array, for example, can be visualized as a table with rows and
columns. Similarly, a three-dimensional array can be represented as a
collection of two-dimensional arrays, and so on.
 To create a multi-dimensional array, you need to specify the dimensions using
multiple sets of square brackets [,]. Here’s an example of creating a two-
dimensional array:
int[,] matrix = new int[3, 4];
Array
 Jagged Arrays
 Jagged arrays, also known as arrays of arrays, are arrays whose elements are
other arrays with varying lengths. They can be visualized as a collection of
rows, where each row can have a different number of columns.
 Creating a jagged array involves declaring an array with a single set of square
brackets [], followed by another set of square brackets []. Then, you can
create the individual arrays for each element of the jagged array. Here’s an
example
data_type[][] name_of_array = new data_type[rows][]
int[][] jagged_arr = new int[4][]
 Example 1:
 Providing the size of each array elements separately. Here each of the
elements is a 1-D array of integers where:
 The first row or element is an array of 2 integers.
 The second row or element is an array of 4 integers.
 The third row or element is an array of 6 integers.
 The fourth row or element is an array of 7 integers.
 jagged_arr[0] = new int[2];
 jagged_arr[1] = new int[4];
 jagged_arr[2] = new int[6];
 jagged_arr[3] = new int[7];
jagged_arr[0] = new int[] {1, 2, 3, 4};
jagged_arr[1] = new int[] {11, 34, 67};
jagged_arr[2] = new int[] {89, 23};
jagged_arr[3] = new int[] {0, 45, 78, 53, 99};
Using the Direct Method
int[][] jagged_arr = new int[][]
{
new int[] {1, 2, 3, 4},
new int[] {11, 34, 67},
new int[] {89, 23},
new int[] {0, 45, 78, 53, 99}
};
Array Function:
 Other useful array methods, such as Min, Max, and Sum, can be found in the
System.Linq namespace:
using System;
using System.Linq;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int[] myNumbers = {5, 1, 8, 9};
Console.WriteLine(myNumbers.Max()); // largest value
Console.WriteLine(myNumbers.Min()); // smallest value
Console.WriteLine(myNumbers.Sum()); // sum of myNumbers
}
}
}

More Related Content

PPT
Arrays in c programing. practicals and .ppt
DOC
Arrays In General
PDF
Arrays and strings in c++
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
DOCX
Array assignment
PPTX
CHAPTER 5
PPTX
CPP Homework Help
Arrays in c programing. practicals and .ppt
Arrays In General
Arrays and strings in c++
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Array assignment
CHAPTER 5
CPP Homework Help

Similar to Visual Programing basic lectures 7.pptx (20)

PDF
PDF
Array and Collections in c#
PDF
ARRAYS
PDF
PDF
Lecture 2 java.pdf
PPTX
Programming in c Arrays
PPTX
C++ Programming Homework Help
PPTX
arrays-120712074248-phpapp01
PPTX
6 arrays injava
PPTX
Programming in C (part 2)
PPTX
Arrays in programming
PPT
PPTX
Lecture 1 mte 407
PPTX
Lecture 1 mte 407
PDF
Array and its types and it's implemented programming Final.pdf
PDF
Acm aleppo cpc training second session
PPT
PDF
Array and Collections in c#
ARRAYS
Lecture 2 java.pdf
Programming in c Arrays
C++ Programming Homework Help
arrays-120712074248-phpapp01
6 arrays injava
Programming in C (part 2)
Arrays in programming
Lecture 1 mte 407
Lecture 1 mte 407
Array and its types and it's implemented programming Final.pdf
Acm aleppo cpc training second session
Ad

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
RMMM.pdf make it easy to upload and study
PDF
Insiders guide to clinical Medicine.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Cell Types and Its function , kingdom of life
PPTX
Institutional Correction lecture only . . .
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Sports Quiz easy sports quiz sports quiz
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
01-Introduction-to-Information-Management.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial diseases, their pathogenesis and prophylaxis
RMMM.pdf make it easy to upload and study
Insiders guide to clinical Medicine.pdf
VCE English Exam - Section C Student Revision Booklet
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Cell Types and Its function , kingdom of life
Institutional Correction lecture only . . .
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Computing-Curriculum for Schools in Ghana
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
TR - Agricultural Crops Production NC III.pdf
PPH.pptx obstetrics and gynecology in nursing
Sports Quiz easy sports quiz sports quiz
Ad

Visual Programing basic lectures 7.pptx

  • 2. C# While Loop  The while loop loops through a block of code as long as a specified condition is True:  Syntax while (condition) { // code block to be executed }
  • 3. The Do/While Loop  The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. Syntax do { // code block to be executed } while(condition);
  • 4. C# For Loop  When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop: Syntax for (statement 1; statement 2; statement 3) { // code block to be executed }
  • 5.  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.
  • 6. Example: for (int i = 0; i < 5; i++) { Console.WriteLine(i); }
  • 7. Nested Loops  It is also possible to place a loop inside another loop. This is called a nested loop.  The "inner loop" will be executed one time for each iteration of the "outer loop":
  • 8. Example // Outer loop for (int i = 1; i <= 2; ++i) { Console.WriteLine("Outer: " + i); // Executes 2 times // Inner loop for (int j = 1; j <= 3; j++) { Console.WriteLine(" Inner: " + j); // Executes 6 times (2 * 3) } }
  • 9. Task 1:  Write a C# Sharp program to find the sum of the first 10 natural numbers. Expected Output : The first 10 natural number is : 1 2 3 4 5 6 7 8 9 10 The Sum is : 55
  • 10. Task 2:  Write a c# program input starting and ending number from the user and generate even number In the given range Example: Enter starting number : 1 Enter Ending Number: 10 Even Number: 2 4 6 8 10
  • 11. Task 3:  Write a program in C# Sharp to display a right angle triangle with an asterisk. Sample Output: * ** *** ****
  • 12. Task 4:  Write a program in C# Sharp to print Floyd's Triangle. The Floyd's triangle is as below : 1 01 101 0101 10101
  • 13. C# Foreach Loop  What is the foreach loop?  A foreach loop is a famous structure in programming languages like PHP, Java, and C#. It is used to iterate through an array or collection of elements and perform specific actions on each element.  Syntax foreach (type variableName in arrayName) { // code block to be executed }
  • 14. using System; namespace MyApplication { class Program { static void Main(string[] args) { string[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; foreach (string i in cars) { Console.WriteLine(i); } } } }
  • 15. Continue statement  We can use the "continue" statement within the loop in such cases. The continue statement tells the loop to skip the current iteration and move on to the next one. int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { if (number == 3) { continue; } Console.WriteLine(number); }
  • 17. For each loop break  When placed inside a foreach loop, the break statement immediately exits the loop and continues with the following line of code outside the loop. It is a valuable tool in controlling the program flow and can be used to optimize performance by avoiding unnecessary iterations. int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { if (number == 3) { break; } Console.WriteLine(number); }
  • 19. Foreach in Collection  The C# foreach loop can be implemented to read the number of occurrences of a certain number or character in the collection.
  • 20. string str = "education is free"; char[] chars = str.ToCharArray(); int ecount = 0; // Loop through chars and find all 'n' and count them foreach (char ch in chars) { if (ch == 'e') ecount++; } Console.WriteLine($"Total e found {ecount}"); }
  • 22. Find Character in string and Remove it
  • 23. C# Arrays  Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.  To declare an array, define the variable type with square brackets: string[] cars; string[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; int[] myNum = {10, 20, 30, 40};
  • 33. C# Sort Arrays  There are many array methods available, for example Sort(), which sorts an array alphabetically or in an ascending order: int[] myNumbers = {5, 1, 8, 9}; Array.Sort(myNumbers); foreach (int i in myNumbers) { Console.WriteLine(i); }
  • 35. Array  Single-Dimensional Arrays  A single-dimensional array is a linear data structure with elements stored sequentially in memory. These arrays can be thought of as a list of elements, where each element has a unique index starting from zero.  To create a single-dimensional array in C#, you need to declare the type of the elements, followed by square brackets [], the name of the array, and then use the new` keyword to create an instance of the array with a specified size. Here’s an example: int[] numbers = new int[5];
  • 36. Array  Multi-Dimensional Arrays  Multi-dimensional arrays are arrays with more than one dimension. A two- dimensional array, for example, can be visualized as a table with rows and columns. Similarly, a three-dimensional array can be represented as a collection of two-dimensional arrays, and so on.  To create a multi-dimensional array, you need to specify the dimensions using multiple sets of square brackets [,]. Here’s an example of creating a two- dimensional array: int[,] matrix = new int[3, 4];
  • 37. Array  Jagged Arrays  Jagged arrays, also known as arrays of arrays, are arrays whose elements are other arrays with varying lengths. They can be visualized as a collection of rows, where each row can have a different number of columns.  Creating a jagged array involves declaring an array with a single set of square brackets [], followed by another set of square brackets []. Then, you can create the individual arrays for each element of the jagged array. Here’s an example data_type[][] name_of_array = new data_type[rows][]
  • 38. int[][] jagged_arr = new int[4][]
  • 39.  Example 1:  Providing the size of each array elements separately. Here each of the elements is a 1-D array of integers where:  The first row or element is an array of 2 integers.  The second row or element is an array of 4 integers.  The third row or element is an array of 6 integers.  The fourth row or element is an array of 7 integers.  jagged_arr[0] = new int[2];  jagged_arr[1] = new int[4];  jagged_arr[2] = new int[6];  jagged_arr[3] = new int[7];
  • 40. jagged_arr[0] = new int[] {1, 2, 3, 4}; jagged_arr[1] = new int[] {11, 34, 67}; jagged_arr[2] = new int[] {89, 23}; jagged_arr[3] = new int[] {0, 45, 78, 53, 99};
  • 41. Using the Direct Method int[][] jagged_arr = new int[][] { new int[] {1, 2, 3, 4}, new int[] {11, 34, 67}, new int[] {89, 23}, new int[] {0, 45, 78, 53, 99} };
  • 42. Array Function:  Other useful array methods, such as Min, Max, and Sum, can be found in the System.Linq namespace:
  • 43. using System; using System.Linq; namespace MyApplication { class Program { static void Main(string[] args) { int[] myNumbers = {5, 1, 8, 9}; Console.WriteLine(myNumbers.Max()); // largest value Console.WriteLine(myNumbers.Min()); // smallest value Console.WriteLine(myNumbers.Sum()); // sum of myNumbers } } }