For loop yani while loopga yaqin bo'lgan loop.
For loopda while loopdan farqi juda hamma kichik.
For loop hamma while loopning singari ishlaydi yani bir hil.
For loop bilan while loopning bir birdan farqi yozilish tartibida.
While loop:
#include <iostream>
using namespace std;
int main()
{
int son = 1;
while(son <= 10)
{
cout << "Ilmhub" << endl;
son ++;
}
return 0;
}
Natija :
Ilmhub
Ilmhub
Ilmhub
Ilmhub
Ilmhub
Ilmhub
Ilmhub
Ilmhub
Ilmhub
Ilmhub
bu coda while loopning yozilishi;
For loop :
#include <iostream>
using namespace std;
int main()
{
for(int i = 1; i <= 10; i++)
{
cout << "Ilmhub" << endl ;
}
return 0;
}
Natija:
Ilmhub
Ilmhub
Ilmhub
Ilmhub
Ilmhub
Ilmhub
Ilmhub
Ilmhub
Ilmhub
Ilmhub
For loop ning While loopdan farqi:
1
while loopning nechidan boshlanishi bildiruvchi integerni loopdan tashqarida yaratsa boladi yani :
int son = 1;
lekin for loopda esa loopning shart berdigan yani () shu ikki qavusning ichida for loopni nechidan boshlanishi yoziladi:
for(int i = 1;)
2
while loopdan while nechidan boshlashi haqida bir integer yaratik yani I = 1; agar biz ga 1 dan boshlab 10 gacha bo'lgan sonlarni chiqarib berishi uchun I++ qilanida bunda har doim loop aylangandan 1 qoshib ketadi:
int i = 1;
while(i <= 10)
{
cout << i << endl;
i++;
}
for loopda esa forning ichiga () shu ikki qavusing orasiga ; shu belgibilan ajjratib yoziladi.Agarda I++ yoki shunga o'hshash ++ bo'ldigan bo'lsa ularning , shu vergul bilan ajjratib bir qatorda yozsa bo'ladi:
for(int i = 1; i <= 10; i++,o++)
Atiga for loop bilan while loopning bir birdan 2 donagin farqi bor ekan!
Top comments (0)