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,

Slides:



Advertisements
Similar presentations
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Advertisements

If Statements & Relational Operators Programming.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Control Constructs Mechanisms for deciding when and how often an action should be taken.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
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.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
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.
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
If Statements & Relational Operators, Part 2 Programming.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 C++ Control Statements ~ Selection and Iteration.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
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.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
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.
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.
Basic Of Computer Science
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
CHAPTER 4 CONTROL STRUCTURES I Selection. In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
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.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
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.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
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.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x)
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
The Ohio State University
Branching statements.
Introduction to Computer Programming
Chapter 3 Selection 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.
A mechanism for deciding whether an action should be taken
Control Structures – Selection
Expression Review what is the result and type of these expressions?
Chapter 4: Control Structures I (Selection)
Miscellaneous Flow Control
Chapter 4 Selection.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Life is Full of Alternatives
COMS 261 Computer Science I
Presentation transcript:

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, , and 100. are doubles. The general rule for division of int and double types is: double/double -> double (normal) double/int -> double (normal) int/double -> double (normal) int/int -> int (special case: any decimal places discarded)

COMP104 Lecture 7 / Slide 3 Rules for Division l Example : 220. / double/double -> double result is / 100 double/int -> double result is / int/double -> double result is / 100 int/int -> int result is 2 Summary: division is normal unless both the numerator and denominator are int, then the result is an int (the decimal places are discarded).

COMP104 Lecture 7 / Slide 4 Forcing a Type Change l You can change the type of an expression with a cast operation l Syntax: variable1 = type(variable2); variable1 = type(expression); l Example: int x=1, y=2; double result1 = x/y;// 1/2 = 0 -> 0.0 double result2 = double(x)/y;// 1.0/2 = 0.5 -> 0.5 double result3 = x/double(y);// 1/2.0 = 0.5 -> 0.5 double result4 = double(x)/double(y); // 1.0/2.0 -> 0.5 double result5 = double(x/y); // double(1/2) = double(0) -> 0.0 int cents = int(result4*100); // cents is 50

COMP104 Lecture 7 / Slide 5 Three Program Structures l Sequence - executable statements which the computer processes in the given order l Choice - sequence(s) selected depending on some condition if { } l Iteration - repetitively executed sequences while { }

COMP104 Lecture 7 / Slide 6 Sequence l It is natural to write a program as a sequence of program structures such as sequences, choices, and iterations

COMP104 Lecture 7 / Slide 7 Choice Constructs l Provide n Ability to control whether a statement list is executed l Two constructs if statement –if –if-else –if-else-if switch statement

COMP104 Lecture 7 / Slide 8 The Basic if Statement l Syntax if(Expression) Action If the Expression is true then execute Action l Action is either a single statement or a group of statements within braces l Example: absolute value if(value < 0) value = -value; Expression Action truefalse

COMP104 Lecture 7 / Slide 9 Absolute Value // program to read number & print its absolute value #include using namespace std; int main(){ int value; cout << "Enter integer: "; cin >> value; if(value < 0) value = -value; cout << "The absolute value is " << value << endl; return 0; }

COMP104 Lecture 7 / Slide 10 Choice ( if ) l Put multiple action statements within braces if { }

COMP104 Lecture 7 / Slide 11 Sorting Two Numbers int value1; int value2; int temp; cout << "Enter two integers: "; cin >> value1 >> value2; if(value1 > value2){ temp = value1; value1 = value2; value2 = temp; } cout << "The input in sorted order: " << value1 << " " << value2 << endl;

COMP104 Lecture 7 / Slide 12 Relational Operators Relational operators are used to construct a logical expression MathC++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

COMP104 Lecture 7 / Slide 13 Relational Expressions Examples: numberOfStudents < > * j == 10 + i

COMP104 Lecture 7 / Slide 14 Operator Precedence Which comes first? * / % + - = > == != = Answer:

COMP104 Lecture 7 / Slide 15 The if-else Statement l Syntax if (Expression) Action 1 else Action 2 l If Expression is true then execute Action 1 otherwise execute Action 2 l Example if(v == 0) cout << "v is 0"; else cout << "v is not 0"; Expression Action 1 Action 2 true false

COMP104 Lecture 7 / Slide 16 Choice ( if and else ) if { } else{ }

COMP104 Lecture 7 / Slide 17 Finding the Big One int value1; int value2; int larger; cout << "Enter two integers: "; cin >> value1 >> value2; if(value1 > value2) larger = value1; else larger = value2; cout << "Larger of inputs is: " << larger << endl;

COMP104 Lecture 7 / Slide 18 Selection l Often we want to perform a particular action depending on the value of an expression l Two ways to do this if-else-if statement –if-else statements “glued” together switch statement –An advanced construct

COMP104 Lecture 7 / Slide 19 if-else-if Statements if { } else if { } else if { } else{ } Q R TS

COMP104 Lecture 7 / Slide 20 if-else-if Statements if { } else if { } else if { } else{ }

COMP104 Lecture 7 / Slide 21 if-else-if Statement l Example if(score >= 90) cout << "Grade = A" << endl; else if(score >= 80) cout << "Grade = B" << endl; else if(score >= 70) cout << "Grade = C" << endl; else if(score >= 60) cout << "Grade = D" << endl; else cout << "Grade = F" << endl;

COMP104 Lecture 7 / Slide 22 switch Statement switch(int(score)/10){ case 10: case 9: cout << "Grade = A" << endl; break; case 8: cout << "Grade = B" << endl; break; case 7: cout << "Grade = C" << endl; break; case 6: cout << "Grade = D" << endl; break; default: cout << "Grade = F" << endl; }

int left; int right; char oper; cout << "Enter simple expression: "; cin >> left >> oper >> right; cout << left << " " << oper << " " << right << " = "; switch (oper) { case '+' : cout << left + right << endl; break; case '-' : cout << left - right << endl; break; case '*' : cout << left * right << endl; break; case '/' : cout << left / right << endl; break; default: cout << "Illegal operation" << endl; }

COMP104 Lecture 7 / Slide 24 Boolean Type C++ contains a type named bool which can have one of two values true (corresponds to non-zero value) false (corresponds to zero value) l Boolean operators can be used to form more complex conditional expressions The and operator is && The or operator is || The not operator is ! l Warning & and | are bit-wise operators n please do NOT confuse them with boolean operators

COMP104 Lecture 7 / Slide 25 A Boolean Type l Example logical expressions bool P = true; bool Q = false; bool R = true; bool S = P && Q; bool T = !Q || R; bool U = !(R && !Q);

COMP104 Lecture 7 / Slide 26 More Operator Precedence Precedence of operators (from highest to lowest) Parentheses ( … ) Unary operators ! Multiplicative operators * / % Additive operators + - Relational ordering = > Relational equality == != Logical and && Logical or || Assignment =

COMP104 Lecture 7 / Slide 27 More Operator Precedence l Examples 5 != 6 || 7 <= 3 (5 !=6) || (7 <= 3) 5 * == 13 && 12 < 19 || !false == 5 < 24 5 * == 13 && 12 < 19 || (!false) == 5 < 24 (5 * 15) + 4 == 13 && 12 < 19 || true == 5 < 24 ((5 * 15) + 4) == 13 && 12 < 19 || true == 5 < 24 ((5 * 15) + 4) == 13 && (12 < 19) || true == (5 < 24) (((5 * 15) + 4) == 13) && true || (true == true) false && true || true (false && true)|| true true Parentheses ( … ) Unary operators ! Multiplicative operators * / % Additive operators + - Relational ordering = > Relational equality == != Logical and && Logical or || Assignment =

COMP104 Lecture 7 / Slide 28 Finding the largest number Consider three numbers, A, B and C How many ways can they be ordered? l A > B > C l A > C > B l B > A > C l B > C > A l C > A > B l C > B > A

COMP104 Lecture 7 / Slide 29 int a=3, b=2, c=1; if ((a > b > c) || (a > c > b)) cout << a << endl; else if ((b > a > c) || (b > c > a)) cout << b << endl; else if ((c > a > b) || (c > b > a)) cout << c << endl; What’s Wrong? a > b > c (a > b) > c (3 > 2) > 1 true > 1 1 > 1 false a > c > b (a > c) > b (3 > 1) > 2 true > 2 1 > 2 false

COMP104 Lecture 7 / Slide 30 Correct program int a=2, b=2, c=1; if ((a > b) && (b > c) || (a > c) && (c > b)) cout << a << endl; else if ((b > a) && (a > c) || (b > c) && (c > a)) cout << b << endl; else if ((c > a) && (a > b) || (c > b) && (b > a)) cout << c << endl;

COMP104 Lecture 7 / Slide 31 Nested if Statements n Nested means that one complete statement is inside another (nested scopes) if (condition_1) { if (condition_2) { if (condition_3) { cout << “123”; } cout << “12”; } cout << “1”; } Scope of if-statement

COMP104 Lecture 7 / Slide 32 “Dangling Else” Problem Problem: Nested if statements can seem ambiguous in their meaning. What is the value of c after the following is executed? int a=-1, b=1, c=1; if(a>0) if(b>0) c = 2; else c = 3;

COMP104 Lecture 7 / Slide 33 “Dangling Else” Problem l C++ groups a dangling else with the most recent if. The following indentation shows how C++ would group this example (answer: c=1 ). int a=-1, b=1, c=1; if(a>0) if(b>0) c = 2; else // dangling else grouped to nearest if c = 3;

COMP104 Lecture 7 / Slide 34 “Dangling Else” Problem Use extra brackets { } to clarify the intended meaning, even if not necessary. int a=-1, b=1, c=1; if(a>0){ if(b>0){ c = 2; else // use { } to avoid dangling else c = 3; }