Selection Control Structure

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/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CS 1400 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
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)
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
TK 1914 : C++ Programming Control Structures I (Selection)
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
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++
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
 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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Decision making If.. else statement.
The Ohio State University
Branching statements.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 4: Control Structures I (Selection)
The if…else Selection Statement
Chapter 3 Control Statements
REPETITION CONTROL STRUCTURE
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Making Decisions.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
A mechanism for deciding whether an action should be taken
EGR 2261 Unit 4 Control Structures I: Selection
Variables A piece of memory set aside to store data
The Selection Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5: Loops and Files.
Topics The if Statement The if-else Statement Comparing Strings
Programming Fundamentals
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Control Structures – Selection
Topics The if Statement The if-else Statement Comparing Strings
Expression Review what is the result and type of these expressions?
Chapter 4: Control Structures I (Selection)
Programming Funamental slides
Decision making If statement.
Chapter 4 Selection.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Boolean Expressions to Make Comparisons
Beginning C Lecture 3 Lecturer: Dr. Zhao Qinpei
Repetition Statements (Loops) - 2
CS 101 First Exam Review.
Relational and Logical Operators
Using C++ Arithmetic Operators and Control Structures
Relational and Logical Operators
HNDIT11034 More Operators.
Selection Control Structure
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Control Structures.
Presentation transcript:

Selection Control Structure

2) Relational Operators 1) Boolean values 3) Logical Operators 4) Type of selection control structures

Boolean values bool bValue; bValue = true; Boolean values only have two possible values: true (1) and false (0). To declare a boolean variable, we use the keyword bool (data type). when converting integers to booleans, the integer zero resolves to boolean false, whereas non-zero integers all resolve to true. bool bValue; bValue = true;

Relational operators

Relational operator Relational operators are used to compare two values to form a condition. Math C++ Plain English = == equals [example: if(a==b) ] [ (a=b) means put the value of b into a ] < < less than  <= less than or equal to > > greater than  >= greater than or equal to  != not equal to

Relational operator Relational operators: Allow comparisons Require two operands (binary) Return 1 if expression is true, 0 otherwise. Comparing values of different data types may produce unpredictable results For example, 8 < '5' should not be done.

Simple Boolean Expressions Example: 8 < 15 = true 2.5 >= 5.8 = false Exercise: 6 != 6 5.9 <= 7.5 False True

Logical operators

Logical operator OPERATOR DESCRIPTION ! Not && AND || OR The AND operator will give the value TRUE when all expression are TRUE The OR operator will give the value TRUE if any expression is TRUE Putting ! in front of a logical expression reverses its value

logical operator Truth table 1 1 1 1

Precedence of operator Precedence of operators (from highest to lowest) Parentheses ( … ) Unary operators ! Multiplicative operators * / % Additive operators + - Relational ordering < <= >= > Relational equality == != Logical and && Logical or || Assignment =

Logical expression

Logical expression Expression that can be thought of as being true or false. True if correct or false if not correct. The expression uses relational operators such as == and < and logical operators such as &&, ||, and !

Logical expression Logical expressions can be unpredictable The following expression appears to represent a comparison of 0, num = 12, and 10: 0 <= num <= 10 It always evaluates true because 0<=12 evaluates to 1, and 1 <= 10 is true. A correct way to write this expression is: 0 <= num && num <= 10

Logical expression Example: (true || false) (7>3 && 5<2)

Exercises x<y && z>y x<=z && x>0 x>=y && z==3 Assume x =5, y = 6 and z =7 Boolean expression Value x<y && z>y x<=z && x>0 x>=y && z==3 y<15||!(y>0) x>y||z>y && !(z<=0) 11011

Exercises Evaluate the following expression: (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) = (17 < 12 + 5 ) || (16 == 16) && !(6 == 6) = (17 < 17 ) || true && !(true) = false || true && false = false || false = false = 0

Decision control structure

Decision control instruction You use the decision control structure or selection control sturcture when you want program to make a decision or comparison and then select one of two paths, depending on the result of that decision or comparison. Example 1 Example 2 If (it is raining) wear a rain coat bring an umbrella If (you have a test tomorrow) study tonight Else watch a movie

Decision control structure 1 One way selection 2 Two way selection 3 Multiple selection

One way selection The syntax of one-way selection is: if (expression) statement; Statement is executed if the value of the expression is true (1) Statement is by passed if the value is false (0); program goes to the next statement

Boolean expression in if condition if (quantity <50) if (age>=25) if (onhand == target) if (quantity != 7500)

One way selection Pseudocode: Flowchart: if (expression) then statements end if Expression True False Statements

Example Problem statement: If the speed of the car is greater than 80km/h, then output a message “you are too fast!” Start Begin input speed if speed > 80 output “you are too fast!” end if End Input speed T You are too fast F speed>80 End

Example #include <iostream.h> void main() { int speed; cin >> speed; if (speed > 80) cout << “you are too fast!”; }

Exercises 1 #include <iostream.h> void main() { int mark; cin >> mark; if (mark >= 50) cout << “PASSED!”; } Ask student to do pseudocode and flowchart also What is the output if I enter 45???

Exercises 2 #include <iostream.h> void main() { int mark; cin >> mark; if (mark >= 50) cout << “PASSED!”; cout<< “thank you for using the program”<<endl; } Ask student to do pseudocode and flowchart also What is the output if I enter 45 and then 50???

Compound statements Use the symbols { and } to group several statements as a single unit. Simple statements within the compound statements end with semicolons. Compound Statements are sometimes called a statement block. Use compound statements in an if statement if you want several actions executed when the Boolean expression is true.

Compound statements (cont.) void main() { int mark; cin >> mark; if (mark >= 50) cout << “PASSED!”; cout<< “thank you”<<endl; } void main() { int mark; cin >> mark; if (mark >= 50) cout << “PASSED!”; cout<< “thank you”<<endl; } Single statement compound statements

Two way selection Two-way selection takes the form: if (expression) statement1; else statement2; If expression is true, statement1 is executed otherwise statement2 is executed statement1 and statement2 are any C++ statements if and else is a reserved word

Two way selection Pseudocode: if (expression) then statement1 else end if

Two way selection Flowchart: True False Statement1 Statement2 Expression True Statement1 False Statement2

Example Program segment Flowchart if (cgpa < 2.0) status = ‘F’; else status = ‘P’; if (choice == 1) gender = ‘F’; gender = ‘M’; cgpa < 2.0 false true P F choice == 1 false true M F

Example Problem statement: If student marks greater than or equal to 50, then output a message “pass”, else “fail”. Pseudocode: Begin input marks if marks >= 50 then output “pass” else output “fail” end if End

Example Flowchart Start Input marks marks>=40 T Pass F Fail End

Example #include <iostream.h> void main() { int marks; cin >>marks; if (marks >= 50) cout << “pass”; else cout << “fail”; }

Compound statements

Compound statements (cont.) if (marks >= 50) { cout << “pass”<<endl; cout << “Good job!!”; } else cout << “fail”; cout << “Try again!!”;

Compound statements (cont.) Syntax and code: #include <iostream.h> void main() { int marks; cin >> marks; if (marks >= 50) cout << “pass”; else cout << “fail”; cout << “you have to have to repeat”; }

Exercise Assume that a program need to determine whether a student’s gender is male or female. If student’s gender equal to ‘f’ then display female; otherwise display male. Write pseudocode for the selection structure. Write a C++ if statement that corresponds to the pseudocode in question 1.

Exercise (cont.) Problem statement: A shopkeeper sells a magazine for RM2.00 each. However, if the customer buys more than 10 magazines, the price of the magazine is only RM1.50 each. Calculate the total the customer has to pay based on the quantity that he buys.

Total = quantity * price Start Input quantity T Display total F quantity>10 price = 2.00 price = 1.50 Total = quantity * price Begin input quantity if quantity > 10 then price = 1.50 else price = 2.00 total = price * quantity output total End End

Syntax and code #include <iostream.h> void main() { int quantity = 0; float total = 0; cin >> quantity; if (quantity > 10) price = 1.50; else price = 2.00 total = quantity * price; cout << total; }

Exercise (one way VS two way) Determine the output of both program segment: A group of one way selection structure: Two way selection structure: int x = -3; if(x<10) x = * x; If(x<100) x = x + 10; cout<<x; int x = -3; if(x<10) x = * x; else x = x + 10; cout<<x;

Thank You !