الاثنين، 13 فبراير 2017

برمجة نضري لغة qbasic if statement

بواسطة : Unknown بتاريخ : 10:57 ص

Saturday, 10 October 2015

IF…THEN…ELSE…END IF...ELSE IF COMMANDS IN QBASIC

The IF and THEN commands are used to compare an expression and then perform some task based on that expression.

Example: 

      x=5

      if x=5 then print "Yes"

      Output: Yes

Using the ELSE command, we can have the program perform a different action if the statement is false.  

Example: 

      x=5

      if x=5 then print "Yes" else print "No"

      Output: Yes 

END IF command allows us to have multiple commands after the IF…THEN statement, but they must start on the line after the IF statement. END IF should appear right after the list of commands. 

Example 1:                                            Example 2:

      x=5                                                       x=16

      if (x=5) then                                           if (x=5) then

      input a$                                                  input a$

      print a$                                                   print a$

      end if                                                      else

      end                                                        print x*2

                                                                    end if
                                                                    end

                                                                   Output: 32

The ELSE IF command allows us to perform a secondary action if the first expression was false. Unlike ELSE, this task is only performed if specified statement is true.  

Example 1:                                                   

x=6

If (x=6) then

Print “statement 1 is true”                           

Else if (x=6) then

Print “Statement 2 is true”

End if 

          Output: Statement 2 is true

 Example 2:     

          x=8

          If (x=5) then

          Print “Statement 1 is true”

          Else if (x=6) then 

          Print “Statement 2 is true”                 

          Else 

          Print “No above statement is true”  

          End if  
          End 

          Output: No above statement is true  

Multiple expressions: You can have more than one expression in IF … Then by using either the OR operator or the AND operator.

Example:   x=5

                   If (x=5 or x=20) then print “Yes”

                   Output: Yes

Example:   x=5

                   If (x>2 and x<6) then print “True”

                   Output: True

Example:   x=16

                   Y=3

         If ((x>5 and x<10) or y=3) then print “OK”

        Output: OK  

Example:    x$=”Hello”

                    If (x$=”Hello” or x$=”Bye”) then print x$

                    Output: Hello 

If the THEN part and the ELSE part, if any, can contain one or more IF-THEN-ELSE-END IF statement then it is a Nested IF-THEN-ELSE-END IFprogram.

Example:

            IF (logical-expression) THEN

            statements

            IF (logical-expression) THEN

            statements

            ELSE

            statements

            END IF

            statements

            ELSE

            statements

            IF (logical-expression) THEN

            statements

            END IF

            statements

            END IF

 

PROGRAMS:   

Q. Write a program to display the greater number between two numbers.

CLS  

Input “First number=”; a

Input “Second number=”;b

IF a > b THEN

PRINT  a; “ Is Greater Than ” ;b

ELSE      

PRINT  b;“ Is Greater Than ”; a

END IF   
End

Q.   Write a program to calculate that the numbers are same or not. If not display the greater number between the two numbers.

CLS

Input "Enter First Number: ", Num1    

Input "Enter Second Number: ", Num2   

If Num1 > Num2 THEN

Print Num1; "Is Greater Than"; Num2

Elseif Num2 > Num1 THEN       

Print Num2; " Is Greater Than"; Num1

Else    

Print “The Numbers Are The Same”

End if
End

Q. Write a program to enter any number and check that it is less than or greater than or equal to 100. 

Cls

Input "Enter a number: ", Number 

If Number < 100 then

Print "Your number was less than 100"         

Else

Print "Your number was greater than or equal to 100" 

End if  
End

Q. Write a program to enter any number and find out whether it is negative or positive.

Cls
Input “Enter the number”; N
If N>0 Then
Print “ The number is positive”
Else
Print “The number is negative”
end if
End 

Q. Write a program to print first 20 natural numbers using label.

Let count = 1

More: 

Print count 

Let count = count + 1 

If count < 21 then goto More 

End

Note: Line 2 is a label in our little program. A label is a single word that is not a QBASIC command (we call those reserved words because they are reserved for a special purpose) followed immediately by a colon. A label is a way to tell the computer where things are. We use a label to tell the computer where we will want to find something later on. In line 5, we look for that label, so that the computer program can jump to the next line (line 3) in the program and continue on from there. The word LET is actually optional!

Q. Write a program to compute the Summation (S) of values from 1 to 100 where:

S = 1+2+3+…………100.

Cls

S = 0

I = 1

More:

S = s + i

I = i + 1

Print s

If i <= 100 then goto More

End

Q. Write a program to compute the summation for even numbers from 0 to N.

Cls

S = 0

I = 0

Input “limit=”; n

More:

S = s + i

I = i + 2

Print s

If i <= n then goto More

End

Q. Write a program to compute Summation of X values from list of 10 different numbers. 

Cls

S = 0

I = 10

More:

Input "value="; x

S = s + x

I = i - 1

If i >= 1 then goto More

Print s

End

Q. Write a program to input any number then determine if the number is odd or even.

Cls

More:

Input “number=”; n

If n<0 p then print”Number is integer” else print “Number is not an integer”

Input” Want to continue”; a$

If a$=”y” or a$=”Y” goto More

End

OR

Input X

If X= int(X) then print "x is integer" If X<> int(X) then Print "x is not integer"

Q. Write a program to calculate and print even numbers between 10 and 99.  

Cls

I= 10

More:

Print i

I=i+2

If i<=99 then goto More

end 
  

    Q. Write a program to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks. 

CLS

INPUT "The marks of Maths, Computer, History, English: "; M, C, H, E
Grade = (M + C + H + E) / 4

IF (Grade >= 40) THEN

    PRINT "Pass"

ELSE

    PRINT "Fail"

END IF

END 

Q. Create a calculator on QBasic. 

CLS

PRINT "Welcome to the QBasic Calculator!"

INPUT "Enter two numbers."; nm1, nm2

PRINT "Choose a basic operation:"

PRINT "1. Add 2. Subtract 3. Multiply 4. Divide "

INPUT operation

IF operation = 1 THEN PRINT "The result is"; nm1 + nm2

IF operation = 2 THEN PRINT "The result is"; nm1 - nm2

IF operation = 3 THEN PRINT "The result is"; nm1 * nm2

IF operation = 4 THEN PRINT "The result is"; nm1 / nm2

END

_________________________________________________________________ 

New Age Tutorial at 08:09

Share

 

No comments:

Post a Comment

Home

View web version

About Me

New Age Tutorial 

View my complete profile

Powered by Blogger.


يتم التشغيل بواسطة Blogger.

جميع الحقوق محفوضة لذى | السياسة الخصوصية | Contact US | إتصل بنا

تطوير : حكمات