2020-10-20
  • |
  • daafoor
  • |
  • مشاهدات: 971

العبارات الشرطية :

العبارات الشرطية تسمح لنا بتنفيذ كتلة من التعليمات البرمجية عندما يكون الشرط المحدد صحيحًا.

يقيم الشرط القيم المنطقية TRUE أو FALSE ويتم اتخاذ القرار بناءً على هذه القيم المنطقية.

 

مثال يختبر عدد ما اذا كان اصغر من 20 :

program ifProg
implicit none
   ! local variable declaration
   integer :: a = 10
 
   ! check the logical condition using if statement
   if (a < 20 ) then
   
   !if condition is true then print the following 
   print*, "a is less than 20"
   end if
       
   print*, "value of a is ", a
 end program ifProg

//outputs
// a is less than 20
// value of a is 10

 

مثال اخر :

program markGradeA  
implicit none  
   real :: marks
   ! assign marks   
   marks = 90.4
   ! use an if statement to give grade
  
   gr: if (marks > 90.0) then  
   print *, " Grade A"
   end if gr
end program markGradeA   

 

ويمكننا استخدام if-then else حيث  انه إذا كانت قيمة الشرط المعطى صحيحة ، فعندئذ يتم تنفيذ if وإذا كان الشرط المعطى خاطئًا ؛يتم تنفيذ else

مثال عن استخدام if then else :

program ifElseProg
implicit none
   ! local variable declaration
   integer :: a = 100
 
   ! check the logical condition using if statement
   if (a < 20 ) then
   
   ! if condition is true then print the following 
   print*, "a is less than 20"
   else
   print*, "a is not less than 20"
   end if
       
   print*, "value of a is ", a
	
end program ifElseProg

 

مثال عن استخدام if else if else : 

في حال لدينا عدة احتمالات :

program ifElseIfElseProg
implicit none

   ! local variable declaration
   integer :: a = 100
 
   ! check the logical condition using if statement
   if( a == 10 ) then
  
      ! if condition is true then print the following 
      print*, "Value of a is 10" 
   
   else if( a == 20 ) then
  
      ! if else if condition is true 
      print*, "Value of a is 20" 
  
   else if( a == 30 ) then
   
      ! if else if condition is true  
      print*, "Value of a is 30" 
  
   else
   
      ! if none of the conditions is true 
      print*, "None of the values is matching" 
      
   end if
   
   print*, "exact value of a is ", a
 
end program ifElseIfElseProg

هل أعجبك المحتوى؟

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

التعليقات
لا يوجد تعليقات
لاضافة سؤال او تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول