Javascript Lesson 2 - Loops / 1974 ///ENG

Eagleweb

Kıdemli Üye
8 May 2021
2,120
1,151
localhost/e8
logo.png


For Loop Structure

As with other programming languages, Javascript has certain program codes to do the desired operation 2 or more times.This is a for loop command that is very similar to other languages.The process of this command is to repeat the desired function or parts of the function at the desired value.


JavaScript:
for ( değişken_başlangıç_değerler1 , değişken_başlangıç_değeri2 ; döngü sayısı ; değişecek_değişken_adı_ve_türü )

{ yapılması istenen işlemler }

You do not need to type the Starting When Changing_Values and variable to change _name_and_Type part given here in parent brackets.The variable used in the loop can then be increased or decreased as desired.

JavaScript:
for (a=0 , b=0 ; c<=3 ; c++)

{ yapılması istenen işlemler }

for ifadesi için kısa yazılım :

var a,b=0;

for (;c<=3;c++)

{ yapılması istenen işlemler }

Now let's reinforce this with a full example. Let's say we want to make a multiplication table.Accordingly, we will give numbers 1 through 10 for the number 5 in a table.Now in this case we would have to write 10 tables without for loops, but with the for loop we will reduce the process to 1 line.

HTML:
<html>

<head>

<title>for döngüsü</title>

</head>

<body>

<script language="JavaScript">

<!-- //eski sürüm tarayıcılardan kodumuzu saklayalım

var cevap=0;

for ( sayi=0 ; sayi>=10 ;)

{

sayi--;

var cevap = 5 * sayi ;

********.write( "5 * " , sayi , " =" , cevap ,"<br>")

}

//saklamayı bitir-->

</script>

</body>

</html>

Conditional loop structure while

When writing Javascript code , as in the previous example in the program, you may not want the counter variable to take the action you want for each value.To do this, you use the while command, which tells Javascript, "Do what I want if this condition is provided!"you're saying. Unlike the for loop in the While loop, variables within the loop must be defined.


JavaScript:
while ( döngü şartı ) { şart doğruysa yapılacak işlemler}

şart doğru değilse yapılacak işlemler

Do.. while structure

Do... While structure generally means repeat the structure of a loop if the condition is correct.That is, the dollar is initially traded without any conditions.Then, if the while requirement is correct, it is returned to the do structure again.To put this in an example; For example, you have a commercial site.People come from you and buy the products they want.They placed their order for a product and in return we calculated the amount of fees shipping vat and told our customer that you will have the product you want today at the following price.We said to do all these calculations with the do structure.And then we asked if you wanted to buy more products?This is asked with the while structure.If the answer is yes, if the do structure is not repeated, the do loop structure is exited.Let's make this kind of example; Let's say we sell books, CDs and cassettes.Assuming you get two books and three CDs from us.


JavaScript:
var kitap=2000000; var cd=3000000; var kaset=1500000;

do {

var kitapistek =prompt ("Kaç tane kitap almak istiyorsunuz ?" , "lütfen rakam giriniz");

var cdistek= prompt ("Kaç tane cd almak istiyorsunuz ?" , "lütfen rakam giriniz");

var kasetistek= prompt ("Kaç tane kaset almak istiyorsunuz ?" , "lütfen rakam giriniz");

var kitaptutar=kitapistek*2000000;

var cdtutar=cdistek*3000000;

var kasettutar=kasetistek*1500000;

var toplamtutar = kitaptutar+cdtutar+kasettutar;

********.write (kitapistek ," tane kitap ", cdistek ," tane cd " , kasetistek , " tane kaset siparişiniz alınmıştır ", "<br>");

********.write ("<br>" , "Aldığınız ürünlerin toplam tutarı = " ,toplamtutar);

var istek =prompt("Başka ürünlerde satın almak istiyor musunuz ?", "e veya h giriniz"); }

while (istek !="h")

********.write ("<br>" ,"Bizden alışveriş yaptığınız için teşekkürler")


ce6xizh.gif



Break and Continue Expressions
After setting the condition with the While command, it becomes automatic to interrupt or continue the operation.In the for cycle, we also perform such an event with break and contine expressions.As soon as Javascript sees break, it interrupts the looping process and processes the next command line.In Continue, the loop is truncated as in break, but the processed line is not the next line.In Continue, the loop is returned.


JavaScript:
for ()

{işlem1; işlem2; break; }

Burada işlem2 ile verilen kısımda örnek olarak bir sorgu yapılabilir. Sorgu doğru ise break ifadesine gelinir ve burada döngü kesilir.

for ()

{ işlem1; işlem2; continue;}

If the query is made here again with operation2, the continuation of the loop is continued with the contine expression.Important : Break and Continue expressions cannot be used to interrupt or resume each command.For example, if an if statement is not correct, it cannot be said to be cut by break.It can only be used to interrupt or resume the loop within the loop.



Switch-Case Expression

This expression generally works in menu usage or query operations.Swicht takes a statement and the transactions are questioned with the case statement


JavaScript:
switch (parametreler)

{ case "ifade1" :

case "ifade2" :

... }



Let's give you an example. Here we are asked if he wants to appear in any of the transactions on our website.If the answer is yes, the process is directed in the desired direction.If the answer is no, the cycle is exited.Here we find ourselves noticing whether the Caps Lock key is pressed on the visitor's keyboard.Because we write our condition statement in both lowercase and uppercase letters.

JavaScript:
var sec;

sec = prompt ("Çıkmak istiyor musunuz " ,"Evet için E veya e , Hayır için H veya h giriniz")

switch (sec)

{ case "e" : case "E" :

********.write ("Tekrar hoşgeldiniz")

//yapılması istenen işlemler

case "h": case "H" :

********.write ("Bizi tercih ettiğiniz çin teşekkürler")

break //Çıkılması istendiği için döngüyü kesmek için break komutunu kullanıyoruz.

Source : Javascript Ders 2 - Döngüler / 1974
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.