Вы находитесь на странице: 1из 55

7.

Loops & Control


Constructs

c
Loops
|  
    
  
  
 
  
  

 
!"#!$%&'  
 (   '&
)*&'+    '&
!$"

` 
   
  
   !
Loops (Contd.)


!
,%
!,$%
  *%
-
 
  $" 

 !./ #

%
-
0


  $" 

 !11
` 
   
  
   "
Kor statement
 $%2%*
  %-
3 45 #    
  3    *
$#2 *  
$6    7
   
7
*6      
   5  
 

` 
   
  
   #
Kor statement (Contd.)
† 5      
   
      
  
8      
 
  9

26   
  
 5     

:      7  
 !"%1%,,
 . #%
` 
   
  
   $
ù nd ng sum oK K rst 10
natural numbers
  

 
!"#!"%&'   ( 
  '&
 !$%!$"%,,&'   '&

!
,%
-
 
  $" 


 !./ #
%
-
0

  $" 

 !11

` 
   
  
   %
 
; 5
4  
$""
  $"""
 <

` 
   
  
   &
†=      

$ *     


  
2    †
  5 5   
> 7#7        #
     
  
?   #
 %2%
$-

` 
   
  
   '
?  7 

  

 !2%
 %1"%,!2&'   '&

 .  7 


/ #%
-
-

` 
   
  
   (
)5 =  
    
3 7   5   
   7 
)     
 
;7 5     
  # 
        

` 
   
  
   c)
)5 =   = 
 |@;$""
 ABB0$1"
 )†B$
  

 
%
 
!|@;%
ABB0%
,!)†B
&'   '&


.*!!"
 . 7 57 5*/ #
%
-
-
` 
   
  
   cc
3  | 

 !$%!$"%,,

 C!$%C!1%C,,

 4!$%4!2%4,,

$
-
-
-
` 
   
  
   c!
 
± )
    

$%
2

 
*%
--

` 
   
  
   c"
È   D
 †
  
 ## 
%
!$%
!"%
 
!"%
  5
  %
 . #EF
!$"
 
!'%
 .2G.!
.*/ ### 
%
,,%-
-

` 
   
  
   c#
07  

 
  
  
 
  #7 %
  
 
7 / %
 .
#E %
7 !"%
 "
7 !7 '$", .$"%
! &$"% -
 07 
!.
#7 %
-

` 
   
  
   c$
)

 

†0A ; ?:|)


 

)†:†D3†)
)†:†D3†)

` 
   
  
   c%
 H
@

)†:†D3†)

†0A

;
 

?:|)

` 
   
  
   c&
  H
 
  
 
  
 
 !"%

 /  
 +  
$ 9
/ %
!I:FEE!IJF
!,*2%

%
-
` 
   
  
   c'
  H
 
!!I$F
   )  %
 
 /  7 5%5 %
-
-K!I$F%
-

` 
   
  
   c(
| )
: 

5  ( #!"
  !

! 

LM!! 

!

 
!,$
! 6 $EE 


` 
   
  
   !)
)
*   
+5  4
* D  
  
* )5       7  
  

        


        


   -
=         


9


` 
   
  
   !c
 )
  
 %
!IF%
!!IF
 /   " N/ %
 . #E%

 " J / %
4%

` 
   
  
   !!
 ) 
 $ @ / %
4%
 2 † / %
4%
 * †/ %
4%
 O ?
/ %
4%
 1 ?7/ %
4%
 P )/ %
4%

` 
   
  
   !"
 ) 
 Q )7 / %
4%
 R  / %
4%
 N 3 / %
4%

 8 / % -
  IF5
   
/ %
   5   / %
! %
K!IF
   )  %--
` 
   
  
   !#
)
%


 IF $%
4%
 IF 2%
4%
-

` 
   
  
   !$
);8†=> 
  %
 

 I,F $%
4%
 I&F 2%
4%
-

` 
   
  
   !%
Å4#= 
#0

* Å4
   4  
          
 
* = 

 
     
* 0
 5 5  
  
 %

  
+ "     / %
 . #E%
2""""
 5
  7
+  

 / % 4%-

` 
   
  
   !&
Å4#= 
#0
  
 

"
 5
 ./ #%
"
 5
   7

/ %
 
%
-
-
K!"%
      / %

` 
   
  
   !'
0
  
 
+ "     
**
S
 **
 
+ "    
+P"
S
   7

 
+ "    
O1
S
 O1
 
+ "    
21"""
S
  7
+  
  
    
'


` 
   
  
   !(
>. Arrays

")
:5

* A  5
* =    5
* :5    #5 

:! 2#O#P#R-
:L"M!2
:L$M!O
:L2M!P
:L*M!R

` 
   
  
   "c
Array Declarat on
 
T L$""M%
*    5 ( $""
  

5
* 8T L"M      $"""
T L M   $""", 'R (
7
L$""M 
 
8 (

T L1M! *"$#*$$#*1$#*P$#*Q$M%

T LM! *"$#H*Q$-%

` 
   
  
   "!
È #      
   
  $L$"M%
 %
  $"   / %
 !"%!N%,,
 . #E $LM%
-
 5
7 / %
 !"%!N%,,
 ./ # LM% --
` 
   
  
   ""
ù nd ng greatest number
and pos t on n a Array
  
 L1M! $#1#2#P#*-%
 !"## !"%
 !"%!O%,,
LM
 !LM%
 !,$% -
-
 

!.
  !./ # # %
-

` 
   
  
   "#
@tr ngs
* )   5 
*   21-%
*  . # %
* † 5        %

  %

    %
*    
     7      
   7#     > 7#

  

 5 7     
 5 
7    8    
 

  
7#    7   
7    
 
 8&@

* A    
  5

    
    

` 
   
  
   "$
!al ndrome
 ?:|)"
  
  !$%
  ## %
L1"M%&'
   
1"'&

     4 
   %
 %
!  +$%
 !"# ! %! &2%,,#++
 
LMK!L M
 !?:|)%4%- -
` 
   
  
   "%
!al ndrome
 

 %

     %
-
 
 .  3@†  %
-

` 
   
  
   "&
ßult D mens onal Arrays
* D
+  :5    5 
 

5
  
 #C%
 (L2ML2M%
 L2ML2M! $#2#*#O-%
 5L2ML2M! 1#P#Q#R-%
 !"%!$%,,
 C!"%C!$%C,,
(LMLCM!LMLCM'5LMLCM%
 (L.ML.M!./ ##C#(LMLCM%-
-
-

` 
   
  
   "'
Å nary @earch
B+  5 
:  Å 5)
 L M%
!"
 ! +$

! %
 
!, &2%
!!
!
%
 
!,$%
  !%-

 
 %
  3 
 %

` 
   
  
   "(
U. ùunct ons

#)
D 
B  = 
D:83

$ 2 * O

$$ $2 2$ *$

` 
   
  
   #c
)

  6 = 
   
± ; 5    
± =    
      #7 
 

± A        
± 7    

± 
     4
± 
 
7    
 
± )4  

± )4       

   

` 
   
  
   #!
)

  6 = 
   
± Å 55 4   79

± B
 5   
± :        
± 8   
    
 


  
± )      

   5 
 # 5  # 5 
   
± 8       
± 8     

 

` 
   
  
   #"
†B  ?


:
      
?
 
?
  
?
 =

` 
   
  
   ##
ùunct on Declarat on


 6 5
  
 
 $#
 2#H%

 $  $#  2%
7 
2  $#  2%&'7 
   '&

*  $#  2%

O%

: 
   5

` 
   
  
   #$
?
 =
* ?
  5  5
*  ?
$21%

! $21%
* ?
 5  


  
* ?
 
   5

   
* 0
 5   

` 
   
  
   #%
?
  

* =   5


* =    2 6
* ?
   
* =      #   
* ?
  5
* =     7 E
 

` 
   
  
   #&
:  
 
 

)$  
 
)2 2  
)* E 27

)O 

)1 B 7

?
 :
)$ 7

)2 
)* 
 7
  


` 
   
  
   #'
B  
  
 !"#!"#
!"%
  # 5%&'
  '&
  2   / %
 .. #E#E%

!#%&'
 '&
 
 . .!. ###
%-
&'
   '&
8  # &'
  '&
 %
!,%

 %-

` 
   
  
   #(
?
 + 8  B  
* ?
    
 

* † 5    
 
      
  
   
  
* † 
  5 
 
 
 5#

  #
 5 
 
  
* ; 
  
   -#

      
  

  
` 
   
  
   $)
:?
  5
 ####
$#
2#
*%
  # %&'
  '&
  2 
 / %
 .. #E#E%

$!#%&'
 '&
  2  
 / %
 .. #E#E%

2!#%&'
 '&

*!
$#
2%
 
 . .!./ ###
$%
 
 . .!./ ###
2%
 
 . .!./ #
$#
2#
*% -
&'
   '&
  # &'
  '&
 %
!,%

 % -
` 
   
  
   $c
= 


)$ 

)2
  07 

)* 
5
52
)O
 7 57 5*:+ 
07 ?

)$7 !"
)2
"
7 !7 '$",
.$"

!
&$"
)*
 7 

` 
   
  
   $!
= 

 = 
:+  

)$
!"
)2
"

!
,
.$"

!
&$"
)*
 

| 4OO  $OP

` 
   
  
   $"
) 
  

)  7   
 #

    
| 4O1  $ON
†    
  5# 4
OP  $1$
† 7   # 4OQ 
 $12

` 
   
  
   $#
0
 7


 
%

 7$%

 
 %

 UJF%

 "%

 O",*"%

 %

` 
   
  
   $$