Jumat, 24 Mei 2013

Perbedaan pengulangan While dan For pada Java

When would you use a while loop rather than a for loop and vice versa?


Bagi teman2 yang ingin tahu atau mau mengerjakan tugas kampus, berikut jawaban yang membedakan anatara While loop dan For loop di java, dikarenakan saya cuma copas, tolong diterjemahkan melalui mbah google atau mungkin yang jago bahasa inggris sok mangga di terjemahkan :


Answer:
The golden rule in iteration: everything done with a for loop can be done with a while loop, BUT not all while loops can be implemented with a for loop.
for-loops are just a short-cut way for writing a while loop, while an initialization statement, control statement (when to stop), and a iteration statement (what to do with the controlling factor after each iteration).

Examples of for-loops

The most basic use for using for-loops is to do something a set number of times:
for(int k = 0; k < 10; k++); // this loops runs for 10 times
another less common use of the for-loop is traversing raw listNodes, since it does contain an initialization(finding the first node), control (as long as there is a next node), and a iteration statement (get my next node). i.e.:
for(ListNode temp = startingNode; temp != null; temp = temp.getNext); // this traverses the entire ListNode list and stops when it has exhausted the list

How to implement for-loops using while loop

Basically for loops are just short hand for while loops, any for loop can be converted from:
for([initialize]; [control statement]; [iteration]);
to
[initialize];
while([control statement]) { //Do something [iteration]; }
These two does the exact same thing.

For When Only while Loop can be used

while-loops are used when the exiting condition has nothing to do with the number of loops or a controll variable, maybe you just want to keep prompting the user for an input until the given input is valid, like the following example which demands a positive number:
int x = [grab input];
while(x < 0) {
// Do code 
x = [grab input]; 
}

But For Loops Can Do Anything A While Loop Can!

It is true that, when used as intended, a for loop cannot do everything a while loop can, however, in reality, for loops are just as versatile. For example, the above while loop can easily be rewritten to be a for loop as so:

for(int x = [grab input]; x < 0; x = [grab input]){

// Do Code

}

The above for loop behaves exactly like the while loop in the previous heading. A better example of a while loop that should not be a for loop might be:

while(true){
// Do some processing
// Check some condition. If condition is met, break out of loop.
// Do some more processing.
}

Here, the checking of the condition comes in the middle of the processing for the while loop, whereas the condition checked in a for loop is always done at the beginning of the loop. Also, the "iteration" statement is non-existant and is a factor of processing done somewhere else in the while loop. Finally, there was no initialization for this while loop. However, this while loop can still be written as a for loop:

for(;true;){
// Do some processing
// Check some condition. If condition is met, break out of loop.
// Do some more processing.
}

As you can see, a for loop is exactly like a while loop if you leave out the initialization and iteration sections (you still needs the semicolons, to signify those parts of the for loop are still there, they just do nothing). However, it is clear that when you do not need the extra portions of the for loop, why not just use a while loop? 

sumber :

Selasa, 07 Mei 2013

Menghitung Elemen array 2 dimensi perbaris dan perkolom dengan Pascal

Assalamu'alaikum warahmatullahi wabarakatuh,

teriring salam dari penghuni langit, akhirnya setelah berhari-hari mencari-cari dan mengutak atik source code program, akhirnya ditemukan juga bagaimana cara menghitung rata-rata perbaris dan perkolom dari sebuah matriks atau array 2 dimensi, pada awalnya memang susah tapi karena saat KUIS di kampus membuat bingung dan penasaran akhirnya membuahkan hasil juga, berikut source codenya :


program dimension2;
uses crt;
var
   nilai: Array [1..50,1..50] of integer;
   n,m,i,j:integer;
   rata21,rata22:real;
   totalbaris,totalkolom:array[1..50]of integer;
begin
   clrscr;
   for i:=1 to 5 do begin
      for j:=1 to 3 do begin
      write('insert nilai A : ',i,',',j,':');
      readln(nilai[i,j]);
      totalbaris[i]:=totalbaris[i]+nilai[i,j];
      totalkolom[j]:=totalkolom[j]+nilai[i,j];
      end;
   end;
   for i:=1 to 5 do begin
      for j:=1 to 3 do  begin
      write(nilai[i,j]:3);
      end;
      writeln;
   end;
   for i:=1 to 5 do
      begin
      rata21:=(totalbaris[i]/j);
      writeln('* rata-rata baris[',i,']=',rata21:2,1,'*');
   end;

   for j:=1 to 3 do
      begin
      rata22:=(totalkolom[j]/i);
      writeln('* rata-rata kolom[',j,']=',rata22:2,1,'*');
      end;
      readln;
end.

berikut outputnya :

ok teman2 silahkan cek dengan kalkulator dan buktikan hasilnyaselamat belajar, jangan lupa komentarnya !!!