Conditional statements and Loops in Java || Java + DSA course || Blog 3

Conditional statements and Loops in Java 

In our daily life, we use conditional statements and loops every day. while shopping, choosing a friend to everything.BUT....what are conditional Statements and Loops?

for a quick overview, we will be learning following for this blog

  • if else conditional statement
  • switch() function
  • for loop
  • while loop
  • do while loop

If else conditional statement


 

Lets say you want to buy a frame as a gift for your best friend. The frame costs 100 rs or more.If you don't have that much money you cant buy it.

Lets code it in Java

Q: Take the input from the user the amount he/she have. If it is equal or more 100 rs, print "Can purchase a frame " .Else, print "Cannot purchase a frame"

Inputs: 150,100,50

 

import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int amount = sc.nextInt(); // amount is taken as an input
if(amount >= 100){ //here its written as if the amount is more than
                             or equal to 100
System.out.println("Can purchase a frame");
}else{//if amount is not more than or equal to 100
System.out.println("Cannot purchase a frame");
}
}
}


Anything written after // in straight line is called a Comment. it doesn't change the code and is used for better understanding the working of it. It is highly recommended to use it .

If input is 150 or 100, it will print Can purchase a frame

If input is 50, it will print Cannot purchase a frame

Now lets say you can buy a mug if you have at least 50 to 100 rs.This can be done using else if statement 

 
if(amount >= 100){ //here its written as if the amount is more than 
                            or equal to 100
System.out.println("Can purchase a frame");

}else if (50<=amount){//it sees if amount is more than 100
System.out.println("Can purchase a mug");
}
else{//if amount is not more than or equal to 100
System.out.println("Cannot purchase a frame");
}

Here , if input is 70, it will first check if it is more than or equal to 100. Since it doesn't satisfy the condition it will not run code inside the brackets and move towards else if(). Here it satisfies the condition therefore it will run Can purchase a mug

Switch() function

You just cannot keep writing else if function if there are many conditIons. Here switch function is used .

Lets say you want to make a calculator. You take three inputs from user, 2 int and 1 asking whether +,-,*,/.

import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int number1 = sc.nextInt(); //taking 1st number
int number2 = sc.nextInt(); //taking 2nd number
System.out.println("Choose an operator: +, -, *, or / ");
char operator = sc.next().charAt(0);//since symbols comes under char
switch(operator){
//if operator is + run this case
case '+': System.out.println(number1 + number2);
break;
//if operator is - run this case
case '-': System.out.println(number1 - number2);
break;
//if operator is * run this case
case '*': System.out.println(number1 * number2);
break;
//if operator is / run this case
case '/': System.out.println(number1 / number2);
break;
default:
System.out.println("Invalid Operation");
}
}
}

Here break is written after every case. the reason is after we run our operation we don't want to keep searching it for other cases. If addition is done, we don't want subtraction so we break the brackets and come out of it.

Default is used when any of the above cases doesn't apply.

For loop

Lets say you want to print hello world 5 times. You will say its easy just code system.out.println("hello world"); 5 times. Its true, but what if we have to print it 100 times?Are we going to write it 100 times? .No. Therefore for loops are used. 

Here you use a variable initially at 1 , while that variable is less or equal to 5 , you will print it and increment the value to variable to +1. Now its value is 2. it will again enter loop and print hello world. This will go till variable becomes 5. When it will not satisfy the condition, it will break the loop.

public class Main{
public static void main(String[] args){
for (int variable = 1;variable <= 5;variable++){
//stating a var;giving a condition;incrementing value
System.out.println("Hello World");

}
}
}

The output:


While loop

It also works as similar as for loop. It doesnt need a variable, it just needs a condition.

public class Main{
public static void main(String[] args){
int a = 1;
while(a<=5){
System.out.println("Hello World");
a++;

}
}
}


Do while loop

This loop runs at least once even if the condition is not true

Lets say  we have a=1 variable .we put a false condition in do while loop that a == 5. since it is not it shouldn't run. But in do while loop the loop runs one time.


______________________________________________________________________________________

Blog 2 : https://javaanddsa.blogspot.com/2022/06/data-types-in-java-java-dsa-course-blog.html#more

Blog 1 : https://javaanddsa.blogspot.com/2022/05/introduction-to-java-java-dsa-course.html


Did you like our Blog 3

Please tell us in the comment section

Comments

Post a Comment

Popular posts from this blog

LinkedList Problems for Beginners DSA || Java and DSA course || Blog 17

Introduction to Java || Java + DSA course || Blog 1

LinkedList in DSA || Java and DSA course || Blog 16