CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester 1432-1433 1 King Saud University College of Applied studies and Community Service Csc 1101.

Slides:



Advertisements
Similar presentations
 Control structures  Algorithm & flowchart  If statements  While statements.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Computer Science Department Relational Operators And Decisions.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures I
CHAPTER 4 CONTROL STRUCTURES I Selection. In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how.
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.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Chapter 4: Control Structures SELECTION STATEMENTS.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter#3 Part1 Structured Program Development in C++
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Week 4 Program Control Structure
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
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.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Control Statements: Part1  if, if…else, switch 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 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.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
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.
Chapter 4: Control Structures I
Chapter 4: Control Structures I
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
JavaScript: Control Statements I
SELECTION STATEMENTS (1)
Chapter 4: Control Structures I
Lecture 2: Logical Problems with Choices
Control Structures: Selection Statement
3 Control Statements:.
Chapter#3 Structured Program Development in C++
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
2.6 The if/else Selection Structure
Week 3 – Program Control Structure
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Control Structures: Selection Statement
Branching statements Kingdom of Saudi Arabia
Control Structure.
Presentation transcript:

CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited By: Ghadah R. Hadba

Control structure  Normally, statements in a program are executed one after the other in the order in which they are written. This is called sequential execution.  Various C++ statements will enable you to specify the next statement to be executed which might be other than the next one in sequence. This is called transfer of control. 2

Types of Control structure  could be written in terms of only three control structures:  Sequence structure  Selection structure  Repetition structure. 3

Types of Control Structures 4

Types of Control structure  could be written in terms of only three control structures:  Sequence structure  Selection structure  Repetition structure. 5

Selection structure  C++ provides three types of selection structures in the form of statements:  The if selection statement either performs (selects) an action if a condition is true or skips the action if the condition is false.  The if … else selection statement performs an action if a condition is true and performs a different action if the condition is false.  The switch selection statement performs one of many different actions depending on the value of an expression. 6

selection structure: if  The if statement is called a single-selection statement because it selects or ignores a single action.  Syntax: if (expression) statement Expression referred to as decision maker. Statement referred to as action statement. 7

selection structure: if -Example  Write a program that reads a student’s grade and prints “passed” if the grade is greater than or equal 60.  Flowchart:  Code:... if ( grade >= 60 ) cout<<"Passed”;... 8 Note that, these statements Represents the selection part Of the code (i.e. not the whole Code)

selection structure: if … else  The if…else statement is called a double- selection statement because it selects between two different actions.  Syntax: if (expression) statement1 else statement2 9

selection structure: if … else -Example  For example,  pseudocode statement  If student’s grade is greater than or equal to 60 Print “Passed” else Print “Failed”  Code: … if ( grade >= 60 ) cout<<"Passed"; else cout<<"Failed"; … 10  Flowchart

conditional Operator (? :)  C++ provides the conditional operator (?:) which is closely related to the if…else statement.  The conditional operator is C++’s only ternary operator i.e. it takes three operands.  Syntax: expression1 ? expression2 : expression3  If expression1 = true, then the result of the condition is expression2.  Otherwise, the result of the condition is expression3. 11

Conditional (? :) Operator- example1 12 Example: int x = 5, y =3, min ; if (x <= y) min = x ; else min = y ; The above statement can be written using the conditional operator as following: min = ( x <= y ? x : y);

Conditional (? :) Operator- example2 13 … if ( grade >= 60 ) cout<<"Passed"; else cout<<"Failed"; … The above statement can be written using the conditional operator as following: Cout = 60 ? “Passed” : “Failed”);

Compound (Block of) Statements 14 Syntax: { statement1 statement2. statementn }

Compound (Block of) Statements: Example 15 if ( grade >= 60 ) cout<<"Passed\n"; else { cout<<"Failed\n" ; cout<<“you have to take this course again\n"; } Java Programming: From Problem Analysis to Program Design, D.S. Malik

Multiple Selection: Nested if if (expression1) statement1 else if(expression2) statement2 else statement3 Multiple if statements can be used if there is more than two alternatives else is associated with the most recent if that does not have an else syntax 16

Multiple Selection: Nested if (Example-1)  Many C++ programmers prefer to write the preceding if statement as if ( grade >= 90 ) cout "A\n" ; else if ( grade >= 80 ) cout = 70 ) cout = 60 ) cout<<"D\n" ; else cout<<"F\n" ; 17

Multiple Selection: Nested if (Example-2) if( tempreture >= 50 ) if (tempreture >= 80) cout<<“Good swimming day”; else cout<<“Good golfing day”; else cout<<“Good tennis day”; 18 Remember: else is associated with the most recent if that does not have an else

Multiple Selection: Nested if (Example-3) 19 if( tempreture >= 50 ) if (tempreture >= 80) cout<<“Good swimming day”; else cout<<“Good golfing day”; Remember: else is associated with the most recent if that does not have an else

Multiple Selection: Nested if (Example-4) 20 //The following code does not work as intended. For example if GPA=3.8 if ( GPA >= 2.0 ) if (GPA >= 3.9) cout<<“Dean Honor list\n”; else cout<<“GPA below graduation requirement\n”; //This is the correct way of writing the above code if ( GPA >= 2.0 ) { if (GPA >= 3.9) cout<<“Dean Honor list”; } else cout<<“GPA below graduation requirement\n”; To force the nested if..else statement to execute as it was originally intended we must use the braces {} as following:

selection structure: Switch  The switch statement is called a multiple- selection statement because it selects among many different actions. 21

selection structure: Switch 22 Expression: is also known as selector. Expression can be an identifier. Value can only be integral.  Syntax: switch (expression) { case value1: statements1 break; case value2: statements2 break;... case valuen: statementsn break; default: statements }

Type of values in Switch Cases 23  In Switch statement each action is associated with a value of constant integral expression (i.e. any combination of character constants and integer constant that evaluates to a constant integer value)  Thus, switch selector must be either :  An variable of integer or char data type, OR  An expression that evaluates to a constant integer value.

Switch With break Statements 24 switch (N) { case 1: x = 10; break; case 2: x = 20; break; case 3: x = 30; break; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? false true break;

Switch With no break Statements 25 switch (N) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? false true

Switch With break Statements 26 Example switch (grade) { case 'A': cout<<"The grade is A."; break; case 'B': cout<<"The grade is B."; break; case 'C': cout<<"The grade is C."; break; case 'D': cout<<"The grade is D."; break; case 'F': cout<<"The grade is F."; break; default: cout<<"The grade is invalid."; }

Previous Example - With Nested If 27  if (grade == 'A') printf("The grade is A."); else if (grade == 'B') printf("The grade is B."); else if (grade == 'C') printf("The grade is C."); else if (grade == 'D') printf("The grade is D."); else if (grade == 'F') printf("The grade is F."); else printf("The grade is invalid.");

Switch With break Statements 28 Example 2 switch (number1+number2) { case 0: cout<<"The sum is 0"; break; case 1: cout<<"The sum is 1"; break; case 2: cout<<"The sum is 2"; break; case 3: cout<<"The sum is 3"; break; case 4: cout<<"The sum is 4"; break; default: cout<<“ illegal expression"; }

Switch With break Statements 29 Example 3 switch (number%2) { case 0: cout<<number<<“is an even number”; break; case 1: cout<< number<<“is an odd number”; break; default: cout<<number<<"is invalid."; }

Exercise  Write a program that reads the price and the production date ( month and year of an item ) then the program prints the price after applying the discount rate which equals to :  50% if it is produced before  30% if it is produced in 2011 but before the 4 th month.  10% if it is produced after 4,2011 and before 8,