MIS Department
Programming Fundamentals
Lec “6”
Prepared by : Dr. Wathq Ahmed Ali Kawelah
Main
Points
Flow Control:
1. The if Statement.
2. The switch Statement.
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 2
3. do Looping.
4. while Loops.
5. Interrupting Loops.
6. Infinite Loops. Examples.
The if Statement
1. The if Statement : is a far more versatile and useful way to make decisions.
The basic structure of a if statement is as follows:
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 3
if ( < test > )
{
< code executed if < test > is true > ;
}
else
{
< code executed if < test > is false > ;
}
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 4
Example
“12”
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 5
Example
Console Applications
using System; using
if (var1 < var2) comparison
System.Collections.Generic; using= "less than"; else { if (var1
System.Linq; using System.Text; == var2) comparison =
"equal to"; else comparison
namespace Example12
= "greater than";
{ class Program }
{ static void Main(string[] args)Console.WriteLine("The first number is {0}
{string comparison; the second number.",comparison);
Console.WriteLine("Enter a number:"Console.ReadKey();}}}
double var1 = Convert.ToDouble(Console
Console.WriteLine("Enter another number:"
var2 = Convert.ToDouble(Console.ReadLine());
Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 6
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 7
Example
The switch Statement
1. The switch Statement : is similar to the if statement in that it executes code
conditionally based on the value of a test.
The basic structure of a switch statement is as follows:
Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 8
switch ( < testVar > )
{ case < comparisonVal1 > :
< code to execute if < testVar > == < comparisonVal1
>> break; case < comparisonVal2 > :
< code to execute if < testVar > == < comparisonVal2 >>
break;
... case <
comparisonValN > :
< code to execute if < testVar > == < comparisonValN
>> break; default:
< code to execute if < testVar > != comparisonVals >
break; }
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 9
Example
using System;
using System.Collections.Generic;
“13”
using System.Linq; using
System.Text; namespace switch (name.ToLower()) {case
Example13 myName:
{ class Program Console.WriteLine("You have the same name as me!");
break; case niceName:
{ static void Main(string[] args)
Console.WriteLine("My, what a nice name you have!");
{const string myName = “ali"; const break; case badName:
string niceName = "numberone"; Console.WriteLine("That’s a very bad name."); break;}
const string badName = "smallman"; Console.WriteLine("Hello {0}!", name);
string name; Console.ReadKey();}}}
Console.WriteLine("What is your name?");
name = Console.ReadLine(); Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 10
Console Applications
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 11
Example
11/28/2023
Looping
• Looping is refers to the repeated execution of statements.
1. do loop : The code you have marked out for looping is executed, a Boolean test is
perform he structure of a do loop is as follows, where < Test
Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 12
> evaluates to a Boolean value:ed, and the code
executes again if this test evaluates to true , and so on. When the test evaluates to
false , do the loop exits.
T {
< code to be looped >
11/28/2023 } while ( < Test >Prepared
); by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 13
Example
“14”
Console Applications
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 14
using System; Convert.ToDouble(Console.ReadLine()) /
100.0; Console.WriteLine("What balance would
using System.Collections.Generic;
you like to have?"); targetBalance =
using System.Linq; using
Convert.ToDouble(Console.ReadLine());
System.Text; namespace
int totalYears = 0; do { balance *=
Example14 interestRate;
{ class Program ++totalYears; }
while (balance < targetBalance);
{ static void Main(string[] args)
Console.WriteLine("In {0} year{1} you’ll have a
{double balance, interestRate, targetBalance;
balance of {2}.",totalYears, totalYears == 1 ?
Console.WriteLine("What is "" : "s",balance);
your current
balance?"); balance Console.ReadKey();=
Convert.ToDouble(Console.ReadLine()); }}}
Console.WriteLine("What is your current annual
interest rate (in %)?"); interestRate = 1 +
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 15
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 16
while Loops
1. while loops : are very similar to do loops, but they have one important difference.
Here’ s how while loops are specified:
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 17
while ( < Test > )
{
< code to be looped >
}
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 18
Example “15”
Console Applications
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 19
using System; Convert.ToDouble(Console.ReadLine()) /
100.0; Console.WriteLine("What balance would
using System.Collections.Generic;
you like to have?"); targetBalance =
using System.Linq; using
Convert.ToDouble(Console.ReadLine()); int
System.Text; namespace
totalYears = 0;
Example15 while (balance < targetBalance) { balance
{ class Program *= interestRate;
++totalYears; }
{ static void Main(string[] args)
while (balance < targetBalance);
{double balance, interestRate, targetBalance;
Console.WriteLine("In {0} year{1} you’ll have a
Console.WriteLine("What is your
balance current
of {2}.",totalYears, totalYears == 1 ?
balance?"); balance "" : "s",balance);
=
Convert.ToDouble(Console.ReadLine()); Console.ReadKey();
Console.WriteLine("What is your current }}} annual
interest rate (in %)?"); interestRate = 1 +
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 20
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 21
for Loops
• for Loops: this type of loop executes a set number of times and maintains its own
counter. To define a for loop you need the following information:
1. A starting value to initialize the counter variable.
2. A condition for continuing the loop, involving the counter variable.
3. An operation to perform on the counter variable at the end of each loop cycle. This
information must be placed into the structure of a for loop as follows:
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 22
for ( < initialization > ; < condition > ; < operation > )
{
< code to loop
> }
Interrupting Loops
• Sometimes you want more control over the processing of looping code.
• C# provides four commands to help you here.
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 23
1. (goto) : Allows jumping out of a loop to a labeled position (not recommended if you
want your code to be easy to read and understand).
2. (break) : Causes the loop to end immediately.
3. (continue) : Causes the current loop cycle to end immediately
(execution continues with the next loop cycle).
1. (return) : Jumps out of the loop and its containing function.
Interrupting Loops
Example “17”
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 24
int i = 1; while int i = 1; while
(i <= 10) (i <= 10)
{ if (i == { if (i == 6)
6) break; goto
Console.WriteLine("{0}" exitPoint;
Console.WriteLine("{0}", i++);
}
Console.WriteLine("This code will never be reached."); exitPoint:
int i;
Console.WriteLine("This code is run when the loop is exited using goto.");
for (i = 1; i <= 10; i++)
{
if ((i % 2) == 0) continue
Console.WriteLine(i);
}
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 25
Infinite Loops
• Infinite loops: through both coding errors and design, to define loops that never end.
• Here’ s how Infinite Loops are specified:
while (true) int i = 1; while
{ (i <= 10)
// code in loop {
} if ((i % 2) == 0) continue;
Console.WriteLine("{0}", i++);
}
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 26
11/28/2023 Prepared by : Dr. Wathq Ahmed Ali Kawelah Wathq@2023 27