You are on page 1of 28

PROGRAMMING FUNDAMENTALS

Fall 2017
Engr. Muhammad Raees
raees.se@must.edu.pk

Department of Software Engineering, MUST. Mirpur AJ&K 1


▪ Repetition
▪ While loop
▪ For loop
▪ Goto statement

Department of Software Engineering, MUST. Mirpur AJ&K 2


▪ What you need is a way for the computer to execute the same
short sequence of instructions multiple times — known as a loop.

▪ There are two constructs in most programming languages for loops


– knows as while loop and for loop.

▪ Some languages have variation of while as do-while and for as for-


each construct.

Department of Software Engineering, MUST. Mirpur AJ&K 3


▪ keyword while introduces the most basic form of
execution loop

while(bool-expression)
{
//repeatedly executed as long as the expression is true.
}

Department of Software Engineering, MUST. Mirpur AJ&K 4


▪ When the while is first encountered, the bool expression is
evaluated. If the expression is true, the code within the block is
executed.
▪ Control passes beyond the closed brace the first time the bool
expression is evaluated and turns out to be false.
▪ If the condition is not true the first time the while loop is
encountered, the set of commands within the braces is never
executed.

Department of Software Engineering, MUST. Mirpur AJ&K 5


You can use the while loop to create the program,
a looping version of the Calculate-Interest
program.

Department of Software Engineering, MUST. Mirpur AJ&K 6


Department of Software Engineering, MUST. Mirpur AJ&K 7
Warning
When writing while loops, don’t forget to increment the
counting variable, as I did in this example:

int nYear = 1;
while (nYear < 10)
{
// . . . whatever . . .
}

Department of Software Engineering, MUST. Mirpur AJ&K 8


Without the increment, year is always 1 and the
program loops forever. The only way to exit this
infinite loop is to terminate the program or
reboot.

Make sure that the terminating condition can be


satisfied. Usually, this means your counting
variable is being incremented properly. Otherwise,
you’re looking at an infinite loop, an angry user,
bad press, and 50 years of drought.

Infinite loops are a common mistake, so don’t be


embarrassed when you get caught in one.

Department of Software Engineering, MUST. Mirpur AJ&K 9


▪ A variation of the while loop is the do . . . while loop. In this example,
the condition isn’t checked until the end of the loop:

int year = 1;
do
{
// . . . some calculation . . .
year = year + 1;
} while (year < duration);
Department of Software Engineering, MUST. Mirpur AJ&K 10
▪ In contrast to the while loop, the do . . . while loop is executed at
least once, regardless of the value of duration.

Department of Software Engineering, MUST. Mirpur AJ&K 11


▪ You can use two special commands to bail out of a loop: break and
continue.
▪ Executing the break command causes control to pass to the first
expression immediately following the loop.
▪ The similar continue command passes control straight back up to
the conditional expression at the top of the loop to start over and
get it right this time.

Department of Software Engineering, MUST. Mirpur AJ&K 12


▪A combination of while and break enables the
program to be a little more flexible.

▪ The program continues to loop until the user


enters the correct input. (In the worst case, the
program could loop until an obtuse user dies of
old age.)

Department of Software Engineering, MUST. Mirpur AJ&K 13


▪ Brackets define the scope – (there are other ways too)

int num = 1;
while(num < largerNum)
{
int average = value / num;
// . . . some series of commands . . .
num++;
}

Department of Software Engineering, MUST. Mirpur AJ&K 14


▪ The while loop is the simplest and second most commonly used
looping structure in C# compared to the for loop.
▪ The for loop has this structure:

for(initExpression; condition; incrementExpression)


{
// . . . body of code . . .
}

Department of Software Engineering, MUST. Mirpur AJ&K 15


Department of Software Engineering, MUST. Mirpur AJ&K 16
▪ Why do you need the for loop?
▪ The short answer is that you don’t — the for loop doesn’t bring
anything new
▪ However, the for loop exist for convenience — and to clearly establish
the three parts that every loop should have: the setup, exit criteria, and
increment.
▪ Not only is this arrangement easier to read, but it’s also easier to get
right. (Remember that the most common mistakes in a while loop are
forgetting to increment the counting variable and failing to provide the
proper exit criteria.)

Department of Software Engineering, MUST. Mirpur AJ&K 17


for( . . .some condition . . .)
{
for( . . .some other condition . . .)
{
// . . . do whatever . . .
}
}

Department of Software Engineering, MUST. Mirpur AJ&K 18


Department of Software Engineering, MUST. Mirpur AJ&K 19
Department of Software Engineering, MUST. Mirpur AJ&K 20
▪ You can transfer control in an unstructured fashion by using the
goto statement.
▪ It’s followed by one of these items:
▪ A label
▪ A case in a switch statement
▪ The keyword default (the default clause of a switch statement)

Department of Software Engineering, MUST. Mirpur AJ&K 21


▪ The idea behind the latter two items is to “jump” from one case to another.
▪ This snippet demonstrates how the goto statement is used:
// If the condition is true . . .
if (a > b)
{
// . . . control passes unconditionally from the goto to the label.
goto exitLabel;
}
// . . . whatever other code goes here . . .
exitLabel:
// Control continues here.

Department of Software Engineering, MUST. Mirpur AJ&K 22


▪ The goto statement is unpopular for the very reason that makes it
such a powerful control.
▪ It is almost completely unstructured.
▪ Tracking the flow of control through anything larger than a trivial
piece of code can be difficult if you use goto. (Can you say
“spaghetti code”?)

Department of Software Engineering, MUST. Mirpur AJ&K 23


▪ The process of finding the largest number (i.e., the maximum of a
group of numbers) is used frequently in computer applications.
Write a program and then a program that inputs a series of 10
numbers and determines and prints the largest of the numbers.
▪ Write a program that uses looping to print the following table of
values.

Department of Software Engineering, MUST. Mirpur AJ&K 24


▪ Write a program that reads in the side of a square and then prints
that square out of asterisks. Your program should work for squares
of all side sizes between 1 and 20. For example,
▪ if your program reads a size of 4, it should print

▪ How can you determine how fast your own computer really
operates? Write a program with a while loop that counts from 1 to
300,000,000 by 1s. Every time the count reaches a multiple of
100,000,000, print that number on the screen. Use your watch to
time how long each 100 million repetitions of the loop takes.

Department of Software Engineering, MUST. Mirpur AJ&K 25


▪ Write a program that prints 100 asterisks, one at a time. After every
tenth asterisk, your program should print a newline character. [Hint:
Count from 1 to 100. Use the remainder operator to recognize each
time the counter reaches a multiple of 10.]
▪ Write a program that reads a nonnegative integer and computes and
prints its factorial
▪ World Population Growth Calculator) Use the web to determine the
current world population and the annual world population growth rate.
Write an application that inputs these values, then displays the
estimated world population after one, two, three, four and five years.

Department of Software Engineering, MUST. Mirpur AJ&K 26


▪ (Target-Heart-Rate Calculator) While exercising, you can use a
heart-rate monitor to see that your heart rate stays within a safe
range suggested by your trainers and doctors. According to the
American Heart Association (AHA) the formula for calculating your
maximum heart rate in beats per minute is 220 minus your age in
years. Your target heart rate is a range that is 50–85% of your
maximum heart rate. [Maximum and target heart rates may vary
based on the health, fitness and gender of the individual.] Create a
program that reads the user’s birthday. Your program should
calculate and display the person’s age (in years), the person’s
maximum heart rate and the person’s target heart rate range.

Department of Software Engineering, MUST. Mirpur AJ&K 27


▪ Next, collections

▪ Reading and Practice


▪ Slides
▪ Chapter 3-repetition
▪ Thing to do

Department of Software Engineering, MUST. Mirpur AJ&K 28

You might also like