Programming Fundamentals 605116: From Problem Analysis To Program Design 4 Edition
Programming Fundamentals 605116: From Problem Analysis To Program Design 4 Edition
5
605116
ISRA University
Faculty of IT
Textbook
BARBARA DOYLE “C# Programming: From Problem Analysis to Program Design 4th
Edition
2
Chapter Objectives
• Learn about conditional expressions that return
Boolean results and those that use the bool data
type
• Examine equality, relational, and logical operators
used with conditional expressions
• Write if selection type statements to include one-
way, two-way, and nested forms
• Learn about and write switch statements
3
What Is Loop?
• A loop is a control statement that allows
repeating execution of a block of statements
– May execute a code block fixed number of
times
– May execute a code block while given
condition holds
– May execute a code block for each member of a
collection
• Loops that never end are called an infinite
loops
Using while(…) Loop
Repeating a Statement While
Given Condition Holds
How To Use While Loop?
while (condition)
•{ The simplest and most frequently used loop
statements;
}
• The repeat condition
– Returns a boolean result of true or false
– Also called loop condition
While Loop – How It Works?
false
condition
true
statement
While Loop – Example
int counter = 0;
while (counter < 10)
{
Console.WriteLine("Number : {0}", counter);
counter++;
}
while(…)
Examples
Sum 1..N – Example
• Calculate and print the sum of the first N natural
numbers
Console.Write("n = ");
int n = int.Parse(Console.ReadLine());
int number = 1;
int sum = 1;
Console.Write("The sum 1");
while (number < n)
{
number++;
sum += number ;
Console.Write("+{0}", number);
}
Console.WriteLine(" = {0}", sum);
Prime Number – Example
• Checking whether a number is prime or not
Console.Write("Enter a positive integer number: ");
uint number = uint.Parse(Console.ReadLine());
uint divider = 2;
uint maxDivider = (uint) Math.Sqrt(number);
bool prime = true;
while (prime && (divider <= maxDivider))
{
if (number % divider == 0)
{
prime = false;
}
divider++;
}
Console.WriteLine("Prime? {0}", prime);
Using break Operator
• break operator exits the inner-most loop
static void Main()
{
int n = Convert.ToInt32(Console.ReadLine());
// Calculate n! = 1 * 2 * ... * n
int result = 1;
while (true)
{
if(n == 1)
break;
result *= n;
n--;
}
Console.WriteLine("n! = " + result);
}
do { … }
while (…)
Loop
Using Do-While Loop
do
•{ Another loop structure is:
statements;
}
while (condition);
statement
true
condition
false
do { … }
while (…)
Examples
Factorial – Example
• Calculating N factorial
static void Main()
{
int n = Convert.ToInt32(Console.ReadLine());
int factorial = 1;
do
{
factorial *= n;
n--;
}
while (n > 0);
Result:
i=1, sum=1
i=2, sum=3
i=4, sum=7
i=8, sum=15
...
26
N^M – Example
• Calculating n to power m (denoted as n^m):
static void Main()
{
int n = int.Parse(Console.ReadLine());
int m = int.Parse(Console.ReadLine());
decimal result = 1;
for (int i=0; i<m; i++)
{
result *= n;
}
Console.WriteLine("n^m = " + result);
}
Using continue Operator
• continue operator ends the iteration of the inner-
most loop
• Example: sum all odd numbers in [1, n]
that are not divisors of 7:
int n = int.Parse(Console.ReadLine());
int sum = 0;
for (int i = 1; i <= n; i += 2)
{
if (i % 7 == 0)
{
continue;
}
sum += i;
}
Console.WriteLine("sum = {0}", sum);
foreach Loop
Iteration over a Collection
For Loops
• The typical foreach loop syntax is:
foreach (Type element in collection)
{
statements;
}
• Iterates over all elements of a collection
– The element is the loop variable that takes
sequentially all collection values
– The collection can be list, array or other
group of elements of the same type
foreach Loop – Example
• Example of foreach loop:
string[] days = new string[] {
"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday" };
foreach (String day in days)
{
Console.WriteLine(day);
}