If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.

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.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
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.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
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.
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
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.
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.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Basic Of Computer Science
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.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
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/
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.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
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.
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
COMS 261 Computer Science I
Expression Review what is the result and type of these expressions?
Control Structures: Selection Statement
Miscellaneous Flow Control
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Control Structures: Selection Statement
Branching statements Kingdom of Saudi Arabia
COMS 261 Computer Science I
Presentation transcript:

if Statements

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

COMP104 If / Slide 3 Sequence * It is natural to write a program as a sequence of program structures such as sequences, choices, and iterations

COMP104 If / Slide 4 Choice Constructs * Provide n Ability to control whether a statement list is executed * Two constructs if statement  if  if-else  if-else-if switch statement

COMP104 If / Slide 5 The Basic if Statement * Syntax if(Expression) Action * Example: absolute value if(value < 0) value = -value; Expression Action truefalse

COMP104 If / Slide 6 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 If / Slide 7 Choice ( if ) * Put multiple action statements within braces if { }

COMP104 If / Slide 8 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 If / Slide 9 Relational Operators Relational operators are used to compare two values 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 If / Slide 10 Relational Expressions Examples: numberOfStudents < * j == 10 + i

COMP104 If / Slide 11 Operator Precedence Which comes first? * / % + - = > == != = Answer:

COMP104 If / Slide 12 The if-else Statement * Syntax of if-else if { } else{ } * Example if(v == 0) cout << "v is 0"; else cout << "v is not 0"; Expression Action 1 Action 2 true false

COMP104 If / Slide 13 Finding the Bigger 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 If / Slide 14 Selection * Often we want to perform a particular action depending on the value of an expression * Two ways to do this if-else-if statement  if-else statements “glued” together switch statement  An advanced construct

COMP104 If / Slide 15 if-else-if Statements if { } else if { } else if { } else{ }

COMP104 If / Slide 16 if-else-if Statement * 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 If / Slide 17 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 If / Slide 19 A 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) * Boolean operators can be used to form more complex conditional expressions The and operator is && The or operator is || The not operator is ! * Warning & and | are also operators

COMP104 If / Slide 20 A Boolean Type * 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 If / Slide 21 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 If / Slide 22 More Operator Precedence * Examples 5 != 6 || 7 <= 3 (5 !=6) || (7 <= 3) 5 * == 13 && 12 < 19 || !false == 5 < 24

COMP104 If / Slide 23 Nested if Statements n Nested means that one complete statement is inside another n Example: if { } }

COMP104 If / Slide 24 “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 If / Slide 25 “Dangling Else” Problem * 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 If / Slide 26 “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 // parenthesis avoid dangling else c = 3; }