QBASIC For| While| Looping| School 8, 9, 10 | Tutorial

 Welcome to QBASIC Looping chapter in this chapter we will learn about looping statement in this chapter we are going to learn  why looping is used in programming.

So I would like to write in Nepali for easy understanding.


यो chapter मा हामी  for, while  looping  को बर्रेमा सिक्ने छौ । यो किन programming मा use  हुन्क्ष? 

एस को कारण येस्परकर छन् :

हामीलाई कैलेकाही  प्रोग्राम तब सम्म चलाउनु पर्क्ष   जवसम्म हामीले भने  जस्तो रिजल्ट औदैन , यो कुरा हामी सबैलाई थाहा छ, र यो कुरा पनि बुझ्न जरुरि छ की  प्रोग्राम जैले पनि माथि बाट तल तिर flow मा जान्छ । येदि हामीले लूपिंग को पर्योग गरेनौ भने  computer ले code लाइ १ चोटी मात्र read  गर्छ र End  हुन्क्ष येसो हुन बाट  रोक्न र एउटै code  बार  बार  repeate  नगर्न को लागि एसको प्रयोग हुन्क्ष ।  


Example:

Using for loop

FOR i = 1 TO 10
    PRINT "2 *"; i; "="; 2 * i
NEXT i

Using While Loop

i = 1
WHILE i <= 10
    PRINT "2 *"; i; "="; 2 * i
    i = i + 1
WEND

येदि हामीले लूपिंग बिना माथि को code  जस्तै Output नीकाल्न यसरि coding गर्नु पर्थेओ । 
 
CLS
PRINT "2 * 1 =", 2 * 1
PRINT "2 * 2 =", 2 * 2
PRINT "2 * 3 =", 2 * 3
PRINT "2 * 4 =", 2 * 4
PRINT "2 * 5 =", 2 * 5
PRINT "2 * 6 =", 2 * 6
PRINT "2 * 7 =", 2 * 7
PRINT "2 * 8 =", 2 * 8
PRINT "2 * 9 =", 2 * 9
PRINT "2 * 10 =", 2 * 10
End

हेर्नुहोस लूपिंग को प्रयोग ले हामीलाई कम automatic गरौन हेल्प गर्ने रैक्ष ।


Now Let's See Examples:

10 for loop and 10 while loop examples Below:

Example #1 Print numbers from 1 to 10

CLS
PRINT "Example 1: Print numbers from 1 to 10"
FOR i = 1 TO 10
    PRINT i
NEXT i
END

Example #2 Print even numbers from 2 to 20

CLS
PRINT "Example 2: Print even numbers from 2 to 20"
FOR i = 2 TO 20 STEP 2
    PRINT i
NEXT i
END

Example #3 Print the sum of numbers from 1 to 100

CLS
PRINT "Example 3: Print the sum of numbers from 1 to 100"
sum = 0
FOR i = 1 TO 100
    sum = sum + i
NEXT i
PRINT "Sum:", sum
END

Example #4 Print a countdown from 10 to 1

CLS
PRINT "Example 4: Print a countdown from 10 to 1"
FOR i = 10 TO 1 STEP -1
    PRINT i
NEXT i
END

Example #5 Print the multiplication table of 5

CLS
PRINT "Example 5: Print the multiplication table of 5"
FOR i = 1 TO 10
    PRINT "5 *"; i; "="; 5 * i
NEXT i
END

Example #6 Calculate the factorial of a number

CLS
PRINT "Example 1: Calculate the factorial of a number"
INPUT "Enter a number: ", num
factorial = 1
FOR i = 1 TO num
    factorial = factorial * i
NEXT i
PRINT "Factorial of"; num; "is"; factorial
END

Example #7 Generate a pattern of asterisks

CLS
PRINT "Example 2: Generate a pattern of asterisks"
FOR i = 1 TO 5
    FOR j = 1 TO i
        PRINT "*";
    NEXT j
    PRINT
NEXT i
END

Example #8 Calculate the sum of even numbers from 1 to 50

CLS
PRINT "Example 3: Calculate the sum of even numbers from 1 to 50"
sum = 0
FOR i = 2 TO 50 STEP 2
    sum = sum + i
NEXT i
PRINT "Sum of even numbers from 1 to 50 is"; sum
END

Example #9 Print a triangle of numbers

CLS
PRINT "Example 4: Print a triangle of numbers"
FOR i = 1 TO 5
    FOR j = 1 TO i
        PRINT j;
    NEXT j
    PRINT
NEXT i
END

Example #10 Calculate the average of a set of numbers

CLS
PRINT "Example 5: Calculate the average of a set of numbers"
INPUT "Enter the count of numbers: ", count
sum = 0
FOR i = 1 TO count
    INPUT "Enter number "; i; ": ", num
    sum = sum + num
NEXT i
average = sum / count
PRINT "Average:", average
END

While Loop Examples:


Example #1 Print numbers from 1 to 10

CLS
PRINT "Example 1: Print numbers from 1 to 10"
i = 1
WHILE i <= 10
    PRINT i
    i = i + 1
WEND
END

Example #2 Print even numbers from 2 to 20

CLS
PRINT "Example 2: Print even numbers from 2 to 20"
i = 2
WHILE i <= 20
    PRINT i
    i = i + 2
WEND
END

Example #3 Calculate the sum of numbers from 1 to 100

CLS
PRINT "Example 3: Calculate the sum of numbers from 1 to 100"
sum = 0
i = 1
WHILE i <= 100
    sum = sum + i
    i = i + 1
WEND
PRINT "Sum:", sum
END

Example #4 Print a countdown from 10 to 1

CLS
PRINT "Example 4: Print a countdown from 10 to 1"
i = 10
WHILE i >= 1
    PRINT i
    i = i - 1
WEND
END

Example #5 Generate a pattern of asterisks

CLS
PRINT "Example 5: Generate a pattern of asterisks"
i = 1
WHILE i <= 5
    j = 1
    WHILE j <= i
        PRINT "*";
        j = j + 1
    WEND
    PRINT
    i = i + 1
WEND
END

Example #6 Calculate the factorial of a number

CLS
PRINT "Example 6: Calculate the factorial of a number"
INPUT "Enter a number: ", num
factorial = 1
i = 1
WHILE i <= num
    factorial = factorial * i
    i = i + 1
WEND
PRINT "Factorial of"; num; "is"; factorial
END

Example #7 Print a triangle of numbers

CLS
PRINT "Example 7: Print a triangle of numbers"
i = 1
WHILE i <= 5
    j = 1
    WHILE j <= i
        PRINT j;
        j = j + 1
    WEND
    PRINT
    i = i + 1
WEND
END

Example #8 Calculate the sum of even numbers from 1 to 50

CLS
PRINT "Example 8: Calculate the sum of even numbers from 1 to 50"
sum = 0
i = 2
WHILE i <= 50
    sum = sum + i
    i = i + 2
WEND
PRINT "Sum of even numbers from 1 to 50 is"; sum
END

Example #9 Find the square of numbers from 1 to 10

CLS
PRINT "Example 9: Find the square of numbers from 1 to 10"
i = 1
WHILE i <= 10
    PRINT "Square of"; i; "is"; i * i
    i = i + 1
WEND
END

Example #10 Print the ASCII values of lowercase alphabets

CLS
PRINT "Example 10: Print the ASCII values of lowercase alphabets"
i = 97
WHILE i <= 122
    PRINT CHR$(i); "=", i
    i = i + 1
WEND
END




If you found this post is useful to you then please share with your friends.







Post a Comment

Previous Post Next Post