Chapter#3 Part1 Structured Program Development in C++

Slides:



Advertisements
Similar presentations
Chapter 1 Pseudocode & Flowcharts
Advertisements

 Control structures  Algorithm & flowchart  If statements  While statements.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
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;
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
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: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 4 Selection Structures: Making Decisions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
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.
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.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
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 1 Pseudocode & Flowcharts
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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.
 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,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Decision making If.. else statement.
Branching statements.
Introduction to Computer Programming
Chapter 4: Control Structures I (Selection)
The if…else Selection Statement
Chapter 3 Control Statements
EGR 2261 Unit 4 Control Structures I: Selection
CSC113: Computer Programming (Theory = 03, Lab = 01)
SELECTION STATEMENTS (1)
Lecture 2: Logical Problems with Choices
Chapter 1 Pseudocode & Flowcharts
Chapter 4: Control Structures I (Selection)
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 Selection.
Chapter 4: Control Structures I (Selection)
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Chapter 1 Pseudocode & Flowcharts
Control Structures.
Presentation transcript:

Chapter#3 Part1 Structured Program Development in C++ King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited By: Ghadah R. Hadba , Alaa Altheneyan, & Noor Alhareqi Chapter#3 Part1 Structured Program Development in C++ 2nd semester 1432-1433

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.

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.

Types of Control Structures

Types of Control structure The control structure of this lecture is : Sequence structure Selection structure Repetition structure.

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.

Selection Structure if selection statement if…else selection statement switch selection statement

Selection structure: if The if statement is called a single-selection statement because it selects or ignores a single action (or, a single group of actions).. Syntax: if (expression) statement Expression it is Boolean expression referred to as decision maker and it can be formed by using equality operators and relational operator. Statement referred to as action statement.

Selection structure: if The semantics (meaning) of the if statement : The Boolean expression is evaluated. If the expression evaluates to true, then the statement is executed. Whether or not the statement is executed, execution continues right after the if statement. In case if there is more than one action should be performed, enclose the statements in { } if (expression) { statement1; statement2; ….. } Compound (Block of) Statements

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”; Note that, these statements Represents the selection part Of the code (i.e. not the whole Code)

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

Exercise Write a fragment of code that test whether an integer variable score contains a valid test score. Valid test scores are in the range from 0 to 100.

Exercise What is the output for the following code lines if hours=35 and hours=60: a) if (hours <= 40) cout << “No overtime” << endl; cout<<"You must work over 40 hours for overtime\n”; cout << “Continue” << endl; B) if (hours <= 40) { cout << "You must work over 40 hours for overtime.\n"; }

Example Write a program that reads two numbers and then compare these two numbers and print the relations that they have satisfied

Example

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

selection structure: if … else The Boolean expression is evaluated. If the expression evaluates to true, then the first statement is executed. If the expression evaluates to false, then the second statement is executed In either case, execution continues right to the next statement after the if’s body In case if there is more than one action should be performed when the condition is true or/and when the condition is false , enclose the statements in { }

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"; Flowchart

Compound (Block of) Statements: Example if ( grade >= 60 ) cout<<"Passed\n"; else { cout<<"Failed\n" ; cout<<“you have to take this course again\n"; }

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 "; Flowchart “Positive” Num>=0 “Negative”

Exercise Write a fragment of code that change an integer value stored in x as follows: If x is even, divide x by 2. If x is odd, multiply x by 3 and subtract 1.

Exercise Find the errors for the following code and correct them? A- if !x > x + y  x = 2 * x;  else  x = x + 3; B- if(speed > 65) cout << "Slow down"; else; cout << "Speed is legal on I5"; else cout << "\n Done";   C- if(speed > 65) cout << "\n Relax, you'll live a longer and happier life\n";

Exercise What is the output of the following C++ code fragment When the Price is 350? When the Price is 60?   float price; if (price>=100) { price=price*0.9; cout<<"The price after the discount is:"<< price; } else cout<<"There is no discount\n";

Exercise Consider the following portion of C++ code: if (y < 10) else x = y%(5+3) - 4;   Find the value of x when y=7? Find the value of x when y=15?

Exercise Consider the following portion of C++ code: int a,b = 4;   int a,b = 4; int c = 3; if((b > a) && (b++ > 0)) c = b+3; else c=b-3; What are the values of b and c if the value of a = 1? What are the values of b and c if the value of a = 5?

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.

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

Exercise By using conditional Operator (? :) rewrite the code of Write a fragment of code that change an integer value stored in x as follows: If x is even, divide x by 2. If x is odd, multiply x by 3 and subtract 1.

Exercise Consider the following portion of C++ code:   int a = 10, b = 20; int max ; max = (a < b) ? ((b < 20)? b * 2 : ((b > 20) ? b % 3 : b / 4)) : ((a == 10) ? a / 2 : a % 3); What is the max value after the evaluation of this expression?

Exercise Draw a flow chart and Write a C++ code for a program that prompts the user to enter 2 numbers. Then the program checks if num1 is equal to twice the value of num2 or not?

Exercise 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 Simple if Statement if (condition) statement; Simple if-else Statement else

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