User Tools

Site Tools


cplusplus:control_structures_selection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cplusplus:control_structures_selection [2013/02/20 00:03] mithatcplusplus:control_structures_selection [2017/01/31 23:57] (current) – [C++ logical operators] mithat
Line 2: Line 2:
 ~~NOTOC~~ ~~NOTOC~~
  
-====== Control Structures: Repetition in algorithms ======+====== Control Structures: Selection ======
  
-Mithat Konar\\  +Mithat Konar\\
-2013-02-03+
  
 ===== Introduction ===== ===== Introduction =====
  
-  * Input validation +  * **Control structures** are used to affect how statements are executed. 
-  Sentinel/flag control +    * Sometimes also called **flow control**.
-    * Menus +
-  Counter control +
-  Iterator+
  
-===== Input validation ===== +===== Control structures in C++ =====
-    * //Get input value from user\\ While input value is not valid\\ Get input value from user//+
  
-===== Sentinel/flag control =====+  * Three kinds of control structures are used in C++ and many other languages: 
 +    * **Sequence** 
 +      * Statements are executed one after the other. 
 +      * This is C++'s default behavior! 
 +    * **Selection structures** 
 +      * Used to choose among alternative courses of action. 
 +      * "Making decisions" 
 +    * **Repetition structures** 
 +      * Used to repeat a set of instructions. 
 +      * "Looping"
  
-  * **Sentinel**: Value(s) that indicate(s) a special condition; "special value+===== Single-entry/single-exit structures ===== 
-  * **Flag**: sometimes synonym for sentinelsometimes means sentinel that is a Boolean value + 
-  * Sentinel controlled repetition+  Control structures that are entered from one point and exited from one point. 
-    * Repeat as long as a sentinel condition is not matched +  Connect the exit point of one control structure to entry point of the next: **control-structure stacking**. 
-    * //While input value is not -999\\ do something// +  * Makes programs easy to build. 
-  * Input validation might be considered a special case of sentinel controlled repetition.+ 
 +===== Selection structures ===== 
 +  * Four selection structures available in C++: 
 +    * //if// 
 +    * //if/else// 
 +    * //nested if/else// 
 +    * //switch// 
 +  * Also has a ternary //conditional operator// that performs selection. 
 + 
 +===== if selection structure ===== 
 + 
 +  * Used to do something if some condition is "true"
 +  * Pseudocode example:<code>if student’grade is greater than 60 
 +  print "Passed"</code> If the condition is ''true'' the print statement is executed and program goes on to next statement. If the condition is ''false'' the print statement is ignored and the program goes onto the next statement. 
 +  * The ''if'' structure is a single-entry/single-exit structure. 
 + 
 +===== if syntax ===== 
 + 
 +  * C++ syntax:<code c++>if (<expression>) 
 +  <statement>;</code> 
 +  * Example:<code c++>if (grade > 60) 
 +  cout << "Passed" << endl;</code> 
 +  * [[selection_examples#if|complete example]] 
 + 
 +===== Relational operators ===== 
 + 
 +  * **Relational operators** are used to compare values and return a ''true'' or ''false'' value. 
 +  * A value that is either ''true'' or ''false'' is a **Boolean** value. 
 +    * ''bool'' type in C++ 
 + 
 +===== C++ relational operators ===== 
 +  * C++ supports the following relational operators: 
 +    * ''%%>%%'' greater than 
 +    * ''%%<%%'' less than 
 +    * ''%%>=%%'' greater than or equal to 
 +    * ''%%<=%%'' less than or equal to 
 +    * ''%%==%%'' equal to (equality) 
 +    * ''%%!=%%'' not equal to (inequality) 
 +    * Note: Do not confuse the ''%%==%%'' equality operator with the ''%%=%%'' assignment operator. This is common mistake and can cause hard-to-find errors! 
 + 
 +===== Order of precedence ===== 
 + 
 +^operator ^associativity ^type ^ 
 +| %%()%% | left to right | parenthesis | 
 +| %%* / %% % | left to right | multiplicative | 
 +| %%+ -%% | left to right | additive | 
 +| %%<< >>%% | left to right | stream insertion/extraction | 
 +| %%< <= > >=%% | left to right | relational | 
 +| %%== !=%%  | right to left | equality | 
 +| %%=%% | right to left | assignment | 
 + 
 +===== if/else selection structure ===== 
 + 
 +  * Used to do one thing if some condition is "true", something else if the condition is "false"
 +  * Pseudocode example:<code>if student’s grade is greater than or equal to 60 
 +  print “Passed” 
 +else 
 +  print "Failed"</code> If the condition is true the "Passed" print statement is executed and the program goes on to next statement. If the condition is false the "Failed" print statement is executed and the program goes onto the next statement. 
 +  * The ''if/else'' structure is a single-entry/single-exit structure. 
 + 
 +===== if/else syntax ===== 
 + 
 +  * C++ syntax:<code C++>if (<expression>
 +  <statement>; 
 +else 
 +  <statement>;</code> 
 +  * Example:<code C++>if (grade >= 60) 
 +  cout << "Passed<< endl; 
 +else 
 +  cout << "Failed" << endl;</code> 
 +  * [[selection_examples#if_else|complete example]] 
 + 
 +===== Nested if/else selection structure ===== 
 + 
 +  Placing //if/else// selection structures inside //if/else// selection structures can be used to test for multiple cases. 
 + 
 +===== Nested if/else pseudocode ===== 
 +  "Logical" formatting:<code>if student’s grade is greater than or equal to 90 
 +  print "A" 
 +else 
 +  if student’s grade is greater than or equal to 80 
 +    print "B" 
 +  else 
 +    if student’s grade is greater than or equal to 70 
 +      print "C" 
 +    else 
 +     if student’s grade is greater than or equal to 60 
 +       print "D" 
 +     else 
 +       print "F"</code> 
 + 
 +===== Nested if/else pseudocode ===== 
 +  "Readability" formatting:<code>if student’s grade is greater than or equal to 90 
 +  print "A" 
 +else if student’s grade is greater than or equal to 80 
 +  print "B" 
 +else if student’s grade is greater than or equal to 70 
 +  print "C" 
 +else if student’s grade is greater than or equal to 60 
 +  print "D" 
 +else 
 +  print "F"</code> 
 +   
 +===== Nested if/else syntax ===== 
 +  C++ syntax:<code c++>if (grade >= 90) 
 +  cout << "A" << endl; 
 +else if (grade >= 80) 
 +  cout << "B" << endl; 
 +else if (grade >= 70) 
 +  cout << "C" << endl; 
 +else if (grade >= 60) 
 +  cout << "D" << endl; 
 +else 
 +  cout << "F" << endl;</code> 
 +  * [[selection_examples#nested_if_else|complete example]] 
 + 
 +===== Compound statements ===== 
 + 
 +  * A **simple statement** in C++ is any statement that ends with semicolon: <code c++>cout << "This is a simple statement." << endl;</code> 
 +  * A **compound statement** is set of simple statements placed between curly brackets: <code c++>{ 
 +  x = 3 * y; 
 +  cout << "The magic number is: " << x << endl; 
 +}</code> 
 + 
 +===== Compound statement example ===== 
 +  * A compound statement can be used anywhere simple statement can be used, for example in an if statement:<code c++>if (grade >= 60) 
 +  cout << "Passed." << endl; 
 +else 
 +
 +  cout << "Failed." << endl; 
 +  cout << "You must take this course again." << endl; 
 +}</code> 
 +  * Without the brackets, the statement<code c++>cout << "You must take this course again." << endl;</code> would be executed no matter what. 
 + 
 +===== Blocks ===== 
 + 
 +  * A compound statement with variable declarations is called a **block**. 
 +  * C++ example:<code c++>if (grade >= 60) 
 +  cout << "Passed." << endl; 
 +else 
 +
 +  char yourGrade; 
 +  yourGrade = 'F'; 
 +  cout << "You received a grade of " << yourGrade << endl; 
 +  cout << "You must take this course again." << endl; 
 +}</code> 
 + 
 +===== Logical operators ===== 
 + 
 +  * **Logical operators** are used to implement standard logical operations. 
 +  * Like relational operators, logical operators return Boolean values (i.e., they form **Boolean expressions**). 
 + 
 +===== C++ logical operators ===== 
 + 
 +  * ''&&'' (logical AND) 
 +    * Takes two operands 
 +    * Returns ''true'' if //both// operands are ''true'', returns ''false'' otherwise. 
 +  * ''||'' (logical OR) 
 +    * Takes two operands 
 +    * Returns ''true'' if //either// operand is ''true'', returns ''false'' otherwise. 
 +  * ''!'' (logical NOT, logical negation) 
 +    * Takes one operand 
 +    * Returns ''true'' when its operand is ''false'', ''false'' otherwise. 
 +  * Precedence: ''%%!%%'' -> ''%%&&%%'' -> ''%%||%%'' 
 + 
 +===== Truth table for logical AND ===== 
 +^expression ^//result// ^ 
 +| ''%%false && false%%'' | //false// | 
 +| ''%%false && true%%'' | //false// | 
 +| ''%%true && false%%'' | //false// | 
 +| ''%%true && true%%'' | //true// | 
 + 
 +===== Truth table for logical OR ===== 
 +^expression ^//result// ^ 
 +| ''%%false || false%%'' | //false// | 
 +| ''%%false || true%%'' | //true// | 
 +| ''%%true || false%%'' | //true// | 
 +| ''%%true || true%%'' | //true// | 
 + 
 +===== Truth table for logical NOT ===== 
 +^expression ^//result// ^ 
 +| ''%%!false%%''  | //true// | 
 +| ''%%!true%%'' | //false// | 
 + 
 +===== Examples ===== 
 +assume: ''int x = 12, y = 5, z = -4;'' 
 + 
 +^expression ^//result// ^ 
 +|''%%(x > y) && (y > z)%%'' |//true// | 
 +|''%%(x > y) && (z > y)%%'' |//false// | 
 +|''%%(x <= z) || (y == z)%%'' |//false// | 
 +|''%%(x <= z) || (y != z)%%'' |//true// | 
 +|''%%!(x >= z)%%''|//false//
 + 
 +  * [[selection_examples#validating_input|complete example]] 
 + 
 +===== Short-circuiting ===== 
 +  * **short-circuit evaluation**: evaluating only as much as necessary (from left to right) to determine the result.<code c++>int x = 5, y = 6; 
 +if ( (x < 0) && (-6 > y-1) )  // (-6 > y-1) is not evaluated 
 +  ... 
 +if ( (x > 0) || (-6 > y-1) )  // (-6 > y-1) is not evaluated 
 +  ...</code> 
 +  * Keep this in mind for later 
 + 
 +===== More about Boolean values ===== 
 + 
 +  * In C++: 
 +    * The value 0 (the integer zero) is considered ''false'', 
 +    * anything else is considered ''true''
 +  * Thus, the following example will print //foo//:<code c++>if (6) 
 +  cout << "foo" << endl; 
 +else 
 +  cout << "bar" << endl;</code> 
 +  * and the following will print //bar//:<code c++>if (0) 
 +  cout << "foo" << endl; 
 +else 
 +  cout << "bar" << endl;</code> 
 + 
 +===== Comparing characters and strings ===== 
 +  * [[selection_examples#comparing_characters|Comparing characters example]] 
 +  * See Gaddis 4.12 (programs Pr4-20.cpp and Pr4-21.cpp). 
 + 
 +===== switch structure ===== 
 +  * [[selection_examples#switch|simple switch example]] 
 +  * See Gaddis 4.14 (programs Pr4-23.cpp to Pr4-27.cpp). 
 + 
 +===== The conditional operator ===== 
 +  * C++ has //ternary// conditional operator 
 +    * Similar to an if/else control structure. 
 +    * Unlike if/else, the conditional operator //returns a value//. 
 +    * Syntax:''expr1 ? expr2 : expr3;'' 
 +    * Semantics: "If ''expr1'' is true, return the value of ''expr2'', otherwise return the value of ''expr3''." 
 +    * Examples:<code c++>y = x<0 ? -1.0*x : x; 
 +w = x<0 ? y=10 : z=20;</code> 
 + 
 +===== More about blocks and scope ===== 
 +  * See Gaddis 4.15 (programs Pr4-28.cpp to Pr4-30.cpp).
  
-===== Menus ===== 
-  * Another special case of sentinel controlled repetition can be used for menus 
  
cplusplus/control_structures_selection.1361318622.txt.gz · Last modified: 2013/02/20 00:03 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki