Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Problem Solving and Structured Programming, Slides of Advanced Computer Programming

During the course of work of the programming, we learn the core of the programming. The main points disucss in these lecture slides are:Additional Control Structures, Switch Statement, Multi-Way Branching, Do-While Statement, Do-While Statement, Break and Continue Statements, Selection Control Structure, Value of Integral Expression, Logical Errors

Typology: Slides

2012/2013

Uploaded on 04/24/2013

banamala
banamala 🇮🇳

4.4

(19)

120 documents

1 / 40

Toggle sidebar

Related documents


Partial preview of the text

Download Problem Solving and Structured Programming and more Slides Advanced Computer Programming in PDF only on Docsity! Chapter 9 Additional Control Structures 1 Docsity.com Chapter 9 Topics • Switch Statement for Multi-Way Branching • Do-While Statement for Looping • For Statement for Looping • Using break and continue Statements 2 Docsity.com Switch Statement • The value of IntegralExpression (of char, short, int, long or enum type) determines which branch is executed • Case labels are constant (possibly named) integral expressions • Several case labels can precede a statement 5 Docsity.com Control in Switch Statement • Control branches to the statement following the case label that matches the value of IntegralExpression • Control proceeds through all remaining statements, including the default, unless redirected with break • If no case label matches the value of IntegralExpression, control branches to the default label, if present--otherwise control passes to the statement following the entire switch statement • Forgetting to use break can cause logical errors because after a branch is taken, control proceeds sequentially until either break or the end of the switch statement occurs 6 Docsity.com Do-While Statement Do-While is a looping control structure in which the loop condition is tested after each iteration of the loop SYNTAX do { Statement } while (Expression); Loop body statement can be a single statement or a block 7 Docsity.com Do-While Loop When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the Do-while statement 10 Statement Expression DO WHILE FALSE TRUE Docsity.com For Loop SYNTAX for (initialization; test expression; update) { Zero or more statements to repeat } 11 Docsity.com For loop contains • An initialization • An expression to test for continuing • An update to execute after each iteration of the body 12 Docsity.com Example of Repetition int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; 15 num OUTPUT 1 Docsity.com Example of Repetition int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; 16 num OUTPUT 1 true Docsity.com Example of Repetition int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; 17 num OUTPUT 1 1Potato Docsity.com Example of Repetition int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; 20 num OUTPUT 2 1Potato 2Potato Docsity.com Example of Repetition int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; 21 num OUTPUT 3 1Potato 2Potato Docsity.com Example of Repetition int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; 22 num OUTPUT 3 true 1Potato 2Potato Docsity.com Example of Repetition int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; 25 num OUTPUT 4 false 1Potato 2Potato 3Potato Docsity.com Example of Repetition int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; 26 num When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement 4 false Docsity.com Output 27 The output was 1Potato 2Potato 3Potato Docsity.com Answer 30 ********** The 10 asterisks are all on one line. Why? Docsity.com What output from this loop? int count; for (count = 0; count < 10; count++); { cout << “*”; } 31 Docsity.com Answer • No output from the for loop! Why? • The semicolon after the () means that the body statement is a null statement • In general, the body of the For loop is whatever statement immediately follows the () • That statement can be a single statement, a block, or a null statement • Actually, the code outputs one * after the loop completes counting to 10 32 Docsity.com Guidelines for Choosing Looping Statement • For a simple count-controlled loop, use the For statement • For an event-controlled loop whose body always executes once, use of Do-While statement • For an event-controlled loop about which nothing is known, use a While statement • When in doubt, use a While statement 35 Docsity.com Continue Statement • The Continue statement is valid only within loops • It terminates the current loop iteration, but not the entire loop • In a For or While, Continue causes the rest of the body of the statement to be skipped; in a For statement, the update is done • In a Do-While, the exit condition is tested, and if true, the next loop iteration is begun 36 Docsity.com Problem Given a character, a length, and a width, draw a box For example, given the values ‘&’, 4, and 6, you would display &&&&&& &&&&&& &&&&&& &&&&&& 37 Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved