0% found this document useful (0 votes)
17 views28 pages

Lecture 7

Uploaded by

Sami Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
17 views28 pages

Lecture 7

Uploaded by

Sami Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 28
ELJ25 -- Programming Fundamentals EY Engr. Dr. M. Ghazanfar Uilak® Professor ~ Department of ElectricaEngineering Ret Na ore Ae EET « Usman institute of Technology ~ Spring 2020 Topic Learning outcomes we ee ae . To be able to implem8ntrepetitive structures i.e, loops, a ay er . To be able to construtt syntactically correct loops inds® Or C ae . To be able tatonstruct count-controlled (Definite) sSops o . To be able tatonstruct event-controlled (Indefinite) loops . To besable'to choose the correct type of lodpbr a given problem. . To beable to construct a nested loopin§ structure. & s eo oo eo OaAhRON = g Basic program flows Three Basic program flows a cs ane oe wah a oy o oe ys fh Sequential Stricture r soot Statement’\ex@cuted one after the other in the order igitten 2 Selestign’ Structure sv o o wr 8 ‘fle next statement executed is not hohe one in sequence 3. Repetition Structure yr a" ‘Repeat some actions/processistatement™ ee é ae A ve “ » oe oe < EL- 125 qffamming Fundamentals oe The repetitive contréF’structure of oe 7 i The Repetitive flow — loops v¥ Mechanisms for deciding‘tinder what conditions an action should be rebeated - also known as loops, v Aloop statement, allows us to execute a stateniatr a group of statements multiple times oe vA controhstcture that causes a statemeft or a set of stategrénts to be repeated. Oy iS v Repetition can be thought of as onfpeat an action oyer and over, as long as a cone or set of conditions is satisfied” The repetitive flow — loops Determining the average 4 ols v Suppose we want t algiilate the average apparent bri. htness: of a list of five star ma Site values + Can we do ies s + Yes, it agi ee easy v ae wen a to calculate the averagevspparent brightness of a list of 81479 stars visible from earth s ot ae we do it? -?'Yes, but it would be gruesome watt the use of iteration The repetitive flow — loops Definite and Indefinite loops ae Conceptually, we distin ee 0 types of loops, which differ in tha” way in which the number of rations (i.e., repetitions of tee polly of the loop) is determipsgr® os p a Definite loops: the Giver of iterations is known before woe the execution of the body of, thle J6bp. Example. D repeat for 10 times printing out charactét * we Indefinite ba : the number of iterations is not kndigibefore we start to execute thé body of the loop, but depends on whena certain condition becomeS true (and this depends on what happegs 1 in the body of the loop) Example: while the user does not decidestd quit, play the game. wy ete Train ES structure * 3 Ri The for loop A for loop is a repetition contrdls structure that allows you to ae efficiently write a loop. disieeds to execute a specific numbesot i x se times. oy ‘ we Syntax nto Wg for (initelizabion; condition; increment/decrement oe om xs’Action Example si ” ot sa ~- are operators R= 0; 1 < 3; i++) ot offiat add and subtract one £ m their operands. E.g. oe > num=num+1; cae x cout << "iis"< using namespace std; int main() { int i; float no, avg, su cout<<"Program to cag numbers"< using namespace std; int main( (EER ys int: EN float no, avg, sum=0.0; A ae cout<<"Program to cake =1 W while(i<=5) ie * { og” cousenas Value "< ror Oy? * ot 0 ag g Sx" & wy eo Recommended reading Chapter 3: “i Object-Oriented Programpiig i in C++, Fourth Edition oO Robert Lafore ory ye ye & oe se Oo ve » 9 no x eo » zo) og ae no) i w ge Cy 7, ve ef 9 F a L125 Programming Fundamentals EL 125 Programming Fundamentals

You might also like