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

Slides:



Advertisements
Similar presentations
Control Structures Selections Repetitions/iterations
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.
Chapter 1 Pseudocode & Flowcharts
 Control structures  Algorithm & flowchart  If statements  While statements.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Structured Program Development in C
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
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.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved. 4.
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.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
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.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
Chapter 1 Pseudocode & Flowcharts
Chapter#3 Part1 Structured Program Development in C++
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
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.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
 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.
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,
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
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.
Decision making If.. else statement.
Branching statements.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Lecturer CS & IT Department UOS MBDIN
SELECTION STATEMENTS (1)
Chapter 4 Control Statements: Part I
Lecture 2: Logical Problems with Choices
Chapter 1 Pseudocode & Flowcharts
1) C program development 2) Selection structure
3 Control Statements:.
Chapter#3 Structured Program Development in C++
Chapter 1 Pseudocode & Flowcharts
Decision making If statement.
Chapter 4: Control Structures I (Selection)
Structured Program Development in C++
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Chapter 1 Pseudocode & Flowcharts
Structural Program Development: If, If-Else
Presentation transcript:

CHAPTER#3 PART1 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, Alaa Altheneyan, & Noor Alhareqi

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  C++ has only three kinds of control structures, which from this point forward we refer to as control statements:  Sequence statements.  Selection statements ( if, if – else, and switch).  Repetition statements ( for, while, and do - while).  Each program combines these control statements as appropriate for the algorithm the program implements. 3

Types of Control Structures 4

Types of Control structure  The control structure of this lecture is :  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 (or, as you’ll see later, a single group of actions)..  Syntax: if (expression) statement Expression referred to as decision maker and it can be formed by using equality operators and relational operator. 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)

9

Example 10

Selection structure 11  Note, you should not put a semicolon after the right parenthesis of the if condition.  if( condition) ;  X

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

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"; … 13  Flowchart

selection structure: if…else –Example2  Write a program that reads a number from the user and determines if it's a positive or negative number.  Code: … if ( number >= 0 ) cout<<" Positive\n "; else cout<<" Negative\n "; … 14  Flowchart Num>=0 “Positive”“Negative”

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  The first operand is a condition.  The second operand is the value for the entire conditional expression if the condition is true  The third operand is the value for the entire conditional expression if the condition is false. 15

Conditional (? :) Operator- example1 16 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 17 … if ( grade >= 60 ) cout<<"Passed"; else cout<<"Failed"; … The above statement can be written using the conditional operator as following: Cout = 60 ? “Passed” : “Failed”); The precedence of the conditional operator is low, so the parentheses in the preceding expression are required

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

Compound (Block of) Statements: Example 19 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

Exercise 20  Write a program that calculates the discount, if any, on a sale. Sales of $100 and over are eligible for a 10% discount. The program should ask the user what the amount of their purchase is and calculates and displays the discount, if there is any, or else it will display a message stating that there is no discount.

Review of Syntax 21 Simple if Statement if (condition) statement; Simple if-else Statement if (condition) statement; else statement;

Review of Syntax 22 Compound if Statement if (condition) { Statements } Compound if-else Statement if (condition) { Statements } else { Statements }