Nested Control Structures * Conditional Operator (?:) * Preincrement and Postincrement * Predecrement and Postdecrement * Counter-Controlled Repetition.

Slides:



Advertisements
Similar presentations
4 Control Statements: Part 1.
Advertisements

Chapter 04 (Part III) Control Statements: Part I.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 3 - Structured Program Development
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Control Structures in C++ while, do/while, for switch, break, continue.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Structured Program Development in C
Lecture 3 Structured Program Development in C
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
For Repetition Structures (L13) * General Form of the for Statement * Components of a Typical for Header * Pre/Postincrement of the Counter * Stream Manipulator.
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
1 Control Statements: Part I Chapter 4 Control Statements: Part I Chapter 4.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved. 1.
1 Lecture 3 Control Structures else/if and while.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) Fax: (757) Office: Gosnold 217b Subject Overview.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Algorithm: procedure in terms of
Control Structures Sequential execution Transfer of control
Chapter 8 - JavaScript: Control Statements I
Control Statements Kingdom of Saudi Arabia
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Capitolo 2 - Control Structures
2.6 The if/else Selection Structure
Chapter 4 - Control Structures: Part 1
EPSII 59:006 Spring 2004.
Presentation transcript:

Nested Control Structures * Conditional Operator (?:) * Preincrement and Postincrement * Predecrement and Postdecrement * Counter-Controlled Repetition * Nested Control Structure Dr. Ming Zhang

Condition Operator (?:) * Conditional Operator (?:) - Ternary Operator Operand1 ? Operand2 : Operand3 * First Operand Condition Expression * Second Operand the value for the entire conditional expression if the condition is true. * Third Operand the value for the entire conditional expression if the condition is false. Dr. Ming Zhang

Conditional Operator and if else * Conditional Operator cout =60 ? “Passed” : “Failed”); * if else if (grade >= 60) cout << “Passed”; else cout << “Failed” Dr. Ming Zhang

Examples of Conditional Operator * If grade is greater than or equal to 90, print on screen “A”, otherwise “B or less than B” cout =90 ? “A” : “B or less than B”); * If grade is greater than or equal to 80, print on screen “A or B”, otherwise “C or less than C” cout =80 ? “A or B” : “C or less than C”); Dr. Ming Zhang

Preincrement and Postincrement * Preincrement ++a Increment a by 1, then use the new value of a in the expression in which a resides. * Postincrement a++ Use the current value of a in the expression in which a resides, then increment a by 1 Dr. Ming Zhang

Example of Pre/Postincrement int main( ) { int c; c = 5; cout << c << endl; // print 5 cout << c++ << endl;//print 5 then postincrement cout << c << endl << endl;// print 6 c = 5; cout << c << endl; // print 5 cout << ++c << endl;//preincrement then print 6 cout << c << endl << endl;// print 6 return (0); } Dr. Ming Zhang

Predecrement and Postdecrement * Predecrement --a Decrement a by 1, then use the new value of a in the expression in which a resides. * Postdecrement a-- Use the current value of a in the expression in which a resides, then decrement a by 1 Dr. Ming Zhang

Example of Pre/Postdecrement int main( ) { int c; c = 5; cout << c << endl; // print 5 cout << c-- << endl;//print 5 then postdecrement cout << c << endl << endl;// print 4 c = 5; cout << c << endl; // print 5 cout << --c << endl;//predecrement then print 4 cout << c << endl << endl;// print 4 return (0); } Dr. Ming Zhang

No increment in while Loop- Continuation Condition #include using std::cout; int main ( ) { int counter = 1; while (counter <= 10) { cout << counter << “ “; counter= counter + 1; } return (0); } /* print on screen */ Dr. Ming Zhang

Preincrement in while Loop- Continuation Condition #include using std::cout; int main ( ) { int counter = 1; while (++counter <= 10) { cout << counter << “ “;} return (0); } /* print on screen */ /* 1 would not be printed, because ++counter) */ Dr. Ming Zhang

Posincrement in while Loop- Continuation Condition #include using std::cout; int mail ( ) { int counter = 1; while (counter ++ <= 10) { cout << counter << “ “; } return (0); } /* print on screen (1?) (11?)*/ Dr. Ming Zhang

Nested Control Structures (1) A college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, several of the students who completed this course took the licensing examination. Naturally, the college wants to know how well its students did on the exam. You have been asked to write a program to summarize the results. You have been given a list of these 10 students. Next to each name is written a 1 if the student passed the exam and a 2 it the student failed. Dr. Ming Zhang

Nested Control Structures (2) Your program should analyze the results of the exam as follow: 1. Input each test result. Display the message “Enter result” on the screen each time the program requests another test result. 2. Count the number of test results of each type. 3. Display a summary of the test results indicating the number of students who passed and the number of students who failed. 4. If more than 8 students passed the exam, print the message “Raise tuition”. Dr. Ming Zhang

Nested Control Structures (3) Top: Analyze exam results and decide if tuition should be raised. First Refinement: Initialize variables Input the ten quiz grades and count passes and failures Print a summary of the exam results and decide if tuition should be raised. Dr. Ming Zhang

Nested Control Structures (4) Fig while (studentCounter <= 10) { cout << “Enter result (1=pass, 2=fail):”; cin >> result; if (result == 1) passes = passes +1; else failures = failures + 1; studentCounter = studentCounter + 1; } Dr. Ming Zhang

Common Programming Error (1) #include using std::cout; int main( ) { int class = 1; while (class <= 10) { cout << class << “ “; class= class + 1; } return (0);} ERROR Dr. Ming Zhang

Common Programming Error (2) #include using std::cout; int main ( ) { int counter = 1; while (counter <= 10) cout << counter << “ “; counter= counter + 1; } return (0);} ERROR Dr. Ming Zhang

Common Programming Error (3) #include using std::cout; using std::cin int main ( ) { int grade; cin >> grade; if (grade >= 60); cout << “Passed”; else cout << “Failed”;return (0); } ERROR Dr. Ming Zhang

Common Programming Error (4) #include using std::cout; int main ( ) { int counter = 1; while (counter <= 10) { cout << counter << “ “; } return (0);} ERROR Dr. Ming Zhang

Common Programming Error (5) #include using std::cout; int main ( ) { int counter = 1; While (counter <= 10) { cout << counter << “ “; counter= counter + 1; } return (0);} ERROR Dr. Ming Zhang

Common Programming Error (6) #include using std::cout; int main ( ) { while (counter <= 10) { cout << counter << “ “; counter= counter + 1; } return (0);} ERROR Dr. Ming Zhang

Common Programming Error (7) #include using std::cout; int main ( ) { int counter = 1; while (counter <= 10) { cout << counter << “ “; ++(counter + 1); } return (0);} ERROR Dr. Ming Zhang

Common Programming Error (8) #include using std::cout; int main ( ) { float counter = 1.0; while (counter <= 10.0 ) { cout << counter << “ “; counter = counter + 1.0; } return (0);} ERROR Dr. Ming Zhang