Lesson 11 of 34

while और do-while Loops

8 min 25 XP

जब पहले से पता न हो कि loop कितनी बार चलेगा, तब while का उपयोग करें। यह condition false होने तक चलता है। Condition बदलना भूल गए तो loop कभी खत्म नहीं होगा!

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

do-while अपनी condition अंत में जाँचता है, इसलिए body कम से कम एक बार ज़रूर चलती है।

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

n % 10 आखिरी अंक देता है और n / 10 उसे हटा देता है। Interviews में ज़्यादातर digit वाले सवाल इन्हीं दो tricks से हल होते हैं।

Challenge

+25 XP

एक संख्या पढ़ें और उसके अंकों का योग print करें। Input: 4721 → Output: 14

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.