Structured Program Development in C++

Slides:



Advertisements
Similar presentations
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Advertisements

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.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
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
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-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.
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.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
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.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
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.
Week 4 Program Control Structure
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.
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,
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):
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Branching statements.
The if…else Selection Statement
Chapter 3 Control Statements
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
Week 3 C Program Structures (Selection Structures)
EGR 2261 Unit 4 Control Structures I: Selection
Week 3 – Selection Structures
CSC113: Computer Programming (Theory = 03, Lab = 01)
DKT121: Fundamental of Computer Programming
Chapter 4: Making Decisions.
JavaScript: Control Statements.
JavaScript: Control Statements I
Control Structures – Selection
Lecture 2: Logical Problems with Choices
Chapter 4: Control Structures I (Selection)
3 Control Statements:.
Chapter#3 Structured Program Development in C++
Chapter 7 Conditional Statements
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Week 3 – Program Control Structure
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

Structured Program Development in C++ King Saud University College of Applied studies and Community Service By: Asma Alosaimi Edited By: Ghadah R. Hadba , Alaa Altheneyan, Noor Alhareqi& safaa albassam Structured Program Development in C++

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

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

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

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. 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 -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"; }

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

Multiple Selection: Nested if Multiple if statements can be used if there is more than two alternatives. This mean we can place the if statement or the if-else statement inside a body of other if or/and else. There are numerous situations to achieve that and it depends on the problem under consideration. Here we will take a look on some situations .

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

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

Multiple Selection: Nested if (Example-3) if( num == 0) cout<<“ the entered number is zero \n” else { if( num < 0) cout<<“the entered number is negative\n”; cout<<“the entered number is positive\n”; }

Multiple Selection: Nested if (Example-4) //The following code does not work as intended. X= 7 y= 6 X=7 y =4 //This is the correct way of writing the above code 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. The switch statement consists of a series of case labels and an optional default case.

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

Type of values in Switch Cases 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 x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? break; switch (N) { case 1: x = 10; break; case 2: x = 20; case 3: x = 30; }

Switch With no break Statements 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 ? Without break statements, each time a match occurs in the switch, the statements for that case and subsequent cases execute until a break statement or the end of the switch is encountered.

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

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

Switch With more than one case labels Example switch (grade) { case 'A': case ‘a': cout<<"The grade is A."; break; case 'B': case ‘b': cout<<"The grade is B."; case 'C': case ‘c': cout<<"The grade is C."; case 'D': case ‘d': cout<<"The grade is D."; case 'F': case ‘f': cout<<"The grade is F."; default: cout<<"The grade is invalid."; }

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

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

Common Errors in the switch

Comparison of if Statements and The Switch Statement if statements are more general than the switch statement. Case labels that contain type float values or strings are not permeated in switch statements. The switch statement is more readable in many contexts. Thus, it should be used whenever practical.