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/03 06:04] mithatcplusplus:control_structures_selection [2017/01/31 23:57] (current) – [C++ logical operators] mithat
Line 4: Line 4:
 ====== Control Structures: Selection ====== ====== Control Structures: Selection ======
  
-Mithat Konar\\  +Mithat Konar\\
-2013-02-02+
  
 ===== Introduction ===== ===== Introduction =====
Line 15: Line 14:
  
   * Three kinds of control structures are used in C++ and many other languages:   * Three kinds of control structures are used in C++ and many other languages:
-    * **Sequence control**+    * **Sequence**
       * Statements are executed one after the other.       * Statements are executed one after the other.
-      * This happens by default in C++.+      * This is C++'s default behavior!
     * **Selection structures**     * **Selection structures**
       * Used to choose among alternative courses of action.       * Used to choose among alternative courses of action.
Line 27: Line 26:
 ===== Single-entry/single-exit structures ===== ===== Single-entry/single-exit structures =====
  
-  * **Single-entry/single-exit structures**: +  * Control structures that are entered from one point and exited from one point. 
-    * Control structures that are entered from one point and exited from one point. +  * Connect the exit point of one control structure to entry point of the next: **control-structure stacking**. 
-    * Connect the exit point of one control structure to entry point of the next: **control-structure stacking**. +  * Makes programs easy to build.
-    * Makes programs easy to build.+
  
 ===== Selection structures ===== ===== Selection structures =====
-  * There are four selection structures available in C++:+  * Four selection structures available in C++:
     * //if//     * //if//
     * //if/else//     * //if/else//
     * //nested if/else//     * //nested if/else//
     * //switch//     * //switch//
 +  * Also has a ternary //conditional operator// that performs selection.
  
 ===== if selection structure ===== ===== if selection structure =====
Line 44: Line 43:
   * Pseudocode example:<code>if student’s grade is greater than 60   * Pseudocode example:<code>if student’s 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.   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 ===== ===== if syntax =====
  
-  * C++ syntax:<code c++>if (grade > 60)+  * C++ syntax:<code c++>if (<expression>
 +  <statement>;</code> 
 +  * Example:<code c++>if (grade > 60)
   cout << "Passed" << endl;</code>   cout << "Passed" << endl;</code>
   * [[selection_examples#if|complete example]]   * [[selection_examples#if|complete example]]
-  * The ''if'' structure is a single-entry/single-exit structure. 
  
 ===== Relational operators ===== ===== Relational operators =====
Line 86: Line 87:
 else 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.   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 ===== ===== if/else syntax =====
  
-  * C++ syntax:<code C++>if (grade >= 60)+  * C++ syntax:<code C++>if (<expression>
 +  <statement>; 
 +else 
 +  <statement>;</code> 
 +  * Example:<code C++>if (grade >= 60)
   cout << "Passed" << endl;   cout << "Passed" << endl;
 else else
   cout << "Failed" << endl;</code>   cout << "Failed" << endl;</code>
   * [[selection_examples#if_else|complete example]]   * [[selection_examples#if_else|complete example]]
-  * The ''if/else'' structure is a single-entry/single-exit structure. 
  
 ===== Nested if/else selection structure ===== ===== Nested if/else selection structure =====
Line 144: Line 149:
   * A **simple statement** in C++ is any statement that ends with a semicolon: <code c++>cout << "This is a simple statement." << endl;</code>   * A **simple statement** in C++ is any statement that ends with a semicolon: <code c++>cout << "This is a simple statement." << endl;</code>
   * A **compound statement** is a set of simple statements placed between curly brackets: <code c++>{   * A **compound statement** is a set of simple statements placed between curly brackets: <code c++>{
-  cout << "I am a "+  x = 3 * y
-  cout << "compound statement." << endl;+  cout << "The magic number is: << x << endl;
 }</code> }</code>
  
Line 180: Line 185:
   * ''&&'' (logical AND)   * ''&&'' (logical AND)
     * Takes two operands     * Takes two operands
-    * Returns ''true'' if //both// operands are ''true'', ''false'' otherwise.+    * Returns ''true'' if //both// operands are ''true'', returns ''false'' otherwise.
   * ''||'' (logical OR)   * ''||'' (logical OR)
     * Takes two operands     * Takes two operands
-    * Returns ''true'' if //either// operand is ''true'', ''false'' otherwise.+    * Returns ''true'' if //either// operand is ''true'', returns ''false'' otherwise.
   * ''!'' (logical NOT, logical negation)   * ''!'' (logical NOT, logical negation)
     * Takes one operand     * Takes one operand
Line 217: Line 222:
 |''%%(x <= z) || (y != z)%%'' |//true// | |''%%(x <= z) || (y != z)%%'' |//true// |
 |''%%!(x >= z)%%''|//false//| |''%%!(x >= z)%%''|//false//|
 +
 +  * [[selection_examples#validating_input|complete example]]
  
 ===== Short-circuiting ===== ===== Short-circuiting =====
Line 241: Line 248:
  
 ===== Comparing characters and strings ===== ===== Comparing characters and strings =====
-  * See Gaddis 4.12.+  * [[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 ===== ===== The conditional operator =====
Line 251: Line 263:
     * Examples:<code c++>y = x<0 ? -1.0*x : x;     * Examples:<code c++>y = x<0 ? -1.0*x : x;
 w = x<0 ? y=10 : z=20;</code> w = x<0 ? y=10 : z=20;</code>
- 
-===== switch structure ===== 
-  * See Gaddis 4.14. 
  
 ===== More about blocks and scope ===== ===== More about blocks and scope =====
-  * See Gaddis 4.15.+  * See Gaddis 4.15 (programs Pr4-28.cpp to Pr4-30.cpp).
  
  
cplusplus/control_structures_selection.1359871457.txt.gz · Last modified: 2013/02/03 06:04 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki