Chapter 10: Control Structures1 Chapter 10 Control Structures.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Chapter 5: Control Structures II (Repetition)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 4: Control Structures II
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
JavaScript: Control Statements I
MSIS 655 Advanced Business Applications Programming
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 4 - Program Control
Chapter 4 - Program Control
Chap 7. Advanced Control Statements in Java
Flow of Control.
Presentation transcript:

Chapter 10: Control Structures1 Chapter 10 Control Structures

Chapter 10Flow of Control2 qSequence qSelection qRepetition condition YesNo action-1action-2condition No action Yes Selection structure Repetition structure

Chapter 10Selection: ‘if’ construct3 qSyntax:if (expression) statement ; qexpression is the condition for the ‘if’ construct. qIf expression is evaluated to non-zero (true), statement is executed. qIf expression is evaluated to zero (false), statement is skipped.

Chapter 10'if’ construct4 ‘if’ construct qSyntax:if (expression) statement ;

Chapter 10Compound-statement Action5 qSyntax:if (expression) { compound-statement ; }

Chapter 10Flags6 qFlag: an integer variable that simulates a Boolean variable. Contains 0 (false) or 1 (true). or

Chapter 10Naming a Flag7 qAppropriate naming is desirable. qExample: a flag ‘attended’ implies u‘attended’ if it contains 1 (true) u‘did not attend’ if it contains 0 (false)

Chapter 10Nested ‘if’ statement8 qExample: qAbove could be rewritten as:

Chapter 10Short-circuit evaluation9 qEvaluation stops as soon as value of expression is known. Evaluation is from left to right. qFor logical AND (&&), if the partial expression is false, the whole expression is false.  For logical OR (||), if the partial expression is true, the whole expression is true.

Chapter 10Short-circuit evaluation10 Short-circuit evaluation qExample:

Chapter 10Complementing a Condition11 Complementing a Condition qComlementing or negating a logical expression means changing the polarity. qExamples: !(a == 30) is equivalent to (a != 30) !(a > b) is equivalent to (a <= b)

Chapter 10DeMorgan’s Theorem12 DeMorgan’s Theorem qNOT(a AND b) same as NOT(a) OR NOT (b) NOT(a OR b) same as NOT(a) AND NOT(b) qIn C: !(expr1 && expr2) same as !(expr1) || !(expr2) !(expr1 || expr2) same as !(expr1) && !(expr2)

Chapter 10DeMorgan’s Theorem13 DeMorgan’s Theorem qExample:

Chapter 10Common Mistakes14 Common Mistakes qDo not use == and != on floating-point numbers. qMixing up == with =. qWrong placament of semi-colon, resulting in empty statement: printf() is outside ‘if’ construct. 

Chapter 10Common Mistakes15 Common Mistakes qTranslating condition in English to C: qLet lower be 10, and upper be 30. If x is 20, (lower <= x) is true, so it is evaluated to 1. Since (1 <= upper) is also true, condition is true! 

Chapter 10Common Mistakes16 Common Mistakes qWrong condition: qCorrect method:  which is equivalent to this (since <= has a higher precedence than &&): 

Chapter 10Common Mistakes17 Common Mistakes qWrong condition: qCorrect method:  

Chapter 10Common Mistakes18 Common Mistakes qForgetting the braces for compound statements: qCorrect method:  

Chapter 10‘if-else’ construct19 ‘if-else’ construct qSyntax: if (expression) statement1 ; else statement2 ; if (expression) { compound-statement1 ; } else { compound- statement2 ; }

Chapter 10‘if-else’ construct20 ‘if-else’ construct qMay be used to avoid redundant code: qUse ‘if-else’ construct:  

Chapter 10‘if-else’ construct21 ‘if-else’ construct qAnother example:

Chapter 10Style22 Style qTwo common styles: if (expression) { compound-statement1 ; } else { compound- statement2 ; } if (expression) { compound-statement1 ; } else { compound- statement2 ; }

Chapter 10Removing common statements23 Removing common statements qCommon statements in the ‘then’ and ‘else’ parts should be moved out of the ‘if’ construct, if appropriate: 

Chapter 10Removing common statements24 Removing common statements qAfter moving common statements out of ‘if’ construct: 

Chapter 10Logical assignment for Flags25 Logical assignment for Flags qExample: ‘if-else’ statement may be replaced by an assignment statement.

Chapter 10Logical assignment for Flags26 Logical assignment for Flags qAnother example:

Chapter 10Nested ‘if-else’ statements27 Nested ‘if-else’ statements qExample:

Chapter 10Nested ‘if-else’ statements28 Nested ‘if-else’ statements qWhich ‘if’ is the ‘else’ associated with? q‘else’ is associated with nearest ‘if’.

Chapter 10Nested ‘if-else’ statements29 Nested ‘if-else’ statements qTo override default association, use braces to mark out block.

Chapter 10Nested ‘if-else’ statements30 Nested ‘if-else’ statements qExample:  

Chapter 10Nested ‘if-else’ statements31 Nested ‘if-else’ statements qExample:

Chapter 10Common Mistakes32 Common Mistakes qWrong matching of ‘else’ with ‘if’. qWrong placement of semi-colon. 

Chapter 10Conditional Operator (?:)33 Conditional Operator (?:) qTernary operator: condition ? expr1 : expr2 qFirst operand is condition. qIf condition is true, take value of expr1; otherwise, take value of expr2.

Chapter 10Conditional Operator (?:)34 Conditional Operator (?:) qExample: equivalent to:

Chapter 10Conditional Operator (?:)35 Conditional Operator (?:) qExample: equivalent to:

Chapter 10‘switch’ construct36 ‘switch’ construct qMulti-way selection statement:

Chapter 10‘switch’ construct37 ‘switch’ construct qMay only test constant integral expressions, i.e., expressions that evaluate to integers or characters. qThe v i ’s are integral values; the s i ’s are compound statements. qAfter expression is evaluated, control jumps to appropriate ‘case’ label. q‘break’ statements are inserted to avoid falling through.

Chapter 10‘switch’ construct38 ‘switch’ construct qExample:

Chapter 10Repetition Structure39 Repetition Structure qCounter-controlled repetiton: number of iterations known. qSentinel-controlled repetiton: iterate until a sentinel value is entered, or terminating condition is true.

Chapter 10‘while’ construct40 ‘while’ construct qLoop structure with pre-test condition. while (expression) statement ; qexpression is loop condition. qIf expression is true, statement in loop body is executed, and expression tested again. qIf expression is false, loop terminates.

Chapter 10‘while’ construct41 ‘while’ construct qExample: Print n asterisks. qcount_star is the loop control variable.

Chapter 10‘while’ construct42 ‘while’ construct qExample: Compute sum of first 100 positive integers.

Chapter 10‘while’ construct43 ‘while’ construct qWhich of these is/are same as previous code?

Chapter 10‘while’ construct44 ‘while’ construct qLoop control variable. uInitialisation: before the loop is entered, the variable must be initialised. uTesting: condition involving the loop control variable is tested before the start of each loop iteration; if condition is true, loop body is executed. uUpdating: loop control variable is updated during each iteration (usually at the beginning or the end of the loop body).

Chapter 10Counter-control repetition45 Counter-control repetition qA counter is used to keep track of number of iterations.

Chapter 10Sentinel-control repetition46 Sentinel-control repetition qA sentinel is used to denote end of data.

Chapter 10‘do-while’ construct47 ‘do-while’ construct qLoop structure with post-test condition. do statement ; while (expression); qLoop body is executed at least once.

Chapter 10‘do-while’ construct48 ‘do-while’ construct qExamples:

Chapter 10Flag-controlled loops49 Flag-controlled loops qWhen loop condition is complex, flags may be used.

Chapter 10‘for’ construct50 ‘for’ construct qAnother pre-test loop structure. qProvides more compact form for counter- controlled loops. for (initialisation-expression; loop-condition; update-expression ) statement;

Chapter 10‘for’ construct51 ‘for’ construct qThe ‘for’ construct is similar to this ‘while’ construct. initialisation-expression; while ( loop-condition ) { statement; update-expression; }

Chapter 10‘for’ construct52 ‘for’ construct qExample:

Chapter 10‘for’ construct53 ‘for’ construct qThe initialisation-expression and update- expression are often comma-separated lists of expressions. qThe comma operator evaluates the list from left to right.

Chapter 10‘for’ construct54 ‘for’ construct qAny of the three expressions in the ‘for’ header may be omitted, but the semi-colons must stay. qIf initialisation-expression is omitted, you must perform necessary initialisation before loop.

Chapter 10‘for’ construct55 ‘for’ construct qIf update-expression is omitted, you must ensure that necessary update operations are done in loop body.

Chapter 10‘for’ construct56 ‘for’ construct qIf loop-condition is omitted, then the test is always true. qThis loop is infinite:

Chapter 10Common Mistakes57 Common Mistakes qWrong placement of semi-colon.  

Chapter 10Common Mistakes58 Common Mistakes qOmitting semi-colons in ‘for’ header. qMixing up semi-colons with commas in ‘for’ header. qOff-by-one error, where the loop executes one more or one fewer iteration than intended. How many iterations does this loop execute?

Chapter 10‘break’ statement59 ‘break’ statement qUsed in loops, ‘break’ causes execution to break out of the loop that contains the statement.

Chapter 10'continue’ statement60 ‘continue’ statement qThe ‘continue’ statement causes execution to skip remaining loop body and proceed to next iteration.

Chapter 10Nested loops & combined structures 61 Nested loops & combined structures qAn example of nested ‘for’ loops.

Chapter 10Nested loops & combined structures 62 Nested loops & combined structures qExample 2:

Chapter 10Nested loops & combined structures 63 Nested loops & combined structures qExample 3:

Chapter 10Homework64 Homework Try exercises behind chapter 10.