Data Structure Basics :
1.Normal Statement
2.Conditional Statement
3.Control flow Statement
Normal Statement:
-->we declare variables and constants by specifying their data type and name. A variable holds a value that is going to use in the Java program.
For example:
int quantity = 20;
boolean flag = false;
String message = "Hello;
Conditional Statement:
==>Conditional statements in programming are used to control the flow of a program based on certain conditions.
==>These statements allow the execution of different code blocks depending on whether a specified condition evaluates to true or false, providing a fundamental mechanism for decision-making in algorithms.
If Statement:
-->The if statement is the most basic form of conditional statement. It checks if a condition is true. If it is, the program executes a block of code.
-->if condition is true, the if code block executes. If false, the execution moves to the next block to check.
Syntax:
if (condition) {
// code to execute if condition is true
}
Flowchart:
Example Program:
package day1if;
public class Players
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
int Cricket = 11;
int basketball = 12;
if(Cricket<basketball)
{
System.out.println("it is true");
}
}
}
Output:
it is true
If-Else Statement:
--> The if-else statement in Java is a powerful decision-making tool used to control the program’s flow based on conditions.
-->It executes one block of code if a condition is true and another block if the condition is false.
Syntax:
if (condition)
{
//Statements to be executed if condition satisfies
}
else
{
//Statements to be executed if the condition is not satisfied
}
Flowchart:
Example Program:
package day1if;
public class UserAge
{
public static void main (String args[])
{
int user = 17;
if(user>=18)
{
System.out.println(" user is 18 or younger");
}
else
{
System.out.println(" user is older than 18");
}
}
}
Output:
user is older than 18
If-Else if Statement:
-->The if-else if statement allows for multiple conditions to be checked in sequence. If the if condition is false, the program checks the next else if condition, and so on.
-->In else if statements, the conditions are checked from the top-down, if the first block returns true, the second and the third blocks will not be checked, but if the first if block returns false, the second block will be checked.
-->This checking continues until a block returns a true outcome.
Syntax:
if (condition)
{
//Statements set 1
}
else if (condition 2)
{
//Statements set 2
}
. . .
else
{
//Statements to be executed if no condition is satisfied.
}
Flowchart:
Example program:
package day1if;
public class ExamResult
{
public static void main (String args[])
{
int mark = 65 ;
if (mark<50)
{
System.out.println("arear");
}
else if (mark<=60)
{
System.out.println(" D grade");
}
else if (mark<=85)
{
System.out.println(" b grade");
}
else
{
System.out.println("pass A grade");
}
}
}
Output:
b grade
Nested if Statement:
-->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:
1.Normal if condition checks condition independently which means each condition works on its own.
2.Whereas nested if checks conditions that depend on each other, which means one condition is only checked if another condition is true.
Syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed
}
}
Flowchart:
Example program:
package day1if;
public class Nestedif
{
public static void main(String args[]) {
int x = 30;
int y = 10;
if( x == 30 )
{
System.out.println("X = 30");
if( y >10 )
{
System.out.print("Y = 10");
}
else if(y<10)
{
System.out.println("Y = 0");
}
else
{
System.out.println("it's false");
if(y==20)
{
System.out.println("Y = 10");
}
else
{
System.out.println("it's False");
}
}
}
}
}
Output:
X = 30
it's false
it's False
Reference:
->https://www.javatpoint.com/types-of-statements-in-java
->https://www.geeksforgeeks.org/conditional-statements-in-programming/
->https://www.scholarhat.com/tutorial/java/java-conditional-statements-if-else-switch
Top comments (0)