Flow of control

50 %
50 %
Information about Flow of control

Published on November 11, 2008

Author: namita_sahu23

Source: authorstream.com

Slide 1: FLOW OF CONTROL The flow of control jumps from one part of the program to another,depending on calculations performed in the program. Program statements that cause such jumps are called control statements. There are two major categories: loops and decisions. Slide 2: STATEMENTS Compound Simple Selection/Decision Iteration/Loop Flelse Switch care For While Do-While Jump statements Break Continue Go to Return Exit FLOW OF CONTROL statements : statements Statements are the instructions given to the computer to perform any kind of action , be it data movements, be it making decisions or be it repeating actions. COMPOUND STATEMENT (BLOCK ) A compound statement in C++ is a requence of statements enclosed by a pair of branches { }. { statements 1 ; statements 2 ; : : } represents a compound statement Statement flow control : Statement flow control Sequence = The sequence construct means the statements are being executed sequentially. This represents the default flow of statement. STATEMENT 1 STATEMENT 2 STATEMENT 3 The sequence construct Slide 5: Selection = The selection construct means the execution of statements depending upon a condition evaluates true, a set of statements is followed. Condition ? Statement 1 Statement 2 Statement 1 Statement 2 true A set of statements Another set of statement The selection construction Slide 6: if-else Statement Syntax Formal syntax:if (<boolean_expression>) <yes_statement>else <no_statement> Note each alternative is only ONE statement! To have multiple statements execute ineither branch  use compound statement Slide 7: Note indenting in this example:if (myScore > yourScore){ cout << "I win!\n"; wager = wager + 100;}else{ cout << "I wish these were golf scores.\n"; wager = 0;} Compound Statement in Action Slide 8: Nested Statements if-else statements contain smaller statements Compound or simple statements (we’ve seen) Can also contain any statement at all, including another if-else stmt! Example:if (speed > 55) if (speed > 80) cout << "You’re really speeding!"; else cout << "You’re speeding."; Note proper indenting! Slide 9: The switch Statement A new stmt for controlling multiple branches Uses controlling expression which returns bool data type (true or false) Slide 11: The switch: multiple case labels Execution "falls through" until break switch provides a "point of entry" Example:case "A":case "a": cout << "Excellent: you got an "A"!\n"; break;case "B":case "b": cout << "Good: you got a "B"!\n"; break; Note multiple labels provide same "entry" Slide 12: switch Pitfalls/Tip Forgetting the break; No compiler error Execution simply "falls through" other cases until break; Biggest use: MENUs Provides clearer "big-picture" view Shows menu structure effectively Each branch is one menu choice Slide 13: Loops cause a section of your program to be repeated a certain number of times. The repetition continues while a condition is true. When the condition becomes false, the loop ends and control passes to the statements following the loop. LOOP Slide 14: Iteration = The iteration construct means repetition of a set of statements depending upon a condition - test. Till the time a condition true { or false depending upon the loop }, a set-of-statements are repeated again and again. As soon as the condition becomes false { or true }, Condition ? Statement 1 Statement 2 The loop body False The exit condition True The interaction construct Slide 15: 3 Types of loops in C++ for Natural "counting" loop do-while Least flexible Always executes loop body at least once while Most flexible No "restrictions“ Slide 16: For Loop Syntax for (Init_Action; Bool_Exp; Update_Action) Body_Statement Like if-else, Body_Statement can bea block statement Slide 17: For Loop Example for (count=0;count<3;count++) { cout << "Hi "; // Loop Body} How many times does loop body execute? Initialization, loop condition and update all"built into" the for-loop structure! A natural "counting" loop Slide 18: while Loop Syntax Slide 19: while Loop Example Consider:count = 0; // Initializationwhile (count < 3) // Loop Condition{ cout << "Hi "; // Loop Body count++; // Update expression} Loop body executes how many times? Slide 20: Do while Loop Syntax Slide 21: Do while Loop Example count = 0; // Initializationdo { cout << "Hi "; // Loop Body count++; // Update expression} while (count < 3); // Loop Condition Loop body executes how many times? do-while loops always execute body at least once! Slide 22: Loop Issues Loop’s condition expression can be ANY boolean expression Examples: while (count<3 && done!=0){ // Do something} for (index=0;index<10 && entry!=-99){ // Do something} Slide 23: Loop Pitfalls: Misplaced ; Watch the misplaced ; (semicolon) Example:while (response != 0) ;{ cout << "Enter val: "; cin >> response;} Notice the ";" after the while condition! Result here: INFINITE LOOP! Slide 24: Loop Pitfalls: Infinite Loops Loop condition must evaluate to false atsome iteration through loop If not  infinite loop. Example:while (1){ cout << "Hello ";} A perfectly legal C++ loop  always infinite! Slide 25: The break and continue Statement Flow of Control Recall how loops provide "graceful" and clear flow of control in and out In RARE instances, can alter natural flow break; Forces loop to exit immediately. continue; Skips rest of loop body These statements violate natural flow Only used when absolutely necessary! Slide 26: Nested Loops Recall: ANY valid C++ statements can beinside body of loop This includes additional loop statements! Called "nested loops" Requires careful indenting:for (outer=0; outer<5; outer++) for (inner=7; inner>2; inner--) cout << outer << inner; Notice no { } since each body is one statement Good style dictates we use { } anyway

Add a comment

Related presentations

Related pages

Datenflusssteuerung – Wikipedia

Mit Datenflusssteuerung (engl. data flow control) werden unterschiedliche Verfahren bezeichnet, mit denen die Datenübertragung von Endgeräten an einem ...
Read more

dict.cc | flow control | Wörterbuch Englisch-Deutsch

Übersetzung für flow control im Englisch-Deutsch-Wörterbuch dict.cc.
Read more

Flow Control

About Flow Control Industries. Flow Control Industries, Inc. (FCI) is a specialty manufacturer of high-performance pressure independent control valves ...
Read more

Flow control (data) - Wikipedia

In data communications, flow control is the process of managing the rate of data transmission between two nodes to prevent a fast sender from overwhelming ...
Read more

Flow Control aktivieren oder nicht - Windows Forum — LAN ...

Flow Control aktivieren oder nicht - geschrieben in Forum Windows Forum — LAN & WAN: Hallo, macht es Sinn, Flow Control im Switch und in den ...
Read more

Flusskontrolle :: flow control :: ITWissen.info

Die Flusskontrolle ist eine Funktion von Kommunikationsprotokollen zur Anpassung der Übertragungsgeschwindigkeit asynchron arbeitender Endgeräte an die ...
Read more

Flow Control | HORA

Flow Control Individuelle Lösungen auf der Basis optimierter Produkte. Flow Control - der neue Name des HORA-Geschäftsbereichs ist Programm: Mit ...
Read more

Control flow - Wikipedia

The kinds of control flow statements supported by different languages vary, but can be categorized by their effect: continuation at a different statement ...
Read more

dict.cc Wörterbuch :: flow control :: Deutsch-Englisch ...

Englisch-Deutsch-Übersetzung für flow control im Online-Wörterbuch dict.cc (Deutschwörterbuch).
Read more


Herzlich Willkommen bei SED Flow Control - Ihr Partner für professionelle Ventiltechnologien. Wir sind ein führendes Unternehmen in der Entwicklung, der ...
Read more