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
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;
The If/Else Statement, Boolean Flags, and Menus Page 180
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.
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.
Selection Structures (if & switch statements) (CS1123)
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.
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
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 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#3 Part1 Structured Program Development in C++
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.
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 (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.
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.
Chapter 4: Control Structures I (Selection)
The if…else Selection Statement
Chapter 3 Control Statements
EGR 2261 Unit 4 Control Structures I: Selection
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
The Selection Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
SELECTION STATEMENTS (1)
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 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++ 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

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. 5

Selection Structure 6 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. 7

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

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

Exercise 11  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 << “No overtime” << endl; cout << "You must work over 40 hours for overtime.\n"; } cout << “Continue” << endl;

Exercise 12  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.  Write a program that reads two numbers and then compare these two numbers and print the relations that they have satisfied

13

Example 14

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 15

selection structure: if … else 16  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"; … 17  Flowchart

Compound (Block of) Statements: Example 18 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 "; … 19  Flowchart Num>=0 “Positive”“Negative”

Exercise 20  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 21  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 << "Slow down"; cout << "\n Relax, you'll live a longer and happier life\n"; else cout << "Speed is legal on I5";

Exercise 22  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 23  Consider the following portion of C++ code: if (y < 10) x = 6*3; else x = y%(5+3) - 4;  Find the value of x when y=7?  Find the value of x when y=15?

Exercise 24  Consider the following portion of C++ code: 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. 25

Conditional (? :) Operator- example1 26 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 27 … 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

Exercise 28  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 29  Consider the following portion of C++ code: int a = 10, b = 20; int max ; max = (a 20) ? b % 3 : b / 4)) : ((a == 10) ? a / 2 : a % 3);  What is the max value after the evaluation of this expression?

Exercise 30  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 31  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.