Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
true (any other value but zero) false (zero) expression Statement 2
Program Control Dilshad M. Shahid New York
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
1 Chapter 4 Simple Selections and Repetitions INTRODUCTION The majority of challenging and interesting algorithms necessitate the ability to make.
Repetition (Loops) Want to do some repetitive sequence of actions: print vertical line of *s * Corresponding program: printf(“*\n”);
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
UNIT II Decision Making And Branching Decision Making And Looping
Chapter 4: Control Structures II
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Chapter 05 (Part III) Control Statements: Part II.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 10: Control Structures1 Chapter 10 Control Structures.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
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.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Introduction to Programming Using C Basic Logic. 2 Contents Conditional programming If statement Relational operators Compound statements Loops While.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Introduction to Computer Programming
Topic 4: Looping Statements
Chapter 4 – C Program Control
Chapter 5: Control Structures II (Repetition)
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Control Statements: Part 2
Chapter 4 - Program Control
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements I
- Additional C Statements
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 4 - Program Control
Chapter 6 Control Statements: Part 2
Chapter 4 - Program Control
Flow of Control.
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Chapter 9 Complex Selections and Repetitions

9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection. For statement, which is another pretest repetition structure. Do-while statement. The do-while statement is the C implementation of the posttest repetition structure.

9.2 COMPLEX PREDICATES Logical Expressions and Logical Operatiors –Logical and (conjunction) –Logical or (disjunction) –Logical not (negation) The C Logical Operators –Logical and is represented by &&. –The symbol || stands for logical or. –The C logical not operator is !. Example 9.1 (semester_average >= 80 ) && (semester_average <= 89)

Example 9.3 ! (student_status == ‘u’) Simplifying Complex Predicates in some selection problems there are several forms for expressing the same condition. We should try to get rid of the negation and find the simplest form in order to enhance the readability of the code.

PredicateEquivalent Simple Form ! (a == b)a != b ! (a = b ! (a > b)a <= b ! (Expression_1 && Expression_2) (! Expression_1) || (Expression_2) Figure 9.1 Equivalent forms for predicates involving negation

Precedence of Logical Operators Logical not, unary arithmetic operators Binary arithmetic operators Relational operators Logical and Logical or Example 9.6 x+y >= 13 && ! (x-y) || x*y -16 ==

9.3 MULTIWAY SELECTION USING THE switch AND break STATEMENTS switch (ControllingExpression){ CaseClause-1 CaseClause-2. CaseClause-n DefaultClause } /* end switch */

In the switch statement the controlling expression is evaluated first. The controlling expression must compute to an integral value and must be of type int or char; If the value of the controlling expression does not match any of the constant values in the case clauses, the content of the default clause is executed.

switch (major_code) { case 1 : printf(“student major is computer science.”; break; case 7 : printf(“Student major is computer engineering.”; break; default : printf(“Student major is a noncomputer field.”); } /* end switch */

If the actions of two or more consecutive cases are identical, all we have to do is list the cases with empty statements and specify the action and a break statement in the final case.

switch (character) { case ‘0’ : case ‘1’ : case ‘2’ : case ‘3’ : case ‘4’ : case ‘5’ : case ‘6’ : case ‘7’ : case ‘8’ : case ‘9’ : printf(“The content is a decimal integer.”); break; default : printf(“The content is a nondecimal character.”); } /* end swithc */

9.4 STYLE CONSIDERATIONS FOR MULTIWAY SELECTION STRUCTURES –Keep the use of logical negation to a minimum in forming complex predicates. –Remember that a nested if statement is more general than a switch statement. –Use proper indentations in forming switch statements. –Whenever possible, use a default clause in your switch statements.

9.5 THE PRETEST REPETITION STRUCTURE The for Statement for Pretest Repetition for (InitializationExpression; LoopControExpression; UpdateExpression) Loopbady /* end for */

Executes the InitializationExpression. Evaluates the LoopControlExpression. If it computes to zero, the loop is exited. If the LoopControlExpression yields a nonzero value, the LoopBody is executed and then the UpdateExpression is evaluated. Tests the LoopControlExpression again. Thus the LoopBody is repeated until the LoopControlExpression computes to a zero value.

int number, sum = 0, counter = 1; ……………………. While (counter <= number) { sum = sum + counter ; counter = counter + 1; } /* end while */

int number, sum, counter ; …………………….. Sum = 0; for (counter = 1 ; counter <= number ; counter = counter + 1) sum = sum + counter; /* end for */

Equivalence of for and while Statements the same repetition problem can be expressed equivalently using either a while or a for statement. Using the for Statement for Counter- Controlled Loops Using the for Statement for Sentinel- Controlled Loops

char answer; printf(“Do you want to continue? (y/n):”); for (scanf(“%c”, &answer); answer != ‘y’ && answer != ‘Y’ && answer != ‘n’ && answer != ‘N’ ; scanf(“%c”, &answer)) printf(“Please type y or n: “); /* end for */

Checking for Incorrect Data in a Loop and the continue Statement we may prefer to warn the user and skip the rest of the loop body. Causes the program control to skip the rest of the loop body and execute the loop again.

for (scanf(“%d”, &test_score); test_score !=0 ; scanf(“%d”, &test_score)) { if (test_score 100) { printf(“Incorrect test score ! Enter a correct value : “) ; continue; } /* end if */ sum = sum + test_score; number_of_students = number_of students + 1; printf(“Enter a test score. Enter 0 to stop: “); } /* end for */

9.6 THE POSTTEST REPETITION STRUCTURE the loop body is executed before the loop control expression is tested The do-while Statement –It executes the LoopBody. –It evaluates the LoopControlExpression. If the value of the LoopControlEcpression is 0, the computer exits the loop; otherwise, it does the LoopBody again.

int number, sum = 0, counter = 0; ……………………. Do { sum = sum + counter ; counter = counter + 1 ; } while (counter <= number) ; /* end do-while */ Use of do-while for Counter- and Sentinel- Controlled Loops

9.7 NESTED LOOPS A nested loop is a repetition structure that contains one or more loops in its body. A loop contained in another loop forms a doubly nested loop. int control_var1, control_var2; for (control_var1=1; control_var1<=8 ; control_var1 +=2) for (control_var2 = control_var1 ; control_var2 <= 10 ; control_var2 +=3 ) printf(“control_var1 = %d control_var2 = %d\n”, control_var1, control_var2); /* end for */

control_var1 = 1 control_var2 = 1 control_var1 = 1 control_var2 = 4 control_var1 = 1 control_var2 = 7 control_var1 = 1 control_var2 = 10 control_var1 = 3 control_var2 = 3 control_var1 = 3 control_var2 = 6 control_var1 = 3 control_var2 = 9 control_var1 = 5 control_var2 = 5 control_var1 = 5 control_var2 = 8 control_var1 = 7 control_var2 = 7 control_var1 = 7 control_var2 = 10

9.8 STYLE CONSIDERATIONS FOR REPETITION STATEMENTS –Indent the loop body of for statements –Avoid the following permitted syntax elements of the for statement : missing initialization, update, and/or loop control expressions in the header. –Avoid using while and for statements for the implementation of posttest repetition structures. –Indent the loop body of do-while

9.9 EXAMPLE PROGRAM 1: A C Program that Computes Distribution of Letter Grades