Control Flow Statements in Java

  • By Kuldeep Singh
  • January 19, 2024
  • JAVA Programming
Control Flow Statements in Java

Control Flow Statements in Java

Control flow statements in Java govern the sequence and execution of code, directing the program’s flow based on conditions and loops. Do read our blog on Learn Java Programming

Try that all code will not every time means when we login at the time login code will run and when we log out at that time logout code will run

if(){

 

}

if(true){

 

}

//here is the condition should be true then only the code which is written in curly braces will execute and vice versa

const isUserloggedIn = true

if (isUserloggedIn){

 

}

//here if the condition will run

For checking the condition we have condition operators like less than <, greater than < , less than equal to <=, greater than equal to >=, equality operator = =, not equal !=, strict equality operator = = = it is use for type checking.

= Single equal is for assignment operator

if (2 == ‘2’){

    console.log(“execute”);

}

//here if condition will run

if (2 == ‘2’){

    console.log(“execute”);

}

console.log(“Nor execute”);

//here both will print

if (2 != 3){

    console.log(“execute”);

}

console.log(“Nor execute”);

//here both will print

if (2 !== 3){

    console.log(“execute”);

}

console.log(“Nor execute”);

//here both will print

const isUserloggedIn = true

const temperature = 41

if ( temperature < 50 ){

    console.log(“less than 50”);

    console.log(“temperature is greater than 50”);

//here both will print

For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

const isUserloggedIn = true

const temperature = 41

if ( temperature < 50 ){

    console.log(“less than 50”);

} else {

    console.log(“temperature is greater than 50”);

}

//here true part will only execute

const isUserloggedIn = true

const temperature = 41

if ( temperature < 50 ){

    console.log(“less than 50”);

} else {

    console.log(“temperature is greater than 50”);

}

const score = 200

if (score > 100) {

    let power = “fly”

    console.log(`User power: ${power}`);

}

//output is User power: fly

const score = 200

if (score > 100) {

    let power = “fly”

    console.log(`User power: ${power}`);

}

console.log(`User power: ${power}`);

console.log(“Run”);

//here we will get error that power is not defined becz we are calling the power variable outside the scope.

//Short hand property

const balance = 1000

if (balance > 500) console.log(“test”),console.log(“test2”);

//output   test              test2

 

//nested if else

if (balance < 500) {

        console.log(“less than 500”);

    } else if (balance < 750) {

        console.log(“less than 750”);

        

    } else if (balance < 900) {

        console.log(“less than 750”);

        

    } else {

        console.log(“less than 1200”);

    }

//&& operator

const userLoggedIn = true

const debitCard = true

if (userLoggedIn && debitCard && 2==2) {

    console.log(“Allow to buy course”);

}

const userLoggedIn = true

const debitCard = true

const loggedInFromGoogle = false

const loggedInFromEmail = true

if (userLoggedIn && debitCard && 2==3) {

    console.log(“Allow to buy course”);

}

if (loggedInFromGoogle || loggedInFromEmail) {

    console.log(“User logged in”); } //User logged in

For Free, Demo classes Call: 020-71173125

Registration Link: Java Training in Pune!

 

When we check a value with multiple conditions at that time we use switch case=>

switch case is like we are having so many looks and a single key and the key matches with the particular look we will open that lock 

 

const month = “march”

 

switch (month) {

    case 1:

        console.log(“January”);

        break;

    case 2:

        console.log(“feb”);

        break;

    case 3:

        console.log(“march”);

        break;

    case 4:

        console.log(“april”);

        break;

 

    default:

        console.log(“default case match”);

        break;

}

Output is=> march

 

The importance of break is that if we remove break from the end of every line and suppose any one condition matches the particular case then the case will print as an output and the cases that are present after that match condition rest of the cases will print except default case means default case will not print, it will only print when no cases will match. So to resolve this problem we use a break statement. So we can say that break will break the control flow.

Suppose we have string value that =>

//switch case with string value

const month = “march”

switch (month) {

    case “jan”:

        console.log(“January”);

        break;

    case “feb”:

        console.log(“feb”);

        break;

    case “march”:

        console.log(“march”);

        break;

    case “april”:

        console.log(“april”);

        break;

 

    default:

        console.log(“default case match”);

        break;

}

 

Nullish Coalescing Operator (??) => 

In this mostly we talk about null and undefined

let val1;

val1 = 5 ??  10

console.log(val1);  //5

 

For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

 

As we can see we are getting the first value as and output so the use of => ?? => is when we are calling data from a database or using Firebase at that time we are not getting a response directly we are getting two values so becz of this there are changes that we will get a null response or there is chances that we will not get any response means we will get undefine by these types of issues our whole code structure will effect. So to handle such a case =>Nullish Coalescing Operator (??).  We can also say that this operator is a safety check of null value

let val1;

val1 = null ??  10

console.log(val1);  //10

 

let val1;

val1 = undefined ??  20

console.log(val1);  //20

 

let val1;

val1 = null ?? 10 ?? 20

console.log(val1);  //10

 

Terniary Operator=>

condition ? true : false

const iceTeaPrice = 100

iceTeaPrice <= 80 ? console.log(“less than 80”) : console.log(“more than 80”)  //more than 80   

Do watch the video on Java: Click Here 

 

Author:-

Kuldeep Singh

Call the Trainer and Book your free demo Class for Java Call now!!!
| SevenMentor Pvt Ltd.

© Copyright 2021 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*