Privacy Policy

Header Ads Widget

java tutorial somo la 11

 

SOMO LA 11


Java  while loop 

Loops zinaweza ku execute block ya code mpaka itakapofikia condition iliyopangwa .

Java while loop 

While loop ina pitia block ya code kutafuta condition iliyopangwa. 

Yaani yenyewe ina execute tu mpaka itakapo pata condition sahihi .

Syntax

 while(condition){

// block ya code 

}


kwenye mfano hapo chini , kodi kwenye loop zita run tena na tena mpaka variable ya (i) itakapo kuwa less than (5)


mfano:


int i = 0;

while (i<5){

System.out.println(i)

i++;

}



The do/ while loop

do/while loop 


Loop hii ita execute block ya code mara moja kabla kabla haija check condition kama ni kweli,

 kisha ita rudia rudia ku execute loop 

mpaka itakapo kuwa true.


syntax:

do{

// code block hapa

} while (condition):



mfano:

int i = 0;

do{

System.out.println(i):

i++;

} while (i<50);



Java for loop 

kama unajua ni mara ngapi unataka ku loop kwenye block ya code zako tumia for loop badala ya while loop 


syntax:

for (statement1;statement2;statement3){

//block ya code zako

}


statement 1;

Hii inakuwa execute mara moja tu kabla ya block ya code zako

statement2:

hii ina define condition au namna ambayo code block yako iwe executed

statement3:

Hii ina kuwa executed mara zote baada ya code block yako kuwa executed.

mfano hapo chini uta print namba kuanza 0 mpaka 4.


mfano:

for (int i =0; i<5;i++){

System.out.println(i);

}



Java Nest Loops 



pia inawezekana ku weka loop kwenye loop ingine na hii ndio  huitwa nested loop

ile ya ndani inaitwa "inner loop"

ile ya nje inaitwa " outer loop"

mfano: // outer loop

for(int i = 1; i<= 2 ; i++){

System.out.println("outer: " + i);


//Inner loop

for (int j=1;j<=3;j++){

System.out.println("Inner: " +j);

}

}



Mwisho wa somo la 11

Jifunze Zaidi

SOMO LA UTANGULIZI

SOMO LA 1

SOMO LA 2

SOMO LA 3

SOMO LA 4

SOMO LA 5

SOMO LA 6

SOMO LA 7

SOMO LA 8

SOMO LA 9

SOMO LA 10

SOMO LA 11

SOMO LA 12

SOMO LA 13



Post a Comment

0 Comments