UGCSA117 : Object-Oriented Programming(OOP)
in Java
(BCA 2nd Sem)
Unit-2
Module2.1:
2.1.1 Boolean Data Type
The Boolean data type is used to store logic values i.e. truth values which are true
or false. It takes only 1 byte of space to store logic values. Here, true means 1,
and false means 0. In the Boolean data type any value other than ‘0’ is considered
as ‘true’. Boolean values are most commonly used in data structures to decide the
flow of control of a program and decision statements. In programming languages,
we have various data types to store different data types. The most used data types
are integer, string, float, and Boolean. The Boolean data type is a type of data that
stores only two types of values i.e. True or False. These values are not case-
sensitive depending upon programming languages. The name Boolean comes
from the branch of mathematics called Boolean algebra, named after George Bool
the mathematician.
Example of Declaration of Boolean Data Type
import java.io.*;
class GFG { public static void main
(String[] args) { boolean a = true;
boolean b = false;
//boolean c = 1; this will give error
//Because in Java only true and false
//can be used in boolean
System.out.println("a: "+a);
System.out.println("b: "+b);
}
}
2.1.2Difference Between Boolean and Other Data Types
In programming languages, there are three types of data which are Booleans, Text,
and Numbers. It is important to understand the differences between them and some
basics about them.
Booleans: They are either true (1) or false (0) and take only 1 byte of space in
memory. while other data types take 2 to 8 bytes depending on the machine.
Numbers: Numbers can be negative, positive, and zero or decimal numbers. The data
type used to store numbers such as short, int, and double can take 2 to 8 bytes of
space in memory.
Text: Text includes characters, alphabets, numbers, and a collection of them. Text can
be of character or string type. The size of 1 character is 2 bytes.
2.1.3 If Else Statement in Programming
An if else statement in programming is a basic programming technique
that allows you to make decisions based on certain conditions. It allows
your program to execute different pieces of code depending on whether
the specified condition evaluates to true or false. This capability is
crucial in building dynamic and functional applications.
2.1.4 Importance of If Else Statement:
The importance of if-else statements lies in their ability to control the execution
of a program. Using if-else statements allows developers to apply logic that
responds to situations, making programs more versatile and powerful. Whether
manipulating user statements, manipulating data, or controlling program flow, if-
else statements play an important role in programming.
2.1.5 Basic Syntax of If Else Statement:
Generally, the basic syntax of if false statements follow this pattern: if
(condition) {
// Code block to execute if condition is true }
else {
// Code block to execute if condition is false }
In this syntax:
The `if` keyword begins a conditional statement.
The condition is enclosed in parentheses `()`.
If the condition evaluates to true, the code block immediately following the `if`
statement is executed.
If the condition evaluates to false, the code block is executed in the `else`
statement.
2.1.6 If Else Statement in Java:
Here is the implementation of if-else statement in Java language:
public class Main {
public static void main(String[] args) {
// Declare and initialize the variable num
int num = 10;
// Check if num is greater than 0
if (num > 0)
// If num is greater than 0, print "Number is positive."
System.out.println("Number is positive.");
} else {
// If num is not greater than 0, print "Number is non-positive."
System.out.println("Number is non-positive.");
} }
}
2.1.7 Nested If Else Statement in Programming
Nested If Else Statements are a fundamental concept in programming.
They allow us to create more complex decision-making structures by
placing one if-else statement inside another. In this article, we will
discuss the Nested if else statement.
• What is Nested If Else Statement?
Nested if-else statements allow for more complex decision-making within the
program. You can nest if-else statements with other if-else statements, creating
conditions at multiple levels.
• Syntax of Nested If Else Statement:
if (condition1) {
// Code block for condition1 being true if
(condition2) {
// Code block for condition1 and condition2 both being true
} else {
// Code block for condition1 being true and condition2 being false
}}
else {
// Code block for condition1 being false }
• Nested If Else Statement in Java:
Here is the implementation of Nested if else statement in Java language:
public class Main {
public static void main(String[] args) {
// Declare and initialize the variable num
int num = 10;
// Outer if-else statement to check if num is greater than 0
if (num > 0) {
System.out.println("Number is positive.");
// Nested if-else statement to check if num is even or odd
if (num % 2 == 0) {
System.out.println("Number is even.");
} else {
System.out.println("Number is odd.");
} else {
// Execute if num is not greater than 0
System.out.println("Number is non-positive.");
} }
2.1.8 Java Nested if
Nested if in Java refers to having one if statement inside another if statement. If
the outer condition is true the inner conditions are checked and executed
accordingly. Nested if condition comes under decision-making statement in Java,
enabling multiple branches of execution.
Note:
Normal if condition checks condition independently which means each condition
works on its own.
Whereas nested if checks conditions that depend on each other, which means one
condition is only checked if another condition is true.
Syntax of Nested if
if (condition1) { if
(condition2) { if
(condition3) {
// statements;
}
}
}
Note: If the outer condition satisfies then only the inner condition will be checked.
Along with if condition, else condition can also be executed.