Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.

Slides:



Advertisements
Similar presentations
More on Algorithms and Problem Solving
Advertisements

3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
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.
Chapter 5: Control Structures II (Repetition)
 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.
Structured Program Development in C
Examples from “c++ how to program” book. SELECTIONS WITH IF-ELSE Example: if ( grade >= 60) cout = 60) cout
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
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.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
1 Control Statements: Part I Chapter 4 Control Statements: Part I Chapter 4.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
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.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
 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.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
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.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Control Statements: Part1  if, if…else, switch 1.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
 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.
Branching statements.
The if…else Selection Statement
- Standard C Statements
Control Statements: Part 1
Topic 3, Selection Statements
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Chapter 4 Control Statements: Part I
Adopted from Pearson Education slides
Lecture 2: Logical Problems with Choices
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

Chapter 04 Control Statements: Part II

OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.

4.6 if…else Double-Selection Statement 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 activity diagram

Conditional Operator Conditional operator ( ?: ) –Three arguments (condition, value if true, value if false ) –Example: int Passed; if ( grade >= 60 ) Passed = 1; else Passed = 0; int Passed = (grade >= 60)? 1 : 0;

Conditional Operator Example: if (grade >= 60) cout << “Passed”; else cout << “Failed”; –The code can be written: cout = 60)? “Passed” : “Failed” ); ConditionValue if trueValue if false

4.6 if…else Double-Selection Statement 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 Nested if…else statements –Written In C++ if ( studentGrade >= 90 ) cout = 80 ) cout = 70 ) cout = 60 ) cout << "D"; else cout << "F";

4.6 if…else Double-Selection Statement The codes in the previous page can be written as: 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"; }

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

Comparison

4.6 if…else Double-Selection Statement Compound statement using braces –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

4.6 if…else Double-Selection Statement Dangling- else problem –Compiler associates else with the immediately preceding if –Example if ( x > 5 ) if ( y > 5 ) cout 5"; else cout << "x is <= 5"; –Compiler interprets as if ( x > 5 ) if ( y > 5 ) cout 5"; else cout << "x is <= 5";

4.6 if…else Double-Selection Statement Dangling- else problem –Rewrite with braces ( {} ) if ( x > 5 ) { if ( y > 5 ) cout 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. –Suggestion: »While writing nested structure, use braces all the time.

Common Programming Error Forgetting one or both of the braces that delimit a block can lead to syntax errors or logic errors in a program. –Good Programming Practice To avoid omitting one or both of the braces, some programmers prefer to type the beginning and ending braces of blocks even before typing the individual statements within the braces.

4.6 if…else Double-Selection Statement Empty statement –A semicolon ( ; ) where a statement would normally be –Performs no action –Also called a null statement

Common Programming Error Placing a semicolon after the condition in an if statement leads to –a logic error in single-selection if statements, and –a syntax error in double-selection if...else statements. –Discuss if ( studentGrade >= 60 ); cout << "Passed.\n"; if (studentGrade >= 60); cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; }

4.7 while Repetition Statement Repetition statement –Action repeated while some condition remains true. –Pseudocode While there are less than 100 products, triple the amount of products. –while loop repeats until condition becomes false.

Fig. 4.6 | while repetition statement UML activity diagram.

4.7 while Repetition Statement Merge symbol –Joins two or more flows of activity into one flow of activity –Represented as a diamond Unlike the decision symbol a merge symbol has –Multiple incoming transition arrows –Only one outgoing transition arrows »No guard conditions on outgoing transition arrows

4.7 while Repetition Statement C++ Implementation: int product = 10; while ( product <= 100 ) { cout << “There are ” << product << “ items.” << endl; product = product * 3; } –Note: Indent the statement(s) in the body of an if statement to enhance readability. If there is only one statement in the body, the braces can be removed.

4.7 while Repetition Statement C++ Implementation: int product = 10; while ( product <= 100 ) product = product * 3; –Note: The variables used in the condition should be initialized. –Discuss the result when the variable is not initialized. The statements in the body will be written to change the condition so as to break loop.

Infinite Loop Example: –Note: This can make a program appear to “hang” or “freeze”. int product = 10; while (product > 0) { product = product * 3; } int product = 10; while (product > 0) ; int product = 10; while (1) { product = product * 3; …… }

Mixed Usage of if- and while- statements if- and while-statements could be mixed or nested. Indenting codes properly can increase readability of your program. while (option != -1) { if (…) { … } else if (…) { while (…) { … }

Indenting Functions in VS Press or to decrease/increase indenting. Decrease/Increase indenting