Explain conditional statement and loop statement with syntax, program flowcharts and discription

KSB

In Java, conditional statements like if, else if, and else allow you to control the program flow based on conditions.


 else if statement: It is used when we have only one if block, multiple else if blocks, and at the last else. block.

Syntax-

if (condition) 

{

Statement 1;

}

else if (condition 2);

{

Statement 2;

}

else

{

 statement 3;

}

 


The if-else statement in Java allows you to make decisions in your code based on conditions. It follows this basic structure:

 

java

Copy code

if (condition) {

 // Code to execute if the condition is true

} else {

 // Code to execute if the condition is false

}



The condition is a true or false expression.

If the condition is true, the code inside the if block runs.

If false, the code inside the else block runs.

Program-

public-class condition. else if {

public static void main (string[] args){

 

   int marks;

System.out.print("Enter marks:");

Scanner obj = new Scanner(System.in);

mark= obj. nextInt();

 

if (mark>= 60 && marks <=100)

{

system outprint ("First Division");

}

else if (mark>=45&& mark c=60)

{

System.out.print ("second division");

}

else if (mark>=33 && mark <=45)

{

System.out.print("thind division");

}

else

{

System out.print("Failed")

}

}

 


Output-

Enter mark = 66

First Division

 

 

Enter mark = 32

Fail



#nested if else:

Whenever we define an if else block inside another if else block, it is called nested if else.

In Java, a nested if-else statement is a structure where multiple if-else statements are placed within each other. This enables checking for various conditions in a hierarchical way. The inner if-else statements are nestled inside the outer ones. Nested if-else statements allow for more intricate decision-making based on multiple conditions, but it's crucial to maintain clarity and readability for better code understanding.

 

Syntax -

if (condition) 

{

if (condition) 

{

//code;

}

else

{

}

}

else

{

//code

}

}


#Control flow

 

Looping statement

1) for

2) while

3) do while

4) for each

 

What do you mean by loop?

loop: Whenever we want to repeat certain statements several times, we should write those statements inside the loop.

 In Java, loop statements like for, while, and do-while enable the repeated execution of code.

 

*for loop

In Java, the "for" loop is a way to repeatedly execute a block of code for a specified range of values.

Syntax -

 For(initialization;condition;inerlder

{

//code

}



This loop initializes a variable (i), checks a condition (i < limit), executes the code inside, and increments the variable (i++). The loop continues until the condition is false.

Program -

 

public class For loop {

public static void main (string []args){

 int num; 

system.out.print("Enter any humber:)

Scanner = new Scanner (system.in);

num. neat Int ()

For (int i=1; i<= 10; i++)

{

System.out.prinin (num*1);

}

}

}

 

 

Output-

Enter number -5.

5

10

15

20

25

30

35

40

45

50

 



#while loop:

Whenever we don't know the statement, we use the while loop.

Syntax-

While (condition)

{

    //

}

 

This loop keeps executing the code as long as the specified condition stays true. When the condition becomes false, the loop stops, and the program proceeds to the next statement after the loop.


Program -

public static void main(String[] args) {

 int num;

System.out print ("Enter any number") Scanner s=new Scanner (System.in);

num=s.nextint();

while (num>=0) 

{

if (num>%2==0)

{

system.out print ("even number")

Break;

}

else

{

system.out.print("Odd number");

break;

}

}

}

}

 



#do while loop:

If any body statement is executed rather than one time whenever this statement is false,

Certainly! A "do-while" loop repeats a block of code while a specified condition is true. Unlike a "while" loop, the condition is checked after the code executes.

 

Syntax -

do

{

Statement;

}

while (condition) ;


This ensures the code runs at least once, even if the condition is initially false. If the condition becomes false after the first run, the loop stops.

 

Program-

 

public class do while-loop {

public static void main (string[] args){

int num;

System.out.print("Enter any number: ");

Sanner s=new Sanner (system.in);

num=s.next int () ;

do

{

system.out.print(num+"") ;

++num;

}

while (num<=10) ;

}

}

 

 

Output

1      2     3     4       5       6       7       8       9       10




#for each loop:

In single variables, multiple values are printed one by one.

 In Java, the "for-each" loop is used to go through elements of an array or collection

 

 This loop makes it easier to iterate through elements without managing indexes, automatically handling each item in the specified array or collection.

Syntax-

for (datatype var1: var2)

{

//statement

}

 These loop structures offer flexibility for handling repetitive tasks, whether the number of iterations is known beforehand or not. Choose the appropriate loop based on your program's specific requirements.


Also Read