Do While

"මම ගිහින් එනකම් පාඩම බලාගන්න", "මම එනකම් halt එකේ ඉන්න", "මම කනකන් TV එක දාල තියන්න" වගේ දේවල් ඔයාල ඔයාලගේ සාමාන්‍ය ජීවිතෙදි අහලා ඇති. මේ අපි කියපු දේම program එකක ලියාගන්නේ do while statement එක බාවිතා කරලා. ඒ කියන්නේ යම් statement එකක් false වෙනකම් දිලතියන code එක execute කරන එක. Example එකක් කරමු එතකොට තේරේවි do while statement එක ගැන.

මෙතැනදී අපි කරන්න යන්නේ userගෙන් input එකක්(number) අරන් ඒ සංඛ්‍යාවට අනුව string එකක් print කරන එක.

import java.util.*;

public class doWhile{
public static void main(String args[]){

System.out.println("Enter a Number");
Scanner sc= new Scanner(System.in);
int num=sc.nextInt();

int count=0;

do{
System.out.println("You Entered "+num);
count++;
}while(count<num);

}
}

Output:



මෙතන අපි count කියලා variable එකක් use කරලා තියනවා. අපි එකේ initial value එක දාල තියෙන්නේ 0 විදියට. අපේ statement එකේදී වෙන්නේ මුලින්ම string එක print වෙනවා. ඊටපස්සේ count variable එක +1 කින් increment කරනවා. ඒ increment වෙලා ආපු count value  එක අපි මුලින් enter කරපු value එකට වඩා වැඩිද බලනවා. වැඩිනම් terminate වෙනවා නැත්තම් නැවතත් කලින් වගේම print වෙලා count එක +1 increment වෙනවා. Count එකෙන් තමයි අපේ string එක print වෙච්ච වාර ගණන බලන්නනේ.