cplusplus:control_structures_selection
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cplusplus:control_structures_selection [2013/02/20 00:03] – mithat | cplusplus:control_structures_selection [2017/01/31 23:57] (current) – [C++ logical operators] mithat | ||
---|---|---|---|
Line 2: | Line 2: | ||
~~NOTOC~~ | ~~NOTOC~~ | ||
- | ====== Control Structures: | + | ====== Control Structures: |
- | Mithat Konar\\ | + | Mithat Konar\\ |
- | 2013-02-03 | + | |
===== Introduction ===== | ===== Introduction ===== | ||
- | * Input validation | + | * **Control structures** are used to affect how statements are executed. |
- | | + | * Sometimes also called |
- | * Menus | + | |
- | | + | |
- | | + | |
- | ===== Input validation | + | ===== Control structures in C++ ===== |
- | * //Get input value from user\\ While input value is not valid\\ Get input value from user// | + | |
- | ===== Sentinel/ | + | * Three kinds of control |
+ | * **Sequence** | ||
+ | * Statements are executed one after the other. | ||
+ | * This is C++'s default behavior! | ||
+ | * **Selection structures** | ||
+ | * Used to choose among alternative courses of action. | ||
+ | * " | ||
+ | * **Repetition structures** | ||
+ | * Used to repeat a set of instructions. | ||
+ | * " | ||
- | | + | ===== Single-entry/ |
- | * **Flag**: sometimes | + | |
- | * Sentinel controlled repetition: | + | |
- | * Repeat as long as a sentinel condition | + | |
- | * //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// | ||
+ | * // | ||
+ | * //nested if/else// | ||
+ | * // | ||
+ | * Also has a ternary // | ||
+ | |||
+ | ===== if selection structure ===== | ||
+ | |||
+ | * Used to do something if some condition is " | ||
+ | * Pseudocode example:< | ||
+ | print " | ||
+ | * The '' | ||
+ | |||
+ | ===== if syntax ===== | ||
+ | |||
+ | * C++ syntax:< | ||
+ | < | ||
+ | * Example:< | ||
+ | cout << " | ||
+ | * [[selection_examples# | ||
+ | |||
+ | ===== Relational operators ===== | ||
+ | |||
+ | * **Relational operators** are used to compare values and return a '' | ||
+ | * A value that is either '' | ||
+ | * '' | ||
+ | |||
+ | ===== C++ relational operators ===== | ||
+ | * C++ supports the following relational operators: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * Note: Do not confuse the '' | ||
+ | |||
+ | ===== Order of precedence ===== | ||
+ | |||
+ | ^operator ^associativity ^type ^ | ||
+ | | %%()%% | left to right | parenthesis | | ||
+ | | %%* / %% % | left to right | multiplicative | | ||
+ | | %%+ -%% | left to right | additive | | ||
+ | | %%<< >>%% | left to right | stream insertion/ | ||
+ | | %%< <= > >=%% | left to right | relational | | ||
+ | | %%== !=%% | right to left | equality | | ||
+ | | %%=%% | right to left | assignment | | ||
+ | |||
+ | ===== if/else selection structure ===== | ||
+ | |||
+ | * Used to do one thing if some condition | ||
+ | * Pseudocode example:< | ||
+ | print “Passed” | ||
+ | else | ||
+ | print " | ||
+ | * The '' | ||
+ | |||
+ | ===== if/else syntax ===== | ||
+ | |||
+ | * C++ syntax:< | ||
+ | < | ||
+ | else | ||
+ | < | ||
+ | * Example:< | ||
+ | cout << | ||
+ | else | ||
+ | cout << " | ||
+ | * [[selection_examples# | ||
+ | |||
+ | ===== Nested if/else selection structure ===== | ||
+ | |||
+ | | ||
+ | |||
+ | ===== Nested if/else pseudocode ===== | ||
+ | | ||
+ | print " | ||
+ | else | ||
+ | if student’s grade is greater than or equal to 80 | ||
+ | print " | ||
+ | else | ||
+ | if student’s grade is greater than or equal to 70 | ||
+ | print " | ||
+ | else | ||
+ | if student’s grade is greater than or equal to 60 | ||
+ | print " | ||
+ | | ||
+ | print " | ||
+ | |||
+ | ===== Nested if/else pseudocode ===== | ||
+ | | ||
+ | print " | ||
+ | else if student’s grade is greater than or equal to 80 | ||
+ | print " | ||
+ | else if student’s grade is greater than or equal to 70 | ||
+ | print " | ||
+ | else if student’s grade is greater than or equal to 60 | ||
+ | print " | ||
+ | else | ||
+ | print " | ||
+ | |||
+ | ===== Nested if/else syntax ===== | ||
+ | | ||
+ | cout << " | ||
+ | else if (grade >= 80) | ||
+ | cout << " | ||
+ | else if (grade >= 70) | ||
+ | cout << " | ||
+ | else if (grade >= 60) | ||
+ | cout << " | ||
+ | else | ||
+ | cout << " | ||
+ | * [[selection_examples# | ||
+ | |||
+ | ===== Compound statements ===== | ||
+ | |||
+ | * A **simple statement** in C++ is any statement that ends with a semicolon: <code c++>cout << "This is a simple statement." | ||
+ | * A **compound statement** is a set of simple statements placed between curly brackets: <code c++>{ | ||
+ | x = 3 * y; | ||
+ | cout << "The magic number | ||
+ | }</ | ||
+ | |||
+ | ===== Compound statement example ===== | ||
+ | * A compound statement can be used anywhere | ||
+ | cout << " | ||
+ | else | ||
+ | { | ||
+ | cout << " | ||
+ | cout << "You must take this course again." | ||
+ | }</ | ||
+ | * Without the brackets, the statement< | ||
+ | |||
+ | ===== Blocks ===== | ||
+ | |||
+ | * A compound statement with variable declarations is called a **block**. | ||
+ | * C++ example:<code c++>if (grade >= 60) | ||
+ | cout << " | ||
+ | else | ||
+ | { | ||
+ | char yourGrade; | ||
+ | yourGrade = ' | ||
+ | cout << "You received a grade of " << yourGrade << endl; | ||
+ | cout << "You must take this course again." | ||
+ | }</ | ||
+ | |||
+ | ===== 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 ===== | ||
+ | |||
+ | * ''&&'' | ||
+ | * Takes two operands | ||
+ | * Returns '' | ||
+ | * '' | ||
+ | * Takes two operands | ||
+ | * Returns '' | ||
+ | * '' | ||
+ | * Takes one operand | ||
+ | * Returns '' | ||
+ | * Precedence: '' | ||
+ | |||
+ | ===== Truth table for logical AND ===== | ||
+ | ^expression ^//result// ^ | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | |||
+ | ===== Truth table for logical OR ===== | ||
+ | ^expression ^//result// ^ | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | |||
+ | ===== Truth table for logical NOT ===== | ||
+ | ^expression ^//result// ^ | ||
+ | | '' | ||
+ | | '' | ||
+ | |||
+ | ===== Examples ===== | ||
+ | assume: '' | ||
+ | |||
+ | ^expression ^//result// ^ | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |||
+ | * [[selection_examples# | ||
+ | |||
+ | ===== Short-circuiting ===== | ||
+ | * **short-circuit evaluation**: | ||
+ | if ( (x < 0) && (-6 > y-1) ) // (-6 > y-1) is not evaluated | ||
+ | ... | ||
+ | if ( (x > 0) || (-6 > y-1) ) | ||
+ | ...</ | ||
+ | * Keep this in mind for later | ||
+ | |||
+ | ===== More about Boolean values ===== | ||
+ | |||
+ | * In C++: | ||
+ | * The value 0 (the integer zero) is considered | ||
+ | * anything else is considered '' | ||
+ | * Thus, the following example will print // | ||
+ | cout << " | ||
+ | else | ||
+ | cout << " | ||
+ | * and the following will print // | ||
+ | cout << " | ||
+ | else | ||
+ | cout << " | ||
+ | |||
+ | ===== Comparing characters and strings ===== | ||
+ | * [[selection_examples# | ||
+ | * See Gaddis 4.12 (programs Pr4-20.cpp and Pr4-21.cpp). | ||
+ | |||
+ | ===== switch structure ===== | ||
+ | * [[selection_examples# | ||
+ | * See Gaddis 4.14 (programs Pr4-23.cpp to Pr4-27.cpp). | ||
+ | |||
+ | ===== The conditional operator ===== | ||
+ | * C++ has a //ternary// conditional operator | ||
+ | * Similar to an if/else control structure. | ||
+ | * Unlike if/else, the conditional operator //returns a value//. | ||
+ | * Syntax:'' | ||
+ | * Semantics: "If '' | ||
+ | * Examples:< | ||
+ | w = x<0 ? y=10 : z=20;</ | ||
+ | |||
+ | ===== 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