Uploaded by markjustinearopo08

CCS102-Selection-Structures

advertisement
CCS102
SELECTION STRUCTURE
THE ELSE IF STATEMENT
In programming, selection structures allow you to
make decisions and execute different code blocks
based on certain conditions. These structures enable
your program to choose between alternative paths of
execution, making it possible to create dynamic and
responsive programs. The most common selection
structure is the conditional statement, which evaluates
a condition and executes different code blocks based
on the result. This guide provides an overview of
selection structures and explains how to make
decisions using conditional statements in Java.
The else if statement allows you to evaluate multiple
conditions in a sequential manner. It is used when you
have more than two possible outcomes. The syntax of
the else if statement is as follows:
CONDITIONAL STATEMENTS
THE SWITCH STATEMENT
Conditional
statements,
also
known
as
decision-making statements, evaluate a condition and
execute different sets of statements based on the
result. The condition can be a comparison, logical
expression, or any expression that evaluates to either
true or false. There are two main types of conditional
statements in Java: if statement and switch
statement.
The switch statement allows you to select one of many
code blocks to be executed based on the value of a
variable or an expression. It provides an alternative to
multiple if-else if statements when there are multiple
possible values to compare. The syntax of the switch
statement is as follows:
THE IF STATEMENT
The if statement allows you to execute a block of
code only if a specified condition is true. The syntax of
the if statement is as follows:
Conclusion
THE IF-ELSE STATEMENT
The if-else statement extends the if statement by
providing an alternative code block to be executed
when the condition is false. The syntax of the if-else
statement is as follows:
Selection structures, such as conditional statements,
provide essential decision-making capabilities in Java
programming. By using conditional statements, you
can control the flow of your program and execute
different code blocks based on specific conditions.
This allows you to create more flexible and dynamic
applications. Understanding how to make decisions
using conditional statements is a fundamental skill for
every Java programmer.
1 | Aropocakes
CCS102
TOPIC 1
2 | Aropocakes
CCS102
RELATIONAL OPERATORS
Relational operators are used in programming to
compare values and determine the relationship
between them. In Java, there are several comparison
operators that allow you to perform comparisons and
make decisions based on the results. These operators
are commonly used in conditional statements to
control the flow of the program. This guide provides an
overview of the comparison operators in Java and
demonstrates their usage in conditional statements
for decision making.
COMPARISON OPERATORS
The following are the commonly used comparison
operators in Java:
In this example, we have two variables a and b with
values 10 and 20, respectively. The == operator is used
to compare the values of a and b. The result of the
comparison is then stored in the equal variable.
The System.out.println statement will output the result
of the comparison, indicating whether a and b are
equal (true or false).
== (equal to): Checks if two values are equal.
!= (not equal to): Checks if two values are not equal.
> (greater than): Checks if the left operand is greater
than the right operand.
< (less than): Checks if the left operand is less than the
right operand.
The output shows that a and b are not equal, as the
value of equal is false.
>= (greater than or equal to): Checks if the left
operand is greater than or equal to the right operand.
<= (less than or equal to): Checks if the left operand
is less than or equal to the right operand.
USAGE IN CONDITIONAL STATEMENTS
Comparison operators are commonly used in
conditional statements to make decisions based on
the result of a comparison. Conditional statements,
such as if, if-else, and switch, evaluate a condition and
execute different code blocks based on the result.
Here are some examples of how comparison operators
can be used in conditional statements:
In this example, we have a variable number with a
value of 10. The != operator is used to check if the
value of a number is not equal to zero. The result of
the comparison is then stored in the notZero variable.
The System.out.println statement will output the result
of the comparison, indicating whether the number is
not zero (true) or zero (false).
3 | Aropocakes
CCS102
The output shows that the number is not zero, as the
value of notZero is true.
In this example, we have two variables x and y with
values 5 and 10, respectively. The > operator is used to
compare the values of x and y. The result of the
comparison is then stored in the greater variable.
The System.out.println statement will output the result
of the comparison, indicating whether x is greater
than y (true or false).
The output shows that x is not greater than y, as the
value of greater is false.
Conclusion
Comparison operators play a crucial role in making
decisions and controlling the flow of a program. By
using these operators in conditional statements, you
can evaluate conditions and execute specific code
blocks
based
on
the
comparison
results.
Understanding the usage of comparison operators is
essential for writing effective and logical programs in
Java.
4 | Aropocakes
CCS102
TOPIC 2
5 | Aropocakes
CCS102
BOOLEAN OPERATORS
Boolean operators, also known as logical operators,
are used in programming to combine and manipulate
boolean values (true or false). In Java, there are three
main boolean operators: AND (&&), OR (||), and NOT (!).
These operators allow you to build complex conditions
by combining multiple boolean expressions. This guide
provides an overview of boolean operators in Java,
demonstrates their usage in combining conditions,
explains short-circuit evaluation, and discusses the
importance of logical expressions in programming.
LOGICAL OPERATORS
The following are the three logical operators in Java:
The code declares and initializes variables age with a
value of 25 and day with a value of 1. It then evaluates
the logical expression (age > 20 && day > 0) and
assigns the result (true) to the variable answer.
The code sets the variable score to 80 and evaluates
the logical expression (score >= 90 || score <= 50),
resulting in false being assigned to the variable
answer since the score is not greater than or equal to
90 and it is not less than or equal to 50.
AND (&&): The AND operator returns true if both of its
operands are true, and false otherwise.
OR (||): The OR operator returns true if at least one of
its operands is true, and false otherwise.
NOT (!): The NOT operator reverses the logical state of
its operand. If the operand is true, the NOT operator
returns false, and if the operand is false, it returns
true.
USAGE OF LOGICAL OPERATORS
Logical operators are often used to combine multiple
conditions and create complex boolean expressions.
They are typically used within conditional statements
such as if, while, and for to determine the flow of the
program based on the evaluation of these conditions.
Here are some examples of using logical operators:
The code initializes variables x and y with values 5 and
10, respectively, and assigns the logical negation of
the comparison x > y to the variable answer, resulting
in true being stored in answer.
SHORT-CIRCUIT EVALUATION
One important feature of logical operators in Java is
short-circuit evaluation. Short-circuit evaluation
means that the second operand of an && or || operator
is not evaluated if the outcome can be determined
solely by evaluating the first operand. This behavior
can be useful in improving performance and
preventing unnecessary evaluations. Here's an
example:
6 | Aropocakes
CCS102
The code initializes variables x and y with values 5 and
10, respectively. It then evaluates the logical
expression (x > 0 && y / x > 2) and assigns the result
(false) to the
variable answer. The expression checks if x is greater
than 0 and if the result of dividing y by x is greater
than 2. Since x is 5 and y divided by x is 2 (which is not
greater than 2), the overall result of the expression is
false. Therefore, the variable answer is assigned the
value false.
Conclusion
Boolean operators play a crucial role in combining
conditions and creating logical expressions in Java
programming. By using &&, ||, and !, you can build
complex conditions that determine the flow of your
program. Additionally, understanding short-circuit
evaluation can help you optimize your code and
improve performance in certain scenarios.
Remember to use parentheses ( ) to clarify the order
of evaluation when using multiple logical operators in
the same expression.
7 | Aropocakes
CCS102
TOPIC 3
8 | Aropocakes
CCS102
IF STATEMENT
The if statement is a fundamental control structure in
programming that allows you to control the flow of
your Java program based on specified conditions. It
provides a way to execute certain blocks of code only
when a given condition is true. This guide will explain
the syntax and usage of the if statement in Java,
provide code snippets to demonstrate its placement
and use, discuss controlling program flow based on
conditions, and highlight the importance of
conditional statements in programming.
The condition is an expression that evaluates to either
true or false. If the condition is true, the code block
enclosed within the curly braces {} following the if
statement will be executed. If the condition is false,
the code block will be skipped, and the program will
continue to the next statement after the if block.
In this example, the condition age >= 18 is evaluated.
If the value of age is greater than or equal to 18, the
message "You are an adult" will be printed to the
console.
NESTED IF STATEMENTS
You can also nest if statements within other if
statements to create more complex decision-making
structures. This allows you to test multiple conditions
and execute different blocks of code based on those
conditions. Here's an example:
SYNTAX AND USAGE
The if statement in Java has the following syntax:
In this example, we use an additional if statement to
check the condition gender == 'M' inside the outer if
block. If the condition is true, it prints the message
"You are a male adult." Otherwise, it prints the
message "You are a female adult."
9 | Aropocakes
CCS102
Conclusion
The if statement is a powerful tool for controlling
program flow based on conditions in Java. By using if,
else, and nested if statements, you can create
dynamic and flexible programs that adapt to different
situations. Conditional statements are essential in
decision-making processes, allowing your code to
execute different actions based on specific conditions
being met or not.
TOPIC 4
10 | Aropocakes
CCS102
11 | Aropocakes
CCS102
12 | Aropocakes
CCS102
IF…ELSE STATEMENT
The if...else statement is a powerful control structure in
Java programming that allows you to execute
different code blocks based on specified conditions. It
provides a way to control the flow of your program by
providing alternative paths of execution. This guide
will explain the syntax and usage of the if...else
statement in Java, provide code snippets to
demonstrate its placement and use, discuss executing
different code blocks based on conditions, and
highlight the importance of conditional statements in
programming
In this example, the condition num % 2 == 0 is
evaluated. If the value of num is divisible by 2 and has
a remainder of 0, the message "The number is even"
will be printed to the console. Otherwise, the message
"The number is odd" will be printed.
MULTIPLE CONDITIONS: ELSE IF
The if...else statement can be extended to include
multiple conditions using the else if clause. This allows
you to test additional conditions and execute different
code blocks accordingly. The syntax for else if is as
follows:
SYNTAX AND USAGE
The if...else statement in Java has the following syntax:
Here's an example that includes an else if clause:
The condition is an expression that evaluates to either
true or false. If the condition is true, the code block
enclosed within the first set of curly braces {} after the
if statement will be executed. If the condition is false,
the code block enclosed within the second set of curly
braces {} after the else statement will be executed.
13 | Aropocakes
CCS102
In this example, the program checks the value of num.
If num is greater than 0, the message "The number is
positive" will be printed. If num is less than 0, the
message "The number is negative" will be printed.
Otherwise, if both conditions are false, the message
"The number is zero" will be printed.
execution of your code and make decisions based on
specific conditions. By combining the if...else
statement with other control structures and operators,
you can create complex and flexible programs.
TOPIC 5
NESTED IF…ELSE STATEMENTS
You can also nest if...else statements within other
if...else statements to create more complex
decision-making structures. This allows you to test
multiple conditions and execute different code blocks
based on those conditions. Here's an example:
In this example, the inner if...else statement is nested
within the outer if statement. If num is greater than 0,
the program checks whether it is divisible by 2 to
determine if it is even or odd. If num is less than or
equal to 0, the message "The number is non-positive"
will be printed.
Conclusion
The if...else statement is a fundamental building block
in Java programming that allows you to make
decisions and control the flow of your program based
on conditions. By using the if...else statement, you can
execute different code blocks depending on whether a
condition is true or false. This enables you to create
dynamic and responsive programs that can adapt to
different scenarios.
Understanding the syntax and usage of the if...else
statement is essential for developing robust and
logical programs. It gives you the power to control the
14 | Aropocakes
CCS102
15 | Aropocakes
CCS102
16 | Aropocakes
CCS102
17 | Aropocakes
CCS102
IF…ELSE IF…ELSE STATEMENT
The if...else if...else statement in Java is a powerful
control structure that allows you to test multiple
conditions and execute different code blocks based on
those conditions. It provides a way to create complex
decision-making structures by combining multiple else
if clauses. This guide will explain the syntax and usage
of the if...else if...else statement in Java, provide code
snippets to demonstrate its placement and use,
discuss using multiple else if clauses for complex
conditions, illustrate nested decision-making, and
highlight the importance of these constructs in
programming.
Each condition is an expression that evaluates to
either true or false. The if clause checks the first
condition, and if it is true, the corresponding code
block is executed. If the first condition is false, the
program moves to the next else if clause and
evaluates its condition. This process continues until a
condition is found to be true, in which case the
corresponding code block is executed. If none of the
conditions are true, the code block within the else
clause is executed.
In this example, the program checks the value of num
and executes the corresponding code block based on
the condition. If num is greater than 0, the message
"The number is positive" will be printed. If num is less
than 0, the message "The number is negative" will be
printed. If num is equal to 0, the message "The
number is zero" will be printed. If none of these
conditions are met, the message "Invalid number" will
be printed.
NESTED DECISION:
USING NESTED IF…ELSE IF…ELSE
SYNTAX AND USAGE
The if...else if...else statement can also be nested within
other if or else clauses to create nested
decision-making structures. This allows you to test
multiple conditions and execute different code blocks
based on those conditions within each level of nesting.
Here's an example:
18 | Aropocakes
CCS102
In this example, the outer if statement checks if num is
greater than 0. If it is, the program enters the nested
if...else statement to check whether num is even or
odd. If num is divisible by 2, it is even; otherwise, it is
odd. If num is less than 0, the message "The number is
negative" will be printed. If num is equal to 0, the
message "The number is zero" will be printed.
Conclusion
The
if...else
if...else
statement
and
nested
decision-making structures provide a flexible way to
handle complex conditions in Java programming. By
using multiple else if clauses, you can create decision
trees with multiple branches, allowing your program to
respond differently based on different conditions.
Nesting if...else if...else statements further enhances
your ability to make intricate decisions by testing
multiple conditions within each level of nesting. These
constructs are essential for writing robust and flexible
programs that adapt to various scenarios.
Understanding the syntax and usage of the if...else
if...else
statement
and
mastering
nested
decision-making
will
greatly
enhance
your
programming skills and enable you to create more
sophisticated and dynamic applications.
19 | Aropocakes
CCS102
TOPIC 6
20 | Aropocakes
CCS102
21 | Aropocakes
CCS102
22 | Aropocakes
CCS102
SWITCH STATEMENT
The switch statement in Java is a control structure
that allows you to execute different code blocks based
on the value of a variable or expression. It provides an
alternative to using multiple if...else if...else statements
when you have a large number of possible cases to
handle. This guide will explain the syntax and usage of
the switch statement in Java, provide code snippets to
demonstrate its placement and use, discuss handling
multiple cases and default behavior, and highlight the
importance of this construct in programming.
SYNTAX AND USAGE
The expression is evaluated, and its value is compared
to the constants specified in each case label. If the
value matches a case constant, the corresponding
code block is executed. The break statement is used to
exit the switch statement after executing the
corresponding code block. If the value doesn't match
any case constant, the code block within the default
label is executed (optional).
In this example, the day variable is evaluated, and the
corresponding code block is executed based on its
value. If day is 1, the message "Monday" will be
printed. If day is 2, the message "Tuesday" will be
printed, and so on. If day doesn't match any of the
case constants, the code block within the default label
is executed, and the message "Invalid day" will be
printed.
DEFAULT BEHAVIOR
The default label in the switch statement provides a
fallback option when none of the cases match the
value of the expression. It allows you to define a
default behavior that should be executed when no
other case matches. The default label is optional, and
its code block will be executed if no matching case is
found.
23 | Aropocakes
CCS102
Conclusion
The switch statement in Java provides a concise and
efficient way to handle multiple cases based on the
value of an expression. It offers an alternative to using
multiple if...else if...else statements, especially when
dealing with a large number of possible cases. The
ability to handle multiple cases and provide a default
behavior enhances the flexibility and readability of
your code.
Understanding the syntax and usage of the switch
statement is essential for writing clean and organized
code that effectively handles different scenarios. It
allows you to simplify complex decision-making
structures and improve the overall readability and
maintainability of your code.
24 | Aropocakes
CCS102
TOPIC 7
25 | Aropocakes
CCS102
26 | Aropocakes
CCS102
27 | Aropocakes
CCS102
NESTED DECISION
In programming, decision-making is a fundamental
concept that allows your code to respond differently
based on conditions. Nested decisions provide a way
to create more intricate and specific decision
structures. By combining multiple if...else statements
within each other, you can create a branching path of
execution. This guide will walk you through the
concept of nested decisions using the Java
programming language.
DEFINITION AND PURPOSE OF
NESTED DECISIONS
Nested decisions refer to the practice of placing one
or more if...else statements inside another if or else
block. This technique enables you to handle complex
decision scenarios where multiple conditions need to
be evaluated sequentially. Nested decisions provide
greater flexibility and granularity when designing your
program's logic.
SYNTAX AND USAGE
The general syntax of a nested if...else statement in
Java is as follows:
NESTED IF…ELSE IF…ELSE STATEMENTS
Building upon the previous example, let's now
categorize the positive numbers as "small" or "large":
NESTED IF…ELSE STATEMENTS
Let's consider a simple example where we determine if
a given number is positive, negative, or zero:
28 | Aropocakes
CCS102
Conclusion
Nested decisions offer a powerful tool for crafting
intricate decision structures in your Java programs. By
nesting if...else statements and combining logical
operators, you can create code that adapts to a
variety of scenarios. Understanding and effectively
using nested decisions will enhance your programming
skills and enable you to build more sophisticated and
dynamic applications.
29 | Aropocakes
CCS102
TOPIC 8
30 | Aropocakes
CCS102
31 | Aropocakes
CCS102
32 | Aropocakes
CCS102
33 | Aropocakes
Related documents
Download