3 Control Statements:.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
Chapter 04 (Part III) Control Statements: Part I.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 3 - Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Structured Program Development in C
Lecture 3 Structured Program Development in C
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
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.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved. 4.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 Control Statements: Part I Chapter 4 Control Statements: Part I Chapter 4.
 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.
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 JavaScript: Control Statements I.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
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.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
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 7 JavaScript: Control Statements, Part 1
Branching statements.
Structured Program Development in C
Control Structures Sequential execution Transfer of control
while Repetition Structure
- Standard C Statements
Control Statements: Part 1
JavaScript: Control Statements I
Chapter 2.1 Control Structures (Selection)
TMF1414 Introduction to Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
Ch 7: JavaScript Control Statements I.
Control Statements Kingdom of Saudi Arabia
JavaScript: Control Statements.
JavaScript: Control Statements I
Adopted from Pearson Education slides
Structured Program
Chapter 3 - Structured Program Development
Chapter 3 - Structured Program Development
Structured Program Development in C
2.6 The if/else Selection Structure
EPSII 59:006 Spring 2004.
Structured Program Development in C
Structured Program Development in C
Control Statements Paritosh Srivastava.
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

3 Control Statements:

Control Structures Sequential execution Transfer of control Statements executed in sequential order Transfer of control Next statement executed is not the next one in sequence

Control Structures (Cont.) Only three control structures needed Three control structures Sequence structure Programs executed sequentially by default Selection structures if, if…else, switch Repetition structures while, do…while, for

Fig. 4.3 | C++ keywords.

Common Programming Error Using a keyword as an identifier is a syntax error.

Common Programming Error Spelling a keyword with any uppercase letters is a syntax error. All of C++’s keywords contain only lowercase letters.

if Selection Statement Selection statements Choose among alternative courses of action Pseudocode example If student’s grade is greater than or equal to 60 Print “Passed” If the condition is true Print statement executes, program continues to next statement If the condition is false Print statement ignored, program continues

if Selection Statement (Cont.) Selection statements (Cont.) Translation into C++ if ( grade >= 60 ) cout << "Passed"; Any expression can be used as the condition If it evaluates to false, it is treated as false

if…else Double-Selection Statement Performs action if condition true if…else Performs one action if condition is true, a different action if it is false Pseudocode If student’s grade is greater than or equal to 60 print “Passed” Else print “Failed” C++ code if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

if…else Double-Selection Statement (Cont.) Ternary conditional operator (?:) Three arguments (condition, value if true, value if false) Code could be written: cout << ( grade >= 60 ? “Passed” : “Failed” ); Condition Value if true Value if false

if…else Double-Selection Statement (Cont.) Nested if…else statements One inside another, test for multiple cases Once a condition met, other statements are skipped Example If student’s grade is greater than or equal to 90 Print “A” Else If student’s grade is greater than or equal to 80 Print “B” Else If student’s grade is greater than or equal to 70 Print “C” Else If student’s grade is greater than or equal to 60 Print “D” Else Print “F”

4.6 if…else Double-Selection Statement (Cont.) Nested if…else statements (Cont.) Written In C++ if ( studentGrade >= 90 ) cout << "A"; else if (studentGrade >= 80 ) cout << "B"; else if (studentGrade >= 70 ) cout << "C"; else if ( studentGrade >= 60 ) cout << "D"; else cout << "F";

if…else Double-Selection Statement (Cont.) Nested if…else statements (Cont.) Written In C++ (indented differently) if ( studentGrade >= 90 ) cout << "A"; else if (studentGrade >= 80 ) cout << "B"; else if (studentGrade >= 70 ) cout << "C"; else if ( studentGrade >= 60 ) cout << "D"; else cout << "F";

if…else Double-Selection Statement (Cont.) if ( x > 5 ) { if ( y > 5 ) cout << "x and y are > 5"; } else cout << "x is <= 5"; Braces indicate that the second if statement is in the body of the first and the else is associated with the first if statement

if…else Double-Selection Statement (Cont.) Compound statement Also called a block Set of statements within a pair of braces Used to include multiple statements in an if body Example if ( studentGrade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } Without braces, cout << "You must take this course again.\n"; always executes

while Repetition Statement Action repeated while some condition remains true Pseudocode While there are more items on my shopping list Purchase next item and cross it off my list while loop repeats until condition becomes false Example int product = 3; while ( product <= 100 ) product = 3 * product;

Common Programming Error Not providing, in the body of a while statement, an action that eventually causes the condition in the while to become false normally results in a logic error called an infinite loop, in which the repetition statement never terminates. This can make a program appear to “hang” or “freeze” if the loop body does not contain statements that interact with the user.

Formulating Algorithms: Counter-Controlled Repetition Problem statement A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Calculate and display the total of all student grades and the class average on the quiz. Counter-controlled repetition Loop repeated until counter reaches certain value Also known as definite repetition Number of repetitions known beforehand

Formulating Algorithms: Counter-Controlled Repetition (Cont.) Counter variable Used to count In example, indicates which of the 10 grades is being entered Total variable Used to accumulate the sum of several values Normally initialized to zero beforehand Otherwise it would include the previous value stored in that memory location

Pseudocode algorithm that uses counter-controlled repetition to solve the class average problem.

Error-Prevention Tip Initialize each counter and total, either in its declaration or in an assignment statement. Totals are normally initialized to 0. Counters are normally initialized to 0 or 1, depending on how they are used

Assignment Operators Assignment expression abbreviations Addition assignment operator Example c = c + 3; abbreviates to c += 3; Statements of the form variable = variable operator expression; can be rewritten as variable operator= expression; Other assignment operators d -= 4 (d = d - 4) e *= 5 (e = e * 5) f /= 3 (f = f / 3) g %= 9 (g = g % 9)

Arithmetic assignment operators.

Increment and Decrement Operators Increment operator ++ Increments variable by one Example c++ Decrement operator -- Decrement variable by one c--

Increment and Decrement Operators (Cont.) Preincrement When the operator is used before the variable (++c or --c) Variable is changed, then the expression it is in is evaluated using the new value Postincrement When the operator is used after the variable (c++ or c--) Expression the variable is in executes using the old value, then the variable is changed

Increment and decrement operators.

Good Programming Practice Unlike binary operators, the unary increment and decrement operators should be placed next to their operands, with no intervening spaces.

Increment and Decrement Operators (Cont.) If c = 5, then cout << ++c; c is changed to 6 Then prints out 6 cout << c++; Prints out 5 (cout is executed before the increment) c then becomes 6

Increment and Decrement Operators (Cont.) When variable is not in an expression Preincrementing and postincrementing have same effect Example ++c; cout << c; and c++; cout << c; are the same