Tuesday, January 25, 2022

How to print numbers in ascending order by using for loop and while loop| JavaScript

 If you are learning JavaScript, these simple program like how to How to print numbers in ascending order by using for loop and while loop? is very helpful . You can understand how actually loops works by these program.

Lets Start how to make print 1 to100 by using two way for loop and while and loop

Let's start with For loop

Step 1 Create Function

function dec() {

}

Step 2 inside function use for loop 

Step 3 call function dec()

 for(let k=1k<=50k++){
            
                console.log(`Even number is ${k}`)
            }

Video Source 

In English



In Hindi
 


Now lets understand logic. 

First we write for and then inside () we write our logic.

first we declare our variable as K and since we want to print number from 1 it is our starting point.

Now we want to specify our condition since when loop will work , in this case our k is not reach to 50.  and k++  means every time will add one. In other word k = K+1 but k++ is short form. 

So in this case k start with 1 and check with the condition that K is less than or equal to 50? in this case yes so next step will console our statement and then it will add 1. So now K becomes 2 , and loop going on until it reach to 51 and it breaks. 


        function dec(){
            for(let k=1k<=50k++){
            
                console.log(`Even number is ${k}`)
            }
        }
dec()



Now we understand with While loop 

 Syntax of while loop is little different

Step 1 first we declare variable 

let i = 1

Step 2 now  specify condition 

while (1<=50) 

then {

{

and inside console.log (i) and main important thing do not forget i++other wise loop will not end .

      function dec(){
            let k = 1;
            while(k<=20)
           {
             
            console.log(k)
             
              
               k++;
           }
           
        }

dec() 


java switch statement | Java Tutorial For Beginners

 Switch statement is alternative and more cleaner way to write code if we have more than two options. we can use else if, this is better way...