4-Selection_short

4-Selection_short - C++ Programming Control Structures I...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C++ Programming Control Structures I (Selection) Control Structures A computer can proceed: In sequence All statements are executed in the order the come. Selectively (branch) - making a choice Some statements are executed only if certain conditions are met A condition is represented by a logical (Boolean) expression that can be true or false A condition is met if it evaluates to true Repetitively (iteratively) looping Repeating the program or a section of it many times. Relational Operators Relational operators: Allow comparisons Require two operands (binary) Return 1(true) if expression is true , (false) otherwise Comparing values of different data types may produce unpredictable results For example, 8 < '5' should not be done Any nonzero value is treated as true Comparing string Types Relational operators can be applied to strings Strings are compared character by character, starting with the first character Comparison continues until either a mismatch is found or all characters are found equal If two strings of different lengths are compared and the comparison is equal to the last character of the shorter string The shorter string is less than the larger string string Comparison Example Suppose we have the following declarations: string str1 = "Hello"; string str2 = "Hi"; string str3 = "Air"; string str4 = "Bill"; str1 > str2 false str1 < str2 true str3 < An true str1 > Hen false str1 == hello false str1 == Hello true str2 > str4 true str3 <= str4 true str5 <= Bigger true str4 >= Billy false Logical (Boolean) Operators Logical (Boolean) operators enable you to combine logical expressions Three logical (Boolean) operators: C++ means Results ! Not Reverse the condition && and If all conditions are true true || or If one of the conditions true true Logical operators take logical values as operands and yield logical values as results ! is unary; Putting ! in front of a logical expression reverses its value !(6 < 7) false | | (or) and &&(and) are binary operators Logical/Boolean operators Condition 1 C1 Condition 2 C2 And C1 && C2 Or C1 || C2 Not !C1 True True True True False False True False True True True False False True False False False False False True Truth Table int A = 5, B = 4, C = 3 ; C < B < A in algebra it makes sense B is in the middle, but in C++ NOT C < B && B < A in C++ each operation must be checked and then the results of each part are combined together using boolean operators. Precedence of Operators Relational and logical operators are evaluated from left to right The associativity is left to right Parentheses can override precedence One-Way ( if ) Selection The syntax of one-way selection is: if (expression) statement Statement is executed if the value of the expression is true Statement is bypassed if the value is...
View Full Document

This note was uploaded on 05/03/2009 for the course CSCI 101L taught by Professor Ghyam during the Spring '07 term at USC.

Page1 / 54

4-Selection_short - C++ Programming Control Structures I...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online