Introduction to C Programming Language ﻣﻘﺪﻣﺔ ﻓﻲ اﻟﺒﺮﻣﺠﺔ ﺑﻠﻐﺔ C
اﻟﻔﺼﻞ اﻟﺮاﺑﻊ :ﺗﺮاﻛﯿﺐ اﻟﺘﻜﺮارِ )اﻟﺤﻠﻘﺎت( )Iteration Structures (Loops
ﺗﺤﺖ ﺷﺮط ﻣﻌ ﯿﻦ ﯾﺘﺤ ﺘﻢ ﻋﻠﯿﻨ ﺎ أﺣﯿﺎﻧ ﺎً ﺗﻨﻔﯿ ﺬ ﺟﻤﻠ ﺔ أو ﻣﺠﻤﻮﻋ ﺔ ﻣ ﻦ اﻟﺠﻤ ﻞ ﻋ ﺪداً ﻣ ﻦ اﻟﻤ ﺮات ،وھ ﺬا ﻣ ﺎ ﯾﻮﺻ ﻒ ﺑ ﺎﻟﺘﻜﺮار .ﻓ ﻲ اﻟﻠﻐ ﺔ Cﺗﻮﺟ ﺪ
ﻣﺠﻤﻮﻋﺔ ﻣﻦ اﻟﺼﯿﻎ اﻟﺘﻲ ﺗﺘﻌﺎﻣﻞ ﻣﻊ ﻣﻔﮭﻮم اﻟﺘﻜﺮار ،وﻣﻦ ھﺬه اﻟﺼﯿﻎ ھﻲ while , do…while , for :
) (4.1اﻟﺤﻠﻘﺔ اﻟﺘﻜﺮارﯾﺔ ﺑﯿﻨﻤﺎ )(The iteration loop while
)while(condition اﻟﺼ ﯿﻐﺔ اﻟﻌﺎﻣ ﺔ
; statement
ﻋﻤﻞ اﻟﺤﻠﻘﺔ : whileأﺧﺘﺒﺮ اﻟﺸﺮط ) (conditionأوﻻً ،ﻓﺄذا ﻛﺎن اﻟﺸﺮط ﺻﺤﯿﺤﺎً ،ﻧﻔﺬ اﻟﺠﻤﻠﺔ ) ،(statementﻛﺮر ھﺬا اﻻﺧﺘﺒﺎر واﻟﺘﻨﻔﯿﺬ ﻟﻐﺎﯾﺔ أن
ﯾﺼﺒﺢ اﻟﺸﺮط ﻏﯿﺮ ﺻﺤﯿﺢ )ﺧﺎﻃﺌﺎً( .
ﻣﻼﺣﻈﺔ) (1) -: (4.1.1ﯾﻤﻜﻦ ﺗﻜﺮار ﺗﻨﻔﯿﺬ أﻛﺜﺮ ﻣﻦ ﺟﻤﻠﺔ ﺿﻤﻦ اﻟﺤﻠﻘﺔ ، whileﺑﺸﺮط أن ﺗﻮﺿﻊ ھﺬه اﻟﺠﻤﻞ ﺿﻤﻦ اﻟﺠﻤﻠﺔ اﻟﻤﺮﻛﺒﺔ ) اﻟﺒﻠﻮك ( .
) (2ﺗﺴﺘﺨﺪم اﻟﺤﻠﻘﺔ whileﻋﻨﺪﻣﺎ ﯾﻜﻮن ﻋﺪد اﻟﺘﻜﺮارات ﻣﻌﻠﻮم أو ﻏﯿﺮ ﻣﻌﻠﻮم .
) (3ﯾﻤﻜﻦ اﺳﺘﺨﺪام أﻛﺜﺮ ﻣﻦ ﺣﻠﻘﺔ ﺗﻜﺮارﯾﺔ ﺑﺼﻮرة ﻣﺘﺪاﺧﻠﺔ ﺿ ﻤﻦ اﻟﺒﺮﻧ ﺎﻣﺞ اﻟﻮاﺣ ﺪ ،وھ ﺬا ﻣ ﺎ أﺻ ﻄﻠﺢ ﻋﻠ ﻰ ﺗﺴ ﻤﯿﺘﺔ اﻟﺤﻠﻘ ﺎت
اﻟﺘﻜﺮارﯾﺔ اﻟﻤﺘﺪاﺧﻠﺔ ) . ( Nested Iteration Loop
ﻣﺜﺎل)-:(4.1.2
Program اﻟﺘﻨﻔﯿﺬ
/*custom countdown using while*/ Enter the starting number : 8
>#include <stdio.h !8, 7, 6, 5, 4, 3, 2, 1, FIRE
)( main
{
; int n
; ) " printf( "Enter the starting number :
; )scanf("%d",&n
)while (n>0
{
; )printf( "%d,",n
;--n
}
; ) "!printf ( "FIRE
}
ﻧﻼﺣﻆ ﻣﻦ ﺧﻼل ﺗﻨﻔﯿﺬ اﻟﺒﺮﻧ ﺎﻣﺞ اﻋ ﻼه أﻧ ﮫ ﺗ ﻢ ﺗﺨﺼ ﯿﺺ اﻟﻘﯿﻤ ﺔ 8ﻟﻠﻤﺘﻐﯿ ﺮ ، nﺛ ﻢ اﺧﺘﺒ ﺎر اﻟﺸ ﺮط )، (n>0ﺣﯿ ﺚ أن اﻟﺸ ﺮط ﻛ ﺎن ﺻ ﺤﯿﺤﺎً ﻓ ﺘﻢ ﻃﺒﺎﻋ ﺔ
اﻟﻘﯿﻤﺔ 8ﺛﻢ أﺻﺒﺤﺖ ﻗﯿﻤﺔ n=7ﻣﻦ ﺧﻼل ﻣﺆﺛﺮ اﻟﻨﻘﺼﺎن --nوﺑﻘﻲ اﻟﺸﺮط ﺻﺤﯿﺤﺎً وھﻜﺬا ﺗﻢ ﺗﻜﺮار اﻟﻌﻤﻠﯿﺔ اﻟﻰ أن أﺻﺒﺤﺖ ﻗﯿﻤﺔ n=0ﻋﻨﺪﺋﺬ أﺻ ﺒﺢ
اﻟﺸﺮط ﻏﯿﺮ ﺻﺤﯿﺢ وﺑﺎﻟﺘﺎﻟﻲ ﺗﻢ اﻟﺨﺮوج ﻣﻦ اﻟﺤﻠﻘﺔ ﻧﮭﺎﺋﯿﺎً ،واﻻﻧﺘﻘﺎل اﻟﻰ ﺗﻨﻔﯿﺬ اﻟﻌﺒﺎرة !. FIRE
4.0,4.5,5.0,5.5,…,9.5,10 ﻣﺜ ﺎل) -:(4.1.3أﻛﺘ ﺐ ﺑﺮﻧﺎﻣﺠ ﺎً ﻟﺤﺴ ﺎب وﻃﺒﺎﻋ ﺔ ﻣﺠﻤ ﻮع اﻷﻋ ﺪاد
Program اﻟﺘﻨﻔﯿﺬ
>#include <stdio.h The Total = 87.00
) ( main
{
; float a=4.0 , sum=0.0
)while (a<=10
{
; sum+=a
; a+=0.5
}
; )printf(" The Total =%f ",sum
}
ﺻﻔﺤﺔ 21 University of Baghdad-College of Science- Department of Mathematics Email : [email protected]
Introduction to C Programming Language ﻣﻘﺪﻣﺔ ﻓﻲ اﻟﺒﺮﻣﺠﺔ ﺑﻠﻐﺔ C
ﻣﺜﺎل) -:(4.1.4أﻛﺘﺐ ﺑﺮﻧﺎﻣﺠﺎً ﻟﺤﺴﺎب وﻃﺒﺎﻋﺔ اﻟﻤﻀﺮوب) (factorialﻟﻸﻋﺪاد ﻣﻦ 1اﻟﻰ ،10ﺣﯿﺚ أن ﻣﻀﺮوب اﻟﻌﺪد (n! )nﯾﺤﺴﺐ ﻛﺎﻻﺗﻲ :
n!=n(n-1)(n-2)…3.2.1
Program اﻟﺘﻨﻔﯿﺬ
>#include <stdio.h 1!= 1
) ( main 2!= 2
{ 3!= 6
; long factorial 4!= 24
; int k ,a=1 5!= 120
)while (a<=10 6!= 720
{ 7!= 5040
; factorial=1 8!= 40320
; k=1 9!= 362880
)while (k<=a 10!= 3628800
{
; factorial*=k
; ++k
}
; )printf("%d!=%d\n ",a ,factorial
; ++a
}
}
ﻓﻲ اﻟﺒﺮﻧﺎﻣﺞ اﻋﻼه ﺗﻢ ﺗﻨﻔﯿ ﺬ ﺣﻠﻘﺘ ﯿﻦ ﺗﻜ ﺮارﯾﺘﯿﻦ ،اﻟﺤﻠﻘ ﺔ أﻻوﻟ ﻰ واﻟﺘ ﻲ ﺗﺴ ﻤﻰ ﺑﺎﻟﺤﻠﻘ ﺔ اﻟﺨﺎرﺟﯿ ﺔ وھ ﻲ ﺗﻨﻔﯿ ﺬ اﻟﺠﻤﻠ ﺔ اﻟﻤﺮﻛﺒ ﺔ ﺑ ﯿﻦ اﻟﻘﻮﺳ ﯿﻦ اﻟﺘ ﺎﺑﻌﯿﻦ ﻟﮭ ﺎ
ﻃﺎﻟﻤﺎ أن اﻟﻌﺪد aﻟﻢ ﯾﺘﺠﺎوز ،10ﺗﺄﺗﻲ ﺑﻌﺪھﺎ اﻟﺤﻠﻘﺔ اﻟﺜﺎﻧﯿﺔ واﻟﺘﻲ ﺗﺴﻤﻰ اﻟﺤﻠﻘﺔ اﻟﺪاﺧﻠﯿﺔ وھﻲ أﯾﺠﺎد ﻣﻀﺮوب اﻟﻌﺪد ،وذﻟﻚ ﻣﻦ ﺧﻼل اﻟﺠﻤﻠﺘﯿﻦ
; factorial*=k
; ++k
) (4.2اﻟﺤﻠﻘﺔ اﻟﺘﻜﺮارﯾﺔ أﻓﻌﻞ...ﺑﯿﻨﻤﺎ )(The iteration loop do… while
do اﻟﺼﯿﻐﺔ اﻟﻌﺎﻣﺔ
; statement
; )while(condition
ﻋﻤ ﻞ اﻟﺤﻠﻘ ﺔ : do…whileھ ﺬه اﻟﺤﻠﻘ ﺔ ﻣﺸ ﺎﺑﮫ ﻟﻠﺤﻠﻘ ﺔ اﻟﺘﻜﺮارﯾ ﺔ whileوﺗﺨﺘﻠ ﻒ ﻋﻨﮭ ﺎ ﻓ ﻲ أﻣ ﺮﯾﻦ ،أوﻻً أن اﻟﺤﻠﻘ ﺔ do…whileﺗﺒ ﺪأ اﻟﺘﻨﻔﯿ ﺬ
ﻟﻠﺠﻤﻠﺔ) (statementﺛﻢ ﺗﺘﺤﻘﻖ ﻣﻦ اﻟﺸﺮط ﺑﻌﺪ ذﻟﻚ ،ﺛﺎﻧﯿﺎ ﯾﺘﻢ اﻟﺘﻔﯿﺬ ﻓﻲ ھﺬه اﻟﺤﻠﻘﺔ ﻣﺮة واﺣﺪة ﻋﻠﻰ اﻻﻗﻞ ﺣﺘﻰ وﻟﻮ ﻛﺎن اﻟﺸﺮط ﻏﯿﺮ ﻣﺘﺤﻘﻖ .
ﻣﺜﺎل) -:(4.2.1أﻛﺘﺐ ﺑﺮﻧﺎﻣﺠﺎً ﻻدﺧﺎل وﻃﺒﺎﻋﺔ ﻋﺪد ﺻﺤﯿﺢ،أوﻗﻒ اﻟﺒﺮﻧﺎﻣﺞ ﻋﻨﺪ أدﺧﺎل ﻋﻨﺪ ادﺧﺎل اﻟﻘﯿﻤﺔ .0
Program اﻟﺘﻨﻔﯿﺬ
>#include <stdio.h Enter number (0 to end): 1254
) ( main You entered: 1254
{ Enter number (0 to end): 160277
; unsigned long n You entered: 160277
do Enter number (0 to end): 33385
{ You entered: 33385
; ) " printf( " Enter number (0 to end): Enter number (0 to end): 0
; )scanf("%d%,&n You entered: 0
; )printf( " You entered: %d\n" ,n
; )}while (n != 0
}
ﺻﻔﺤﺔ 22 University of Baghdad-College of Science- Department of Mathematics Email : [email protected]
Introduction to C Programming Language C ﻣﻘﺪﻣﺔ ﻓﻲ اﻟﺒﺮﻣﺠﺔ ﺑﻠﻐﺔ
-:(4.2.2)ﻣﺜﺎل
Program اﻟﺘﻨﻔﯿﺬ
#include <stdio.h> : 1 اﻟﺘﻨﻔﯿﺬ
main ( ) Enter the starting number : 8
{ 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
int n ;
printf( "Enter the starting number : " ) ;
scanf("%d%,&n) ; : 2 اﻟﺘﻨﻔﯿﺬ
do Enter the starting number : 0
{ 0, FIRE!
printf( "%d , " , n) ;
--n;
} while (n>0)
printf( "FIRE!" ) ;
}
4.0,4.5,5.0,5.5,…,9.5,10 أﻛﺘ ﺐ ﺑﺮﻧﺎﻣﺠ ﺎً ﻟﺤﺴ ﺎب وﻃﺒﺎﻋ ﺔ ﻣﺠﻤ ﻮع اﻷﻋ ﺪاد-:(4.2.3)ﻣﺜ ﺎل
Program اﻟﺘﻨﻔﯿﺬ
#include <stdio.h> The Total = 87.00
main ( )
{
float a=4.0 , sum=0.0 ;
do
{
sum+=a ;
a+=0.5 ;
} while (a<=10) ;
printf( " The Total =%f " ,sum ) ;
}
. 10 اﻟﻰ1 ( ﻟﻸﻋﺪاد ﻣﻦfactorial) أﻛﺘﺐ ﺑﺮﻧﺎﻣﺠﺎً ﻟﺤﺴﺎب وﻃﺒﺎﻋﺔ اﻟﻤﻀﺮوب-:(4.2.4)ﻣﺜﺎل
Program اﻟﺘﻨﻔﯿﺬ
#include <stdio.h> 1!= 1
main ( ) 2!= 2
{ 3!= 6
long factorial ; 4!= 24
int k ,a=1 ; 5!= 120
do 6!= 720
{ 7!= 5040
factorial=1 ; 8!= 40320
k=1 ; 9!= 362880
do 10!= 3628800
{
factorial*=k ;
++k ;
} while (k<=a)
printf("%d!=%d\n ",a ,factorial) ;
++a ;
} while (a<=10)
}
23 ﺻﻔﺤﺔ University of Baghdad-College of Science- Department of Mathematics Email : [email protected]
Introduction to C Programming Language C ﻣﻘﺪﻣﺔ ﻓﻲ اﻟﺒﺮﻣﺠﺔ ﺑﻠﻐﺔ
(The iteration loop for) ( اﻟﺤﻠﻘﺔ اﻟﺘﻜﺮارﯾﺔ ﻷﺟﻞ4.3)
f o r( i n i t i a l i za t i o n ; c o n d i t i o n ; i n c re a s e ) اﻟﺼﯿﻐﺔ اﻟﻌﺎﻣ ﺔ
statement ;
ﺛﺎﻧﯿ ﺎً أﺧﺘﺒ ﺮ اﻟﺸ ﺮط،(( واﻟﺬي ﯾﻤﺜﻞ اﻟﻘﯿﻤﺔ اﻷوﻟﯿﺔ ﻟﻌﺪاد اﻟﺤﻠﻘ ﺔ)ﻋ ﺪاد اﻟﺤﻠﻘ ﺔ ھ ﻮ ﻋﺒ ﺎرة ﻋ ﻦ ﻣﺘﻐﯿ ﺮinitialization) أوﻻً ﻧﻔﺬ: for ﻋﻤﻞ اﻟﺤﻠﻘﺔ
( واﻟﺬي ﯾﻤﺜﻞ ﻣﻘﺪار اﻟﺰﯾﺎدة أو اﻟﻨﻘﺼﺎن ﺑﻤﻘ ﺪار ﻣﻌ ﯿﻦ ﻓ ﻲincrease) ﺛﺎﻟﺜﺎً ﻧﻔﺬ،(statement) ﻧﻔﺬ اﻟﺠﻤﻠﺔ،ً ﻓﺄذا ﻛﺎن اﻟﺸﺮط ﺻﺤﯿﺤﺎ، (condition)
. (ً ﻛﺮر اﻟﺨﻄﻮات ﺛﺎﻧﯿﺎً وﺛﺎﻟﺜﺎً ﻟﻐﺎﯾﺔ أن ﯾﺼﺒﺢ اﻟﺸﺮط ﻏﯿﺮ ﺻﺤﯿﺢ )ﺧﺎﻃﺌﺎ،ﻋﺪاد اﻟﺤﻠﻘﺔ
-:(4.3.1)ﻣﺜﺎل
Program اﻟﺘﻨﻔﯿﺬ
/*countdown using a for loop */ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
#include <stdio.h>
int main ( )
{
int k ;
for (k=10; k>0; k--)
printf("%d,",k);
printf( "FIRE!") ;
}
. 10 اﻟﻰ1 ( ﻟﻸﻋﺪاد ﻣﻦfactorial) أﻛﺘﺐ ﺑﺮﻧﺎﻣﺠﺎً ﻟﺤﺴﺎب وﻃﺒﺎﻋﺔ اﻟﻤﻀﺮوب-:(4.3.2)ﻣﺜﺎل
Program اﻟﺘﻨﻔﯿﺬ
#include <stdio.h> 1!= 1
main ( ) 2!= 2
{ 3!= 6
long factorial ; 4!= 24
int k ,a ; 5!= 120
for (a=1;a<=10;a++) 6!= 720
{ 7!= 5040
factorial=1 ; 8!= 40320
for(k=1;k<=a;k++) 9!= 362880
factorial*=k ; 10!= 3628800
printf("%d!=%d\n ",a ,factorial) ;
}
}
4.0,4.5,5.0,5.5,…,9.5,10 أﻛﺘ ﺐ ﺑﺮﻧﺎﻣﺠ ﺎً ﻟﺤﺴ ﺎب وﻃﺒﺎﻋ ﺔ ﻣﺠﻤ ﻮع اﻷﻋ ﺪاد-: (4.3.3)ﻣﺜ ﺎل
Program اﻟﺘﻨﻔﯿﺬ
#include <stdio.h> The Total = 87.00
main ( )
{
float a , sum=0.0 ;
for( a=4.0;a<=10;a+=0.5)
sum+=a ;
printf( " The Total = %f", sum ) ;
}
24 ﺻﻔﺤﺔ University of Baghdad-College of Science- Department of Mathematics Email : [email protected]
Introduction to C Programming Language ﻣﻘﺪﻣﺔ ﻓﻲ اﻟﺒﺮﻣﺠﺔ ﺑﻠﻐﺔ C
ﻣﺜﺎل) -:(4.3.4اﻟﻤﻄﻠﻮب ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺠﺎً ﻟﺤﺴﺎب وﻃﺒﺎﻋﺔ اﻟﻤﻌﺪل ﻟﻤﺠﻤﻮﻋﺔ ﻣﻦ اﻻﻋﺪاد اﻟﺤﻘﯿﻘﯿﺔ واﻟﺘﻲ ﻋﺪدھﺎ . n
Program اﻟﺘﻨﻔﯿﺬ
>#include <stdio.h Enter The Size of The List : 7
) ( main The Data : 12.5 -10.04 35 1.432 7.7 101.1 34.567
{ The Average of all Numbers = 26.037
; int n, counter
; float number, sum, average
; sum=0.0
; ) " printf( "Enter The Size of The List :
; )scanf("%d",&number
; )" printf("\nThe Data :
)for( counter=0;counter<n;counter++
{
; )scanf("%",&number
; sum+=number
}
; average=sum/n
; ) printf( "\n The Average of all Numbers =%f ",average
}
) (4.4اﻟﺠﻤﻞ اﻟﺘﻔﺮﻋﯿﺔ )(Branching Statement
ﻓﻲ ﺑﻌﺾ اﻟﺒﺮأﻣﺞ ﯾﺘﻌﯿﻦ ﻋﻠﯿﻨﺎ ﺗﺤﻮﯾﻞ اﻟﻤﺴﺎر اﻟﺘﺘﺎﺑﻌﻲ ﻻواﻣﺮ اﻟﺒﺮﻧ ﺎﻣﺞ اﻟ ﻰ اﻟﺨ ﺮوج ﻣ ﻦ اﻟﺤﻠﻘ ﺎت اﻟﺘﻜﺮارﯾ ﺔ أو اﻟﺮﺟ ﻮع اﻟﯿﮭ ﺎ أو اﻟﺨ ﺮوج ﻧﮭﺎﺋﯿ ﺎً
ﻣﻦ اﻟﺒﺮﻧﺎﻣﺞ ،اﻟﺠﻤﻞ اﻟﺘﻔﺮﻋﯿﺔ ﺗﺴﺎﻋﺪﻧﺎ ﻓﻲ ھﺬه اﻟﻌﻤﻠﯿﺔ ،وﻟﻜﻦ ﯾﺠﺐ أن ﯾﻜﻮن أﺳﺘﺨﺪام ھﺬه اﻟﺠﻤﻞ ﻣﺤ ﺪود،ﻻﻧ ﮫ اﺣﯿﺎﻧ ﺎ وﻓ ﻲ اﻟﺒ ﺮأﻣﺞ اﻟﻜﺒﯿ ﺮة ﺗﺴ ﺒﺐ ﻟﻨ ﺎ
ﻣﺸﺎﻛﻞ أﺛﻨﺎء اﻟﺘﻨﻔﯿﺬ .ﻣﻦ ھﺬه اﻟﺠﻤﻞ ھﻲ :
) (1ﺟﻤﻠﺔ أﻗﻄﻊ : Breakﺗﺴﺘﺨﺪم ھﺬه اﻟﺠﻤﻠﺔ ﻟﻠﺨﺮوج ﻣﻦ اﻟﺤﻠﻘﺎت اﻟﺘﻜﺮارﯾﺔ ،ﺣﯿﺚ ﯾﺘﻢ أﻧﮭﺎء اﻟﺘﻜﺮار ﻣﺘﻰ ﻣﺎ وﺻ ﻞ اﻟﺘﻨﻔﯿ ﺬ اﻟ ﻰ ھ ﺬه اﻟﺠﻤﻠ ﺔ.
ﻧﺘﺬﻛﺮ ﺑﺄن ھﺬه اﻟﺠﻤﻠﺔ اﺳﺘﺨﺪﻣﺖ ﻟﻨﻔﺲ اﻟﻤﻌﻨﻰ ﻓﻲ ﻣﻮﺿﻮع اﻟﺘﺮﻛﯿﺐ اﻻﻧﺘﻘﺎﺋﻲ . switch
ﻣﺜﺎل)-: (4.4.1
Program اﻟﺘﻨﻔﯿﺬ
/*break loop example*/ !10, 9, 8, 7, 6, 5, 4, 3, countdown aborted
>#include <stdio.h
) ( main
{
; int n
)for (n=10; n>0; n--
{
; )printf("%d ," ,n
)if (n==3
{
; )"!printf("countdown aborted
; break
}
}
}
ﺗﻢ اﻟﺘﻔﯿﺬ ﺑﻄﺒﺎﻋﺔ اﻻﻋﺪاد ﻣﻦ 10وﻟﻐﺎﯾﺔ 3ﺛﻢ ﻃﺒﻌ ﺖ اﻟﻌﺒ ﺎرة ! countdown abortedﺑﻌ ﺪھﺎ ﺗ ﻢ اﻟﺘﻮﻗ ﻒ واﻟﺨ ﺮوج ﻣ ﻦ اﻟﺤﻠﻘ ﺔ اﻟﺘﻜﺮارﯾ ﺔ وذﻟ ﻚ ﻟﻜ ﻮن
اﻟﺸﺮط ﻗﺪ ﺗﺤﻘﻖ ﻓﻲ اﻟﺘﺮﻛﯿﺐ اﻟﺸﺮﻃﻲ اﻟﺒﺴﯿﻂ . if
ﺻﻔﺤﺔ 25 University of Baghdad-College of Science- Department of Mathematics Email : [email protected]
Introduction to C Programming Language ﻣﻘﺪﻣﺔ ﻓﻲ اﻟﺒﺮﻣﺠﺔ ﺑﻠﻐﺔ C
) (2ﺟﻤﻠﺔ اﻻﺳ ﺘﻤﺮار : Continueﺗﺴ ﺘﺨﺪم ھ ﺬه اﻟﺠﻤﻠ ﺔ ﺑﺎﻻﺳ ﺘﻤﺮار ﻓ ﻲ ﺗﻮﺟﯿ ﮫ اﻟ ﺘﺤﻜﻢ اﻟ ﻰ ﻧﮭﺎﯾ ﺔ اﻟﺤﻠﻘ ﺔ وﺑﺎﻟﺘ ﺎﻟﻲ اﻟﺮﺟ ﻮع اﻟ ﻰ ﺑﺪاﯾ ﺔ اﻟﺤﻠﻘ ﺔ
وأﻛﻤﺎل ﺗﻨﻔﯿﺬھﺎ ﺣﺘﻰ اﻟﻨﮭﺎﯾﮫ .
ﻣﺜﺎل)-: (4.4.2
Program اﻟﺘﻨﻔﯿﺬ
/* continue loop example*/ !10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE
>#include <stdio.h
) ( main
{
; int n
)for (n=10; n>0; n--
{
)if (n==5
; continue
; )printf("%d ," ,n
}
; )"!printf( "FIRE
}
) (3ﺟﻤﻠﺔ ﺧﺮوج : Exitوﺗﻌﻨﻲ اﻟﺨﺮوج ﻧﮭﺎﺋﯿ ﺎً ﻣ ﻦ اﻟﺒﺮﻧ ﺎﻣﺞ وﺗﺮﺟ ﻊ ﺑﺎﻟﻘﯿﻤ ﺔ ﺻ ﻔﺮاً اذا ﻛ ﺎن اﻟﺒﺮﻧ ﺎﻣﺞ ﻧﻔ ﺬ ﻋﻠ ﻰ أﺣﺴ ﻦ ﻣ ﺎ ﯾ ﺮام ،وﺑ ﺄي ﻗﯿﻤ ﺔ ﻻ
ﺗﺴﺎوي ﺻﻔﺮاً اذا ﻛﺎن ھﻨﺎك ﺧﻄﺄ.
ﻣﺜﺎل)-: (4.4.3
Program اﻟﺘﻨﻔﯿﺬ
>#include <stdio.h
) ( main Enter value : 9
{ Enter value : 4
;int i, number, sum=0 Enter value : 6
)for (i=1;i<10;i++ Enter value : 8
{ Enter value : 1
; ) "printf("\nEnter value : Enter value : 12
; )scanf("%d",&number Enter value : -7
)if (number< 0 This is negative
{
; )" printf("This is negative
;) (exit
}
; sum+=number
}
; )printf("The sum of positive numbers %d", sum
}
ﻋﻨﺪﻣﺎ ﯾﻜﻮن اﻟﺸﺮط اﻟﻤﻮﺟﻮد ﻓﻲ اﻟﺘﺮﻛﯿﺐ اﻟﺸﺮﻃﻲ اﻟﺒﺴﯿﻂ ifﺻﺤﯿﺤﺎً ﺗﻄﺒﻊ اﻟﺮﺳﺎﻟﺔ اﻟﻤﻄﻠﻮﺑﺔ،ﺛﻢ ﺗﻨﻔﺬ ﺟﻤﻠ ﺔ اﻟﺨ ﺮوج )( exitاﻟﺘ ﻲ ﺗﻌﻨ ﻲ اﻟﺨ ﺮوج
ﻟﯿﺲ ﻓﻘﻂ ﻣﻦ اﻟﺤﻠﻘﺔ forﺑﻞ اﻟﺨﺮوج ﻧﮭﺎﺋﯿﺎً ﻣﻦ اﻟﺒﺮﻧﺎﻣﺞ وﺑﺎﻟﺘﺎﻟﻲ ﻟﻦ ﺗﻨﻔﺬ اﻟﺠﻤﻠﺔ اﻟﻤﻮاﻟﯿ ﺔ ﻟﮭ ﺬه اﻟﺤﻠﻘ ﺔ ،وﻋﻠﯿ ﺔ ﻟ ﻦ ﯾﻄﺒ ﻊ اﻟﺒﺮﻧ ﺎﻣﺞ ﻣﺠﻤ ﻮع اﻻﻋ ﺪاد
اﻟﻤﻮﺟﺒﺔ اﻟﻤﺪﺧﻠﺔ ،وھﺬا ﻣﺎ ﻧﺸﺎھﺪة ﻓﻲ اﻟﺘﻨﻔﯿﺬ اﻋﻼه .
) (4ﺟﻤﻠﺔ أذھﺐ اﻟﻰ : gotoﺗﺴﺘﺨﺪم ھﺬه اﻟﺠﻤﻠﺔ ﺑﺘﺤﻮﯾﻞ اﻟﻤﺴﺎر اﻟﺘﺘﺎﺑﻌﻲ ﻻواﻣﺮ اﻟﺒﺮﻧﺎﻣﺞ اﻟﻰ ﺟﻤﻠﺔ ﻣﻌﯿﻨﺔ.
; goto lable اﻟﺼﯿﻐﺔ اﻟﻌﺎﻣﺔ
ﺣﯿﺚ labelاﺳﻢ اﻟﻌﻨﻮان وﯾﻨﻄﺒﻖ ﻋﻠﯿﮫ ﻧﻔﺲ ﺷﺮوط اﺳﻢ اﻟﻤﺘﻐﯿﺮ ﺣﯿﺚ ﯾﻤﻜﻦ وﺿﻌﮫ ﻓﻲ أي ﻣﻜﺎن ﻣﻦ اﻟﺒﺮﻧﺎﻣﺞ .
ﺻﻔﺤﺔ 26 University of Baghdad-College of Science- Department of Mathematics Email : [email protected]
Introduction to C Programming Language ﻣﻘﺪﻣﺔ ﻓﻲ اﻟﺒﺮﻣﺠﺔ ﺑﻠﻐﺔ C
ﻣﺜﺎل)-: (4.4.4
Program اﻟﺘﻨﻔﯿﺬ
/*goto loop example*/ !10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE
>#include <stdio.h
) ( main
{
; int n=10
last :
; )printf("%d ," ,n
;n--
)if (n>0
; goto last
; ) "printf( "FIRE!\n
}
ﻧﻼﺣﻆ أن اﻟﺸﺮط ﻛﻠﻤﺎ ﻛﺎن ﺻﺤﯿﺤﺎً ﻓﺄن اﻟﻌﺒﺎرة gotoﻣﺘﺤﻘﻘﺔ وﺑﺎﻟﺘﺎﻟﻲ ﻓﺎن اﻟﺘﻔﯿﺬ ﯾﻨﺘﻘﻞ اﻟﻰ اﻟﻌﻨﻮان last :اﻟ ﻰ أن ﯾﺼ ﺒﺢ اﻟﺸ ﺮط ﻏﯿ ﺮ ﺻ ﺤﯿﺢ
ﻋﻨﺪﺋﺬ ﯾﻨﺘﻘﻞ اﻟﺘﻨﻔﯿﺬ اﻟﻰ ﻃﺒﺎﻋﺔ اﻟﻌﺒﺎرة !. FIRE
ﺗﻤﺎرﯾﻦ ) اﻟﻔﺼﻞ اﻟﺮاﺑﻊ(
ﺗﻤﺮﯾﻦ : 1أﻛﺘﺐ ﺑﺮﻧﺎﻣﺠﺎً ﻟﺤﺴﺎب وﻃﺒﺎﻋﺔ ﺣﺎﺻﻞ ﺟﻤﻊ ﻣﺮﺑﻌﺎت اﻻﻋﺪاد اﻟﺼﺤﯿﺤﺔ اﻟﻔﺮدﯾﺔ اﻟﻮاﻗﻌﺔ ﺑﯿﻦ ﻋﺪدﯾﻦ ﯾﺘﻢ ادﺧﺎﻟﮭﻤﺎ ﻋﻦ ﻃﺮﯾﻖ ﻟﻮﺣﮫ
اﻟﻤﻔﺎﺗﯿﺢ .
ﺗﻤﺮﯾﻦ : 2اﻟﻤﻄﻠﻮب ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻻﯾﺠﺎد ﺟﻤﯿﻊ اﻻﻋﺪاد اﻻوﻟﯿﺔ اﻟﻮاﻗﻌﺔ ﺑﯿﻦ 1و . 100
ﺗﻤﺮﯾﻦ : 3أﻛﺘﺐ ﺑﺮﻧﺎﻣﺠﺎً ﻟﺤﺴﺎب وﻃﺒﺎﻋﺔ ﻣﺠﻤﻮع اﻟﻤﺘﺴﻠﺴﻠﺔ اﻟﺘﺎﻟﯿﺔ :
ﺗﻤﺮﯾﻦ : 4ﻓﺼﻞ دراﺳﻲ ﺑﮫ ﻋﺪد ﻏﯿﺮ ﻣﻌﺮوف ﻣﻦ اﻟﻄﻼب ،أﻛﺘﺐ ﺑﺮﻧﺎﻣﺠﺎً ﻟﻘﺮاءة رﻗﻢ اﻟﻄﺎﻟﺐ IDوﺟﻨﺴﺔ SEXودرﺟﺎﺗﮫ ﻓﻲ ﺛﻼث اﻣﺘﺤﺎﻧﺎت T1
، , T2 , T3أوﻗﻒ ﺗﻨﻔﯿﺬ اﻟﺒﺮﻧﺎﻣﺞ ﻋﻨﺪ ادﺧﺎل رﻗﻢ اﻟﻄﺎﻟﺐ ﻗﯿﻤﺔ ﺳﺎﻟﺒﺔ ،اﻟﻤﻄﻠﻮب :
ﺣﺴﺎب وﻃﺒﺎﻋﺔ ﻣﻌﺪل اﻟﺪرﺟﺎت ﻟﻜﻞ ﻃﺎﻟﺐ . .a
ﺣﺴﺎب وﻃﺒﺎﻋﺔ ﻋﺪد اﻟﻄﺎﻟﺒﺎت ﻓﻲ اﻟﺼﻒ . .b
أﯾﺠﺎد وﻃﺒﺎﻋﺔ أﻛﺒﺮ ﻣﻌﺪل ﻓﻲ اﻟﺼﻒ . .c
ﻃﺒﺎﻋﺔ رﻗﻢ اﻟﻄﺎﻟﺐ وﻣﻌﺪﻟﮫ ﻣﻊ اﻟﻨﺘﯿﺠﺔ اﻟﻤﻘﺎﺑﻠﺔ ﻟﻤﻌﺪﻟﮫ واﻟﺘﻲ ھﻲ ﻋﻠﻰ اﻟﻨﺤﻮ اﻻﺗﻲ : .d
اﻟﻨﺘﯿﺠﺔ ﺣﺪود اﻟﻤﻌﺪل
Fail أﺻﻐﺮ ﻣﻦ 50
Admit table أﻛﺒﺮ ﻣﻦ أو ﯾﺴﺎوي 50وأﺻﻐﺮ ﻣﻦ 60
Medial أﻛﺒﺮ ﻣﻦ أو ﯾﺴﺎوي 60وأﺻﻐﺮ ﻣﻦ 70
Good أﻛﺒﺮ ﻣﻦ أو ﯾﺴﺎوي 70وأﺻﻐﺮ ﻣﻦ 80
Very Good أﻛﺒﺮ ﻣﻦ أو ﯾﺴﺎوي 80وأﺻﻐﺮ ﻣﻦ 90
Excellence أﻛﺒﺮ ﻣﻦ أ وﯾﺴﺎوي 90وأﺻﻐﺮ ﻣﻦ 100
ﺻﻔﺤﺔ 27 University of Baghdad-College of Science- Department of Mathematics Email : [email protected]