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
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
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 +,-,*,/.
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.
The output:
While loop
It also works as similar as for loop. It doesnt need a variable, it just needs a condition.
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
Nice blog
ReplyDeleteThis is great
ReplyDelete