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.

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

If Statements & Relational Operators Programming.
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.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
C++ for Engineers and Scientists Third Edition
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
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.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 05 (Part III) Control Statements: Part II.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
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.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
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 Making Decisions 4. Relational Operators 4.1.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CPS120: Introduction to Computer Science Decision Making in Programs.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
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 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
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)
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.
A First Book of C++ Chapter 4 Selection.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
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.
A mechanism for deciding whether an action should be taken
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Control Structures: Selection Statement
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Structured Program Development in C++
Control Structures: Selection Statement
Presentation transcript:

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 in C++.

THE if STATEMENT The if statement allows conditional execution. Its syntax is if (condition) statement; where condition is an integral expression and statement is any executable statement. The statement will be executed only if the value of the integral expression is nonzero. Notice the required parentheses around the condition. Example: int main() { int n,d; cout << "Enter two positive integers: "; cin >> n >> d; if (n%d) cout << n << " is not divisible by " << d << endl; }

THE if..else STATEMENT The if..else statement causes one of two alternative statements to execute depending upon whether the condition is true. Its syntax is if (condition) statement1; else statement2; where condition is an integral expression and statement1 and statement2 are executable statements. If the value of the condition is nonzero then statement1 will execute; otherwisestatement2 will execute. Example: int main() { int n,d; cout << "Enter two positive integers: "; cin >> n >> d; if (n%d) cout << n << " is not divisible by " << d << endl; else cout << n << " is divisible by " << d << endl; }

KEYWORDS A keyword in a programming language is a word that is already defined and is reserved for a unique purpose in programs written in that language. Standard C++ now has 74 keywords:

COMPARISON OPERATORS The six comparison operators are x < y // x is less than y x > y // x is greater than y x <= y // x is less than or equal to y x >= y // x is greater than or equal to y x == y // x is equal to y x != y // x is not equal to y Example: int main() { int n1,n2,n3; cout << "Enter three integers: "; cin >> n1 >> n2 >> n3; int min=n1; // now min <= n1 if (n2 < min) min = n2; // now min <= n1 and min <= n2 if (n3 < min) min = n3; // now min <= n1,min <= n2,and min <= n3 cout << "Their minimum is " << min << endl; }

STATEMENT BLOCKS A statement block is a sequence of statements enclosed by braces{ }, like this: { int temp=x; x = y; y = temp; } In C++ programs, a statement block can be used anywhere that a single statement can be used. EXAMPLE //This program inputs two integers and then outputs them in increasing order: int main() { int x,y; cout << "Enter two integers: "; cin >> x >> y; if (x > y) { int temp=x; x = y; y = temp; } // swap x and y cout << x << " <= " << y << endl; }

COMPOUND CONDITIONS Conditions such asn % dand x >= y can be combined to form compound conditions. This is done using the logical operators && (and), || (or), and ! (not). They are defined by –p && q evaluates to true if and only if both p and q evaluate to true –p || q evaluates to false if and only if both p and q evaluate to false –!p evaluates to true if and only if p evaluates to false –For example, (n % d || x >= y) will be false if and only ifn % dis zero and x is less than y. Example: int main() { int n1,n2,n3; cout << "Enter three integers: "; cin >> n1 >> n2 >> n3; if (n1 <= n2 && n1 <= n3) cout << "Their minimum is " << n1 <<endl; if (n2 <= n1 && n2 <= n3) cout << "Their minimum is " << n2 <<endl; if (n3 <= n1 && n3 <= n2) cout << "Their minimum is " << n3 <<endl; }

BOOLEAN EXPRESSIONS A boolean expression is a condition that is either true or false.

NESTED SELECTION STATEMENTS Like compound statements, selection statements can be used wherever any other statement can be used. So a selection statement can be used within another selection statement. This is called nesting statements Example: int main() { int n,d; cout << "Enter two positive integers: "; cin >> n >> d; if (d != 0) if (n%d == 0) cout << d << " divides " << n << endl; else cout << d << " does not divide " << n << endl; }

THE else if CONSTRUCT Nested if..else statements are often used to test a sequence of parallel alternatives, where only the else clauses contain further nesting. In that case, the resulting compound statement is usually formatted by lining up the else if phrases to emphasize the parallel nature of the logic. Example: int main() { char language; cout << "Engl.,Fre n.,Ger.,Ital.,or Rus.? (e|f|g|i|r): "; cin >> language; if (language == 'e') cout << "Welcome to ProjectEuclid."; else if (language == 'f') cout << "Bon jour,ProjectEuclid."; else if (language == 'g') cout << "Guten tag,ProjectEuclid."; else if (language == 'i') cout << "Bon giorno,ProjectEuclid."; else if (language == 'r') cout << "Dobre utre,ProjectEuclid."; else cout << "Sorry; we don't speak your language."; }

THE switch STATEMENT The switch statement can be used instead of the else if construct to implement a sequence of parallel alternatives. Its syntax is switch (expression) { case constant1: statementList1; case constant2: statementList2; case constant3: statementList3; : case constantN: statementListN; default: statementList0; } This evaluates the expression and then looks for its value among the case constants. If the value is found among the constants listed, then the statements in the corresponding statementList are executed. Otherwise if there is a default (which is optional), then the program branches to its statementList. The expression must evaluate to an integral type

EXAMPLE Using a switch Statement to Select a Range of Scores int main(){ int score; cout > score; switch (score/10) { case 10: case 9: cout << "Your grade is an A." << endl; break; case 8: cout << "Your grade is a B." << endl; break; case 7: cout << "Your grade is a C." << endl; break; case 6: cout << "Your grade is a D." << endl; break; case 5: case 4: case 3: case 2: case 1: case 0: cout << "Your grade is an F." << endl; break; default: cout << "Error: score is out of range.\n"; } cout << "Goodbye." << endl; }

THE CONDITIONAL EXPRESSION OPERATOR C++ provides a special operator that often can be used in place of the if...else statement. It is called the conditional expression operator. It uses the ? and the : symbols in this syntax: condition ? expression1 : expression2 It is a ternary operator; i.e., it combines three operands to produce a value. That resulting value is either the value of expression1 or the value of expression2, depending upon the boolean value of the condition. For example, the assignment min = ( x<y ? x : y ); would assign the minimum of x and y to min, because if the condition x<y is true, the expression ( x<y ? x : y ) evaluates to x; otherwise it evaluates to y. Conditional expression statements should be used sparingly: only when the condition and both expressions are very simple.