Control Constructs, While, Do While-Programming and Computer Architecture-Lecture Slides, Slides of Computer Architecture and Organization

Chanchal Mahanthapa delivered this lecture at Chandra Shekhar Azad University of Agriculture and Technology for Programming and Computer Architecture. It includes: Control, Constructs, While, Do-while, Repetition, Structure, Pseudocode, Flowchart, Controlled

Typology: Slides

2011/2012

Uploaded on 07/13/2012

ekraj
ekraj 🇮🇳

4.1

(11)

123 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Programming
Control constructs, while, do-while,
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Control Constructs, While, Do While-Programming and Computer Architecture-Lecture Slides and more Slides Computer Architecture and Organization in PDF only on Docsity!

Computer ProgrammingControl constructs, while, do-while,

while Repetition Structure^ ^ Repetition structure^ ◦^

Action repeated while some condition remainstrue ◦ Pseudocode^ while there are more items on my shopping listPurchase next item and cross it off my list ◦ while

loop repeated until condition becomes

false  Example^ int product = 2;while ( product <= 1000 )

product = product * 2;

Formulating Algorithms (Counter-Controlled Repetition)^ ^ Counter-controlled repetition^ ◦^

Loop repeated until counter reaches certainvalue  Definite repetition ◦ Number of repetitions known  Example^ A class of ten students took a quiz.The grades(integers in the range 0 to 100) for this quiz areavailable to you. Determine the class average onthe quiz.

Formulating Algorithms (Counter-Controlled Repetition)^ ^ Pseudocode for example:

Set total to zeroSet grade counter to oneWhile grade counter is less than or equal to tenInput the next gradeAdd the grade into the totalAdd one to the grade counterSet the class average to the total divided by tenPrint the class average  Next: C++ code for this example

21 // processing phase 22 while ( gradeCounter <= 10 ) {

// loop 10 times

23 cout << "Enter grade: ";

// prompt for input

24 cin >> grade;

// read grade from user

25 total = total + grade;

// add grade to total

26 gradeCounter = gradeCounter + 1; // increment counter 27 } 2829 // termination phase 30 average = total / 10;

// integer division

3132 // display result 33 cout << "Class average is " << average << endl; 3435 return 0;

// indicate program ended successfully 3637 } // end function main Enter^ grade:

Enter^ grade:

Enter^ grade:

Enter^ grade:

Enter^ grade:

Enter^ grade:

Enter^ grade:

Enter^ grade:

Enter^ grade:

Enter^ grade:

Class^ average

is^81

The counter gets incrementedeach time the loop executes.Eventually, the counter causesthe loop to end.

Formulating Algorithms (Sentinel-Controlled Repetition)^ ^ Suppose problem becomes:

Develop a class-averaging program that will processan random number of grades each time the programis run ◦ Unknown number of students ◦ How will program know when to end?  Sentinel value ◦ Indicates “end of data entry” ◦ Loop ends when sentinel input ◦ Sentinel chosen so it cannot be confused withregular input^ ^ -1 in this case

1 // code2.cpp 2 // Class average program with sentinel-controlled repetition. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 using std::fixed; 9 10 #include

// parameterized stream manipulators 1112 using std::setprecision; // sets numeric output precision 1314 // function main begins program execution 15 int main() 16 { 17 int total;

// sum of grades 18 int gradeCounter; // number of grades entered 19 int grade;

// grade value 2021 double average;

// number with decimal point for average 2223 // initialization phase 24 total = 0;

// initialize total 25 gradeCounter = 0; // initialize loop counter

Data type

double

used to represent

floating point numbers.

2627 // processing phase 28 // get first grade from user 29 cout << "Enter grade, -1 to end: "; // prompt for input 30 cin >> grade;

// read grade from user

3132 // loop until sentinel value read from user 33 while ( grade != -1 ) { 34 total = total + grade;

// add grade to total

35 gradeCounter = gradeCounter + 1; // increment counter 3637 cout << "Enter grade, -1 to end: "; // prompt for input 38 cin >> grade;

// read next grade

3940 } // end while 4142 // termination phase 43 // if user entered at least one grade ... 44 if ( gradeCounter != 0 ) { 4546 // calculate average of all grades entered 47 average = static_cast ( total ) / gradeCounter; 48

static_cast()

treats^

total

as a

double

temporarily (casting).

Required because dividing two integers truncates theremainder. gradeCounter

is an^ int

, but it gets

promoted

to

double

Nested Control Structures^ ^ Problem statement

A college has a list of test results (1 = pass, 2 = fail)for 10 students. Write a program that analyzes theresults. If more than 8 students pass, print "RaiseTuition".

^ Notice that^ ◦^ Program processes 10 results^ 

Fixed number, use counter-controlled loop ◦ Two counters can be used  One counts number that passed  Another counts number that fail ◦ Each test result is 1 or 2  If not 1, assume 2

Nested Control Structures^ ^ Top level outline

Analyze exam results and decide if tuition should be raised  First refinement^ Initialize variablesInput the ten quiz grades and count passes and failuresPrint a summary of the exam results and decide if tuitionshould be raised  Refine^ Initialize variables^ to^ Initialize passes to zeroInitialize failures to zeroInitialize student counter to one

Nested Control Structures^ ^ Refine

Print a summary of the exam results and decide if tuitionshould be raised

to

Print the number of passesPrint the number of failuresIf more than eight students passedPrint “Raise tuition”  Program next

1 // code3.cpp 2 // Analysis of examination results. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 // function main begins program execution 10 int main() 11 { 12 // initialize variables in declarations 13 int passes = 0;

// number of passes 14 int failures = 0;

// number of failures 15 int studentCounter = 1;

// student counter

16 int result;

// one exam result 1718 // process 10 students using counter-controlled loop 19 while ( studentCounter <= 10 ) { 2021 // prompt user for input and obtain value from user 22 cout << "Enter result (1 = pass, 2 = fail): "; 23 cin >> result; 24

Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Passed 6Failed 4Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Passed 9Failed 1Raise tuition

do/while Repetition Structure^ ^ Similar to

while

structure

◦^ Makes loop continuation test at end, notbeginning ◦^ Loop body executes at least once  Format^ do {

statement} while ( condition );

true false action(s) condition