If STATEMENTS
Numeric Type Declarations and Variables
Declarations for Numeric Types: Just as with the types char and string, we can declare named constants and variables of type int and float. Such declarations use the same syntax as before, except that the literals and the names of the data types are different.
Variable Declarations: We declare numeric variables the same way in which we declare char and string variables, except that we use the names of numeric types. The following are valid variable declarations:
- (Note: The transcript states that numeric variable declarations follow the same syntax as for char and string variables; specific examples are not provided in the excerpt.)
Variable (definition):
- A variable is a location in memory, referenced by an identifier, that contains a data value that can be changed.
- All single characters (enclosed in single quotes) and strings (enclosed in double quotes) are constants.
MATTERS OF STYLE: Capitalization of Identifiers
- Programmers often use capitalization as a quick visual clue to what an identifier represents.
- Different programmers adopt different conventions for using uppercase and lowercase letters.
- Some people use only lowercase, separating the English words in an identifier with underscores. Examples from the transcript include:
- pay_rate
- Emp_num
- pay_file
C++ Conditions and If Statements
- Relational Operators (examples):
- Less than: a < b
- Less than or equal to: a \le b
- Greater than: a > b
- Greater than or equal to: a \ge b
- Equal to: a == b
- Not Equal to: a \ne b
Syntax of if condition
- Syntax:
if (condition) {
// block of code to be executed if the condition is true
}
- Notes:
- Use the if statement to specify a block of C++ code to be executed if a condition is true.
- The keyword if must be in lowercase; uppercase variants like If or IF will generate an error.
- Example structure with an else branch:
if (x < 0) {
// x < 0
/* lots of code */
} else {
// x >= 0
/* lots of code */
}
- Nested and multiple tests:
if (/* some test */) {
// test is true
if (/* some other test */) {
// second test is true
} else {
// second test is false
}
} else {
// the test is false
}
Sample Program: if condition
#include <iostream>
using namespace std;
int main() {
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}
return 0;
}
- What the sample program demonstrates:
- Declaring two integer variables, x and y.
- A simple conditional check: if x > y, print a message.
- In this example, since 20 > 18, the program prints: x is greater than y.
Practice Program: if condition statement
- Code fragment given in the transcript (interpreted with standard C++ syntax):
int found = 0, count = 5;
if (!found || --count == 0) {
cout << "danger" << endl;
}
cout << "count = " << count << endl;
What is being tested:
- found is 0, so !found evaluates to true.
- The operator used is logical OR: ||. The transcript shows it as "| |" due to formatting; this should be interpreted as ||.
- Because the left operand /!found/ is true, the right operand in the OR expression is not evaluated (short-circuit evaluation).
- Therefore, the expression inside the if becomes true and prints "danger".
- The decrement of count on the right side of the OR expression is not performed due to short-circuiting, so count remains 5.
Output analysis:
- Expected output:
- First line: danger
- Second line: count = 5
Note on syntax: The original transcript shows "||" with spacing as "| |"; in C++ the correct operator is written as ||. The fragment in practice uses pre/post-decrement: --count decrements before use in the expression.
Connections and Practical Implications
- Consistency with earlier declarations: Numeric types (int, float) use the same declaration style as char and string, reinforcing that C++ treats different types with uniform syntax for declarations, while literals differ by type.
- Naming conventions: Identifiers’ capitalization can convey meaning (constants vs variables, types, file names, etc.). Underscores are a common convention to separate words in identifiers.
- Conditions and control flow: Understanding relational operators and the syntax of if statements is foundational for program logic and branching behavior.
- Short-circuit evaluation: Logical operators (||, &&) in conditional expressions can affect both the flow and side effects (e.g., whether parts of an expression are evaluated, and thus whether expressions like --count are executed).
- Real-world relevance: These concepts underpin basic decision-making in programs, such as validating input, enforcing business rules, and driving branching logic in software workflows.
Formulas and Equations
- Relational comparisons in code examples are represented as logical statements:
- a < b
- a \le b
- a > b
- a \ge b
- a = = b (note: in code this is a\,==\,b)
- a \ne b
Numerical and Statistical References
- Numeric literals and type names are used in declarations (e.g., int, float).
- Sample values in the transcript include integers for variables (e.g., x = 20, y = 18; count = 5).
- The practice example demonstrates how a pre/post-decrement interacts with logical operators to influence both condition evaluation and variable state, yielding the observed output.