Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
If Statements & Relational Operators, Part 2 Programming.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
C++ Programming: From Problem Analysis to Program Design, Fourth 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.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Selection Structures (if & switch statements) (CS1123)
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
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.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
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.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Week 4 Program Control Structure
1 CS 101 Fall 2001 Lecture 3. 2 Boolean expressions The expressions that are allowed to be in the parentheses following an if can be of the form x>y,
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
The Ohio State University
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 3 Selection Statements
Selection Control Structure
Bill Tucker Austin Community College COSC 1315
Decisions Chapter 4.
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Lecture 2: Logical Problems with Choices
Compound Assignment Operators in C++
Programming Funamental slides
Programming Funamental slides
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Looping III (do … while statement)
Branching statements Kingdom of Saudi Arabia
Selection Control Structure
Life is Full of Alternatives Part 3
Presentation transcript:

Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language: One-Way Selection Two-Way Selection Multi-Way Selection Nested Structures Selection statements in C++ language

Programming Fundamentals by Dr. Nadia Y. Yousif2 Selections.. cont. The selection statement allows to choose a set of statements for execution. The selection depends on the validity of a condition when the program is executed. The condition is a logical expression that has values as true or false.

Programming Fundamentals by Dr. Nadia Y. Yousif3 One-Way Selection IF.. END IF statement (in pseudo code): Syntax: IF (condition) THEN statements END IF The semantics (execution) of this statement: - If the value of the “condition” is true, the statements between IF.. END IF are executed and the execution continues to the statement after END IF. - If the value of the “condition” is false, the statements between IF.. END IF are ignored and the execution continues from the statement that follows END IF.

Programming Fundamentals by Dr. Nadia Y. Yousif4 One-Way Selection The following figure shows the execution of this selection statement. Condition true false continue statements

Programming Fundamentals by Dr. Nadia Y. Yousif5 One-Way Selection.. Examples Example 1: Write an algorithm that takes an integer and prints its double value if it is less than 50. First, we have to analyze the problem to understand what is its requirements and how to solve it. 1- Analysis stage: Problem Input: - An integer, say n Problem Output: - The double value of n Criteria if n < 50, print its double value

Programming Fundamentals by Dr. Nadia Y. Yousif6 Example 1.. cont. 2- Algorithm Design ALGORITHM Double INPUT n IF ( n < 50 ) THEN OUTPUT “The double value is “, n * 2 END IF OUTPUT “ Finished” END

Programming Fundamentals by Dr. Nadia Y. Yousif7 Example 1.. cont. 3- Testing the algorithm n (n < 50) true The output: The double value is 24 Finished

Programming Fundamentals by Dr. Nadia Y. Yousif8 Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 1- One way selection: Syntax: if (logical expression) statements;

Programming Fundamentals by Dr. Nadia Y. Yousif9 Example 1: C++ Program // File: double.cpp /*The program corresponds to Example 1 on slide 5. It takes an integer and prints its double value if it is less than 50 */ #include void main ( ) { int n ; cin >> n ; if (n < 50) cout << “The double value is “ << n * 2 << endl ; cout << “ Finished “ << endl; }

Programming Fundamentals by Dr. Nadia Y. Yousif10 Example 2 Write an algorithm that takes two integers and prints the smallest number. Use only one output statement. 1- Analysis stage: Problem Input: - Two integers, say num1 and num2 Problem Output: - The smaller number Criteria Print the smaller number using only one output statement

Programming Fundamentals by Dr. Nadia Y. Yousif11 Example 2.. cont. 2- Algorithm Design ALGORITHM Smallest INPUT num1, num2 temp  num1 IF ( num2 < num1 ) THEN temp  num2 END IF OUTPUT “The smallest number is “, temp END Smallest

Programming Fundamentals by Dr. Nadia Y. Yousif12 Example 2.. cont. 3- Testing the algorithm num1num2 temp (num2<num1) true 3 The output: The smallest number is 3

Programming Fundamentals by Dr. Nadia Y. Yousif13 Two-Way Selection IF.. THEN.. ELSE statement (in pseudo code): This statement chooses- statements to run- from two sets of choices. Syntax: IF (condition) THEN statements1 ELSE statements2 END IF

Programming Fundamentals by Dr. Nadia Y. Yousif14 Two-Way Selection.. cont. The semantics (execution) of this statement: - If the value of the “condition” is true, the statements after THEN are executed and the execution continues to the statement after END IF. - If the value of the “condition” is false, the statements after ELSE are executed and the execution continues from the statement that follows END IF. - The following figure shows this execution:

Programming Fundamentals by Dr. Nadia Y. Yousif15 Two-Way Selection.. cont. condition statements2statements1 continue true false

Programming Fundamentals by Dr. Nadia Y. Yousif16 Two-Way Selection.. Examples Example 3: Write an algorithm that takes two integers and prints the smallest number with appropriate message. 1- Analysis stage: Problem Input: - Two integers, say num1 and num2 Problem Output: - The smaller number

Programming Fundamentals by Dr. Nadia Y. Yousif17 Example 3.. cont. 2- Algorithm Design ALGORITHM Smaller INPUT num1, num2 IF ( num1 < num2 ) THEN OUTPUT “The smaller number is “, num1 ELSE OUTPUT “The smaller number is “, num2 END IF END Smaller

Programming Fundamentals by Dr. Nadia Y. Yousif18 Example 3.. cont. 3- Testing the algorithm num1num2 (num2<num1) true The output: The smaller number is 3

Programming Fundamentals by Dr. Nadia Y. Yousif19 Selection Statements in C++ 2- Two way selection: Syntax: if (logical expression) statements1; else statements2;

Programming Fundamentals by Dr. Nadia Y. Yousif20 Example 2: C++ Program // File: Smaller.cpp /*The program corresponds to Example 3 on slide 14. It takes two integers and prints the smaller value using one output statement*/ #include void main ( ) { int num1, num2 ; cin >> num1 >> num2 ; if ( num1 < num2 ) cout << “The smaller value is “ << num1 << endl ; else cout << “The smaller value is “ << num12 << endl ; }

Programming Fundamentals by Dr. Nadia Y. Yousif21 Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a) Multi way selection by nested IF structure (b) Multi way selection by SWITCH structure

Programming Fundamentals by Dr. Nadia Y. Yousif22 Multi Way Selection by Nested IF Structure The structure that contains another structure of the same type is called a nested structure. In the Nested IF structure, the statements that exists between IF and ELSE or between IF and END IF can contain IF statement.

Programming Fundamentals by Dr. Nadia Y. Yousif23 Multi Way Selection by Nested If Structure.. Cont. Syntax of one possible structure: IF (condition1) THEN Statements1 ELSE IF (condition2) THEN Statements2 ELSE IF (Condition3) THEN Statements3 ELSE IF (Condition4) THEN Statements4 END IF Note: The nest can be to many levels. The following figure shows the execution of this structure.

Programming Fundamentals by Dr. Nadia Y. Yousif24 Multi Way Selection by Nested If Structure.. Cont. Condition1 Condition2 Statements2 Statements3 Rest of algorithm Statements1 Condition3 Statements4 True False

Programming Fundamentals by Dr. Nadia Y. Yousif25 Multi Way Selection by Nested If Structure.. Examples Example 4: Write an algorithm that inputs a student mark and outputs the corresponding grade, where grades are as follows: mark grade A 80-89B 70-79C 60-69D < 60E

Programming Fundamentals by Dr. Nadia Y. Yousif26 Example 4.. Cont. 1- Analysis stage: Problem Input: - student’s mark, mark Problem Output: - grade Criteria - according to the previous grade table

Programming Fundamentals by Dr. Nadia Y. Yousif27 Example 4.. Cont. 2- Algorithm Design ALGORITHM Grades INPUT mark IF ( mark 100 ) THEN OUTPUT “ Mark out of range” ELSE IF ( mark  90 AND mark  100 ) THEN OUTPUT “A” ELSE IF ( mark  80 ) THEN OUTPUT “B” ELSE IF ( mark  70 ) THEN OUTPUT “C” ELSE IF ( mark  60 ) THEN OUTPUT “D” ELSE OUTPUT “E” END IF END Grades

Programming Fundamentals by Dr. Nadia Y. Yousif28 Example 3: C++ Program // File: Grades.cpp /*The program corresponds to Example 4 on slide 21. It reads a student’s mark and prints the corresponding grade */ #include void main ( ) { int mark ; cin >> mark; if ( mark 100 ) cout << “ Mark out of range” << endl; else if ( mark >= 90 ) cout << “A” << endl ; else if ( mark >= 80 ) cout << “A” << endl ; else if ( mark >= 70 ) cout << “C” << endl ; else if ( mark >= 60 ) cout << “D” << endl ; else cout << “E” << endl ; }

Programming Fundamentals by Dr. Nadia Y. Yousif29 Comparison of Nested IF Structure and a Sequence of IF Structures Some times the nested IF structure is a good solution for a given problem than a sequence of IF structures. Consider the following example: (a) Nested case: IF ( x > 0 ) THEN pos_count  pos_count + 1 ELSE IF ( x < 0 ) THEN neg_count  neg_count + 1 ELSE zero_count  zero_count + 1 END IF

Programming Fundamentals by Dr. Nadia Y. Yousif30 Comparison of Nested IF Structure and a Sequence of IF Structures ( b) Sequence case: IF ( x > 0 ) THEN pos_count  pos_count + 1 END IF IF ( x < 0 ) THEN neg_count  neg_count + 1 END IF IF ( x = 0 ) THEN zero_count  zero_count + 1 END IF

Programming Fundamentals by Dr. Nadia Y. Yousif31 Comparison of Nested IF Structure and a Sequence of IF Structures In the previous example, only one statement should be executed for any value of x. In the sequence case, the sequence does not show clearly that exactly one of the three statements is executed for any value of x. In the nested case, only one statement is executed exactly. Therefore, it is better than the sequence case.

Programming Fundamentals by Dr. Nadia Y. Yousif32 Problem : Read three numbers to print the smallest one. Program #include void main() { int a, b, c; cout >a>>b>>c; cout<<"\nMin= "; if ((a < b) && (a < c)) cout<<a; if ((b < a) && (b < c)) cout<<b; if ((c < a) && (c < b)) cout<<c; cout<<endl; }

Programming Fundamentals by Dr. Nadia Y. Yousif33 Program2 (nested if) #include void main() { int a, b, c; cout >a>>b>>c; cout<<"\nMin= "; if (a < b) if (a < c) cout<<a; else cout<<c; else if (b < c) cout<<b; else cout<<c; cout<<endl; }