FLOW OF CONTROLThe flow of control jumps from one part of theprogram to another,depending on calculationsperformed in the ...
FLOW OF CONTROL                          STATEMENTSCompound                                                Simple Selectio...
 Statements are the instructions given to the computer to perform  any kind of action , be it data movements, be it makin...
Sequence = The sequence construct means the statements are  being executed sequentially. This represents the default flow ...
Selection = The selection construct means the execution of   statements depending upon a condition evaluates true, a set o...
if-else Statement Syntax• Formal syntax:  if (<boolean_expression>)       <yes_statement>  else       <no_statement>• Note...
Compound Statement in            Action• Note indenting in this example:  if (myScore > yourScore)  {      cout << "I win!...
Nested Statements• if-else statements contain smaller  statements  – Compound or simple statements (we’ve seen)  – Can als...
The switch Statement A new statement for controlling multiple branches Uses controlling expression which returns bool da...
The switch: multiple case                      labelsExecution "falls through" until break   switch provides a "point of e...
Switch Pitfalls/TipForgetting the break;   No compiler error   Execution simply "falls through" other cases until break;Bi...
LOOPLoops cause a section of your program to be repeateda certain number of times. The repetition continues while a condit...
Iteration = The iteration construct means repetition of a set  of statements depending upon a condition - test. Till the  ...
3 Types of loops in C++       for           Natural "counting" loop      do-while           Least flexible           Alw...
For Loop Syntaxfor (Init_Action; Bool_Exp; Update_Action)       Body_StatementLike if-else, Body_Statement can bea block s...
For Loop Examplefor (count=0;count<3;count++){       cout << "Hi "; // Loop Body}How many times does loop body execute?I...
While Loop Syntax
While Loop Example Consider:count = 0;              // Initializationwhile (count < 3)       // Loop Condition{       cout...
Do while Loop Syntax
Do while Loop Examplecount = 0;         // Initializationdo{      cout << "Hi ";      // Loop Body      count++;          ...
Loop IssuesLoop’s condition expression can be ANYboolean expression.Examples:      while (count<3 && done!=0)  {      // D...
Loop Pitfalls: Misplaced ;Watch the misplaced ; (semicolon)  Example:  while (response != 0) ;  {     cout << "Enter val: ...
Loop Pitfalls: Infinite LoopsLoop condition must evaluate to false atsome iteration through loop   If not  infinite loop....
The break and continue StatementFlow of Control  Recall how loops provide "graceful" and  clear flow of control in and out...
Nested LoopsRecall: ANY valid C++ statements can beinside body of loopThis includes additional loop statements!   Called "...
Control statements
Control statements
Control statements
Upcoming SlideShare
Loading in...5
×

Control statements

3,277

Published on

Name-Kanwalpreet Kaur
Roll No.-115338
D1 CSE (evening)
Group- N2
GNDEC

Published in: Education, Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,277
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
232
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Control statements

  1. 1. FLOW OF CONTROLThe flow of control jumps from one part of theprogram to another,depending on calculationsperformed in the program.Program statements that cause such jumps are called control statements. There are two majorcategories: loops and decisions.
  2. 2. FLOW OF CONTROL STATEMENTSCompound Simple Selection/Decision Iteration/LoopFL else Switch care For While Do-While Jump statementsBreak Continue Go to Return Exit
  3. 3.  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. Statements are the smallest executing unit of a C++ program. COMPOUND STATEMENT (BLOCK ) A compound statement in C++ is a sequence of statements enclosed by a pair of branches { }. { statements 1 ; statements 2 ; : } represents a compound statement
  4. 4. 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
  5. 5. Selection = The selection construct means the execution of statements depending upon a condition evaluates true, a set of statements is followed. A set of statements true Condition Statement 1 Statement 2 ? Statement 1Anotherset ofstatement Statement 2 The selection construction
  6. 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 in either branch  use compound statement
  7. 7. Compound Statement in Action• 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; }
  8. 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!
  9. 9. The switch Statement A new statement for controlling multiple branches Uses controlling expression which returns bool data type (true or false)
  10. 10. The switch: multiple case labelsExecution "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"
  11. 11. Switch Pitfalls/TipForgetting 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
  12. 12. LOOPLoops cause a section of your program to be repeateda 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 statementsfollowing the loop.
  13. 13. 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 }, False Condition ? The exit condition True Statement 1 The loop body Statement 2 The interaction construct
  14. 14. 3 Types of loops in C++ for Natural "counting" loop  do-while Least flexible Always executes loop body at least on  while Most flexible No “restrictions”
  15. 15. For Loop Syntaxfor (Init_Action; Bool_Exp; Update_Action) Body_StatementLike if-else, Body_Statement can bea block statement
  16. 16. For Loop Examplefor (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
  17. 17. While Loop Syntax
  18. 18. While Loop Example Consider:count = 0; // Initializationwhile (count < 3) // Loop Condition{ cout << "Hi "; // Loop Body count++; // Update expression} Loop body executes how many times?
  19. 19. Do while Loop Syntax
  20. 20. Do while Loop Examplecount = 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!
  21. 21. Loop IssuesLoop’s condition expression can be ANYboolean expression.Examples: while (count<3 && done!=0) { // Do something } for (index=0;index<10 && entry!=-99) { // Do something }
  22. 22. 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!
  23. 23. Loop Pitfalls: Infinite LoopsLoop 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!
  24. 24. The break and continue StatementFlow of Control Recall how loops provide "graceful" and clear flow of control in and out In RARE instances, can alter natural flowbreak; Forces loop to exit immediately.continue; Skips rest of loop bodyThese statements violate natural flow Only used when absolutely necessary!
  25. 25. Nested LoopsRecall: ANY valid C++ statements can beinside body of loopThis 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×