Condition true ஆகும் வரைக்கும் ஒரு particular block of statement-யை execute செய்யும்.
Syntax:
While(condition)
{
Block of statement;
}
முதலில் condition-யை check செய்யும்.
condition ஆனது true-ஆக இருந்தால் block of statement ஆனது execute செய்யப்படும்.
மீண்டும் அது condition-யை check செய்யும்.
condition true-ஆக இருந்தால் block of statement-யை execute செய்யும் .
இதே போல் அந்த condition ஆனது true ஆக இருக்கும் வரை ஒரு block of statement ஆனது execute செய்யப்படும்.
Var count=2;
While(count>0)
{
console. log(“count”);
Count--;
}
Console. Log(“while loop stop”);
விளக்கம்:
முதலில் count என்கிற variable-க்கு ஒரு memory உருவாக்கப்பட்டு அதில் 2 என்கிற மதிப்பு store செய்யப்படுகின்றது.
while statement -யில் condition-ஆனது check செய்யப்படுகின்றது.
(count>0)(ie:(2>0))condition true என்பதால் block-ன் உள்ளே இருக்கும் முதல் statement-ஆன count என்பது display செய்யப்படுகின்றது.
அடுத்த,statement-ஆன count-- என்பது count-ன் மதிப்பிலிருந்து ஒன்றை குறைகின்றது .
அதாவது count-ன் மதிப்பு 2-இருந்து 1-ஆக மாற்றப்படுகிறது .
மறுபடியும் condition check செய்யப்படுகின்றது.
இப்படியே condition true ஆகின்ற வரைக்கும் check செய்யப்படும்.
condition false ஆகின்ற போது loop-ன் வெளியே இருக்கின்ற while loop stop என்கிற message ஆனது display செய்யப்படும்.
இப்படி தான் while loop ஆனது வேலை செய்யப்படும்.
No comments:
Post a Comment