Classroom Notes: Unit 1.2 Selection and Logic in flowcharts
1. What are the arithmetic operators used in programming?
Arithmetic operators commonly used in programming:
- Addition (+) → Adds two values.
Example: 5 + 3 = 8 - Subtraction (-) → Subtracts one value from another.
Example: 10 - 4 = 6 - Multiplication (*) → Multiplies two values.
Example: 7 * 2 = 14 - Division (/) → Divides one value by another.
Example: 20 / 5 = 4 - Modulus (%) → Returns the remainder after division.
Example: 17 % 5 = 2 (since 17 divided by 5 is 3 with remainder 2) - Increment (++) → Increases a value by 1 (used in languages like C, Java, C++).
Example: x++ if x = 5 → x = 6 - Decrement (--) → Decreases a value by 1.
Example: y-- if y = 8 → y = 7
2. What are the comparison operators used in programming?
Comparison (relational) operators commonly used in programming:
- Equal to (==) → Checks if two values are equal.
Example: 5 == 5 → true - Not equal to (!=) → Checks if two values are not equal.
Example: 7 != 3 → true - Greater than (>) → Checks if the left value is larger than the right.
Example: 10 > 6 → true - Less than (<) → Checks if the left value is smaller than the right.
Example: 4 < 9 → true - Greater than or equal to (>=) → Checks if the left value is larger than or equal to the right.
Example: 4 >= 3 → true - Less than or equal to (<=) → Checks if the right value is larger than or equal to the left.
Example: 3 <= 3 → true
3. What are the Boolean operators used in programming?
Boolean (logical) operators used in programming:
- AND (&& or and) → Returns true if both conditions are true.
Example: (5 > 2) && (10 > 3) → true - OR (|| or or) → Returns true if at least one condition is true.
Example: (4 > 7) || (8 > 2) → true - NOT (! or not) → Reverses the Boolean value.
Example: !(5 > 2) → false
4. What is a selection statement in programming?
A selection statement is a programming construct that lets the program choose which block of code to execute based on a condition.
Some of the common types of selection statements are:
Some of the common types of selection statements are:
- if → Executes code if condition is true.
- if-else → Executes one block if true, another if false.
- if-else if ladder → Checks multiple conditions in sequence.
- switch → Selects one option from many based on a variable's value.
5. Draw a flowchart for the following scenarios:
- Check if a number is positive
- Check if a student passed (Passing marks is 40 out of 100)
- Check if a number is even or odd
- Compare two numbers and say which one is greater