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

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

1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
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.
true (any other value but zero) false (zero) expression Statement 2
C++ for Engineers and Scientists Third Edition
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
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.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Flow of Control Part 1: Selection
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Chapter 4: Control Structures SELECTION STATEMENTS.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
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 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
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.
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
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
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 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
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.
 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.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
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.
EGR 2261 Unit 4 Control Structures I: Selection
Engineering Problem Solving with C++, Etter/Ingber
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 4: Control Structures I (Selection)
Lec 5.
Control Structures: Selection Statement
3 Control Statements:.
Chapter#3 Structured Program Development in C++
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Control Structures: Selection Statement
Control Structure.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited By: Ghadah R. Hadba & Noor Al- Hareqi

Types of Control structure  The control structure :  Sequence structure  Selection structure  Repetition structure.  In this lecture we will continue the talking about the Selection structure. 2

Selection structure  We learned that the C++ provides three types of selection structures in the form of statements:  The if selection statement : performs 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. But!! What if there are more than two actions under different conditions ?  The switch selection statement: performs one of many different actions depending on the value of an expression. 3

Multiple Selection: Nested if 4 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 if (expression1) statement1 else if(expression2) statement2 else statement3 else is associated with the most recent if that does not have an else 5 if (expression1) if(expression2) statement1 else statement2 else statement3 Example for if-else statement inside the else body of other if-else statement Example for if-else statement inside the if body of other if-else statement

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

Multiple Selection: Nested if (Example-2) 7 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 Example of if-else inside an if statement

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

Multiple Selection: Nested if 9 if (expression1) statement1 else if(expression2) statement2 if (expression1) { if(expression2) statement1 } else statement2 Example for if statement inside the else body of other if-else statement Example for if statement inside the if body of other if-else statement

Multiple Selection: Nested if (Example-4) 10 To force the nested if..else statement to execute as it was originally intended we must use the braces {} as following: //The following code does not work as intended. //This is the correct way of writing the above code

Multiple Selection: Nested if (Example-5)  In this example we want to print a score letter depending on the input studentGrade 11 if ( grade >= 90 ) cout "A\n" ; else if ( grade >= 80 ) cout = 70 ) cout = 60 ) cout<<"D\n" ; else cout<<"F\n" ;  Many C++ programmers prefer to write the preceding if statement as

Comparing if Statements 12 if (score>=90) printf("A"); if (score>=80 && score<90) printf("B"); if (score>=70 && score<80) printf("C"); if (score>=60 && score<70) printf("D"); if (score<60) printf("F"); if (score>=90) printf("A"); else if (score>=80) printf("B"); else if (score>=70) printf("C"); else if (score>=60) printf("D"); else printf("F"); 5 simple if statements 1 multiple alternative if statement

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

selection structure: Switch 14 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 15  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 16 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 17 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 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 18 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 19  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 more than one case labels 20 Example switch (grade) { case 'A': case ‘a': cout<<"The grade is A."; break; case 'B': case ‘b': cout<<"The grade is B."; break; case 'C': case ‘c': cout<<"The grade is C."; break; case 'D': case ‘d': cout<<"The grade is D."; break; case 'F': case ‘f': cout<<"The grade is F."; break; default: cout<<"The grade is invalid."; }

Switch With break Statements 21 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 22 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."; }

Common Errors in the switch 23

Comparison of if Statements and The Switch Statement 24  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.

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,