You are on page 1of 8

SILLIMAN UNIVERSITY JUNIOR HIGH SCHOOL

STUDENT COUNCIL
S.Y. 2023 - 2024

Project REVIEW: ICT 10 Notes


Spencer R. Renacia

Lesson: Arithmetic Operator Precedence and Associativity

Arithmetic Operator Precedence and Associativity

Key Concepts:

● The order in which operators are evaluated in a compound expression is called OPERATOR PRECEDENCE.

● In C++, all operators are assigned a level of precedence.

● Precedence level 1 has the highest precedence level, and level 3 has the lowest. Operators with a higher precedence level
(1) get evaluated first.

The PIDMMDAS Rule

1 - Parentheses | “ () “
Left-to-right
1 - Increment | “ ++ ”
Right-to-left
1 - Decrement | “ -- ”
Right-to-left
2 - Multiplication | “ * “
Left-to-right
2 - Modulus | “ % “
Left-to-right
2 - Division | “ / “
Left-to-right
3 - Addition | “ + “

Left-to-right
3 - Subtraction | “ - “
Left-to-right
SILLIMAN UNIVERSITY JUNIOR HIGH SCHOOL
STUDENT COUNCIL
S.Y. 2023 - 2024

Project REVIEW: ICT 10 Notes


Spencer R. Renacia

Getting STRING Inputs


There are two ways to get string input from the user: the cin or getline command.

● cin command

cin >> string_variable; // for getting string input

Note: cin considers a space (whitespace, tabs, etc) as terminating characters. Meaning, it can display only a single word even
if you type many.

● When a blank space is detected, the extraction operator stops reading input.
● The command “cin” allows us to enter only one word.

● getline command

getline (cin, variable_name); // for getting string input

Note: When working with strings, we use the getline() function to read a line of text.

● It takes cin as the first parameter and the string variable as the second.

Lesson: Conditional Statements and Logical Operators

CONDITIONAL STATEMENTS

Key Concepts:

● A CONDITIONAL STATEMENT is a programming language statement that selects an execution path based on a given
condition and evaluates it to whether true or false.
● RELATIONAL OPERATOR performs a comparison between two values and produces or returns a value 1 (TRUE) or 0 (FALSE)
result.
● LOGICAL OPERATORS provide us with this capability to test multiple conditions.
SILLIMAN UNIVERSITY JUNIOR HIGH SCHOOL
STUDENT COUNCIL
S.Y. 2023 - 2024

Project REVIEW: ICT 10 Notes


Spencer R. Renacia

The if-statement
The if statement checks whether the test condition is true or not. If the test condition is true, it executes the code/s inside the body
of the if statement. But if the test condition is false, it skips the code/s inside the body of the if statement.

● Syntax / General Form:

if (condition)
statement ;

next statement;

● Syntax / General Form for Compound if statement:

if (condition)
{
statement1;
Statement2;
statement n;

next statement;

Note: CONDITION is the comparison of two values using relational operators.

RELATIONAL OPERATORS
Relational operators perform a comparison between two values and produce or return a value 1 (TRUE) or 0 (FALSE) result.

OPERATORS MEANING

< Is less than

<= Is less than or equal to

> Is greater than

>= Is greater than or equal to

== Is equal to

!= Is not equal to
SILLIMAN UNIVERSITY JUNIOR HIGH SCHOOL
STUDENT COUNCIL
S.Y. 2023 - 2024

Project REVIEW: ICT 10 Notes


Spencer R. Renacia

The if-else statement


The if-else executes the body of if when the test expression/condition is true and executes the body of else if the test
expression/condition is false.

● Syntax / General Form:

if (condition)
statement(T);

else
statement(F);

next statement;

● Syntax / General Form for Compound if statement:

if (condition)
{
statement(T1);
statement(T2);
statement(T3);
}

else
{
statement(F1);
statement(F2);
statement(F3);
}

next statement;

Note: if(condition) and else should not have any semicolon.

Nested if-else statement


The nested if-else statement has more than one test expression. If the first test expression is true, it executes the code inside the
braces{ } just below it. But if the first test expression is false, it checks the second test expression. If the second test expression is true,
it executes the code inside the braces{ } just below it. This process continues. If all the test expressions are false, code/s inside else
is/are executed and the control of the program jumps below the nested if...else.
SILLIMAN UNIVERSITY JUNIOR HIGH SCHOOL
STUDENT COUNCIL
S.Y. 2023 - 2024

Project REVIEW: ICT 10 Notes


Spencer R. Renacia

● Syntax of Nested if-else

if(test condition1)
{
statement/s to be executed if test expression1 is true;
}

else if(test condition2)


{
statement/s to be executed if test expression1 is false and 2 is true;
}

else if(test condition3)


{
statement/s to be executed if text expression1 and 2 are false and 3 is true;
}

else
{
statements to be executed if all test expressions are false;
}

next statement;

LOGICAL OPERATORS
Logical operators provide us with this capability to test multiple conditions.

OPERATOR SYMBOL FORM OPERATION

Logical NOT ! !x true if x is false, or false if x is true

Logical AND && x && y true if both x and y are true, false otherwise

Logical OR || x || y true if either x or y are true, false otherwise

● LOGICAL AND ( & & ) OPERATOR

The logical AND operator is used to test whether both conditions are true. If both conditions are true, logical AND returns
true. Otherwise, it returns false.
SILLIMAN UNIVERSITY JUNIOR HIGH SCHOOL
STUDENT COUNCIL
S.Y. 2023 - 2024

Project REVIEW: ICT 10 Notes


Spencer R. Renacia

● LOGICAL OR ( | | ) OPERATOR

The logical OR operator is used to test whether either of the two conditions is true. If the left operand evaluates to true, or
the right operand evaluates to true, the logical OR operator returns true. If both operands are true, then logical OR will
return true as well. If both operands are false, then logical OR will return false.

● LOGICAL NOT ( ! ) OPERATOR

The “NOT” logical operator is used to convert a value from true to false, or from false to true. Similarly, if an operand
evaluates to true, a logical “not” would cause it to evaluate to false. If an operand evaluates to false, its logical “not”
equivalent would be true.

Lesson: Switch Case Statements

SWITCH STATEMENTS

Key Concepts:

● A switch statement is a flow control statement that is used to execute different blocks of statements based on
the value of the given expression.
● The break keyword is used to prevent running code into the next case.
● The default statement states what should be done if the variable does not match any case.

What is a Switch Statement?


This is a flow control statement that is used to execute different blocks of statements based on the value of the given
expression.

Note: You can create any number of cases in the switch statement, however, the case value can only be of type
int or char.

This statement helps in testing the equality of a variable against a set of other values. And, each value compared is known
as a case.

Note: Compared to the if-else-if ladder, switch statements are faster.


SILLIMAN UNIVERSITY JUNIOR HIGH SCHOOL
STUDENT COUNCIL
S.Y. 2023 - 2024

Project REVIEW: ICT 10 Notes


Spencer R. Renacia

Break Keyword
This is used inside the switch statement to prevent code from running into the next case. It terminates the statement
sequence.

Note: The usage of the break keyword is OPTIONAL.

When the compiler encounters a break keyword, the execution of switch statements terminates and control jumps to
the line that comes after the switch statement.

● Syntax of Switch Statements

switch (expression/variable)
{

case value_1:
// statement_1;
break;

case value_1:
// statement_1;
break;

case value_1:
// statement_1;
break;

default:
// default_statements;
break;

Definition:
● Variable is used for a comparison to be made.
● Case this is where the comparison of a variable to another value is done.
● Break this prevents execution to the next case statement and is optional.
● Default this states what should be done when the variable does not match with any case test value and is
optional.
SILLIMAN UNIVERSITY JUNIOR HIGH SCHOOL
STUDENT COUNCIL
S.Y. 2023 - 2024

Project REVIEW: ICT 10 Notes


Spencer R. Renacia

Rules of Switch Case Statements


1. The case value must be either int or char type by declaration.
2. There can be any number of cases.
3. There must be NO DUPLICATE CASES.
4. Each statement may have a break keyword optionally, though, it is recommended.
5. The default statement is optional, though, it is recommended.

Note: When comparing characters, enclose them in single quotes (‘). Example: case ‘A’.

Note: Numbers may be enclosed in single quotes, though, it is possible to not do so.

You might also like