Referat Instructiunea Repeat

Mai jos puteti citi fragmente din Referat Instructiunea Repeat si de asemenea puteti face Download Referat Instructiunea Repeat

Citeste fragmente din Referat Instructiunea Repeat

INSTRUCŢIUNEA REPEAT Această instrucţiune reproduce structura repeat until şi are forma generală: REPEAT i1; i2; . . . in UNTIL expresie logică Aici , i1,…,in reprezintă instrucţiuni. Principiul de executare este următorul: Se execută secvenţa de instrucţiuni; Se evaluează expresia logică; Dacă aceasta ia valoarea FALSE se execută din nou secvenţa de instrucţiuni, contrar se trece mai departe. Iată cum arată instrucţiunea REPEAT prezentată cu ajutorul diagramei de sintaxă: INSTRUCŢIUNE INSTRUCŢIUNEA REPEAT Observaţie: secvenţa se execută cel puţin odată, după care se pune problema dacă să se repete sau nu (prin evaluarea expresiei logice). Regula instrucţiunii REPEAT Are forma: {P} A {Q}, (Q and not b) <= P {P} repeat A until b {Q and b} Formula P se numeşte specificaţie invariantă. Pentru simplitate, aplicarea acestor reguli în demonstraţii se va nota prin: repeat repeat {P} {P} A sau A {Q} {Q} until b until b {Q and b} Observaţie: din prezentarea neformalizată a semanticii, se ştie că instrucţiumea: repeat A until b este echivalentă cu instrucţiunea compusă: begin A; while not b do A End Se poate arăta acum, în termenii semanticii axiomantice, că regula instrucţiunii repeat poate fi dedusă din regula instrucţiunii while, regula consecinţelor şi regula instrucţiunii compuse. Într- adevăr, în ipostazele {P} A {Q} şi (Q and not b) <= P poate fi construită următoarea demonstraţie: {P} begin A; {Q} while not b do {Q} A end {Q and b}. Regula consecinţelor a fost aplicată pentru a demonstra că specificaţia Q este invariantă pentru instrucţiunea while: {Q and not b} {P} A {Q}. Exemplu. Notaţia: Program R1; Type natural =0..maxint; Var x, y, z, u : natural {(x = a) and (y = b) and (a >= 0) and (b >= 0)} {(x * y = x * y) and (x > 0)} begin z := 0; {(z + x * y = x * y) and (x > 0)} u := x; repeat {(z + u * y = x * y) and (x > 0)} z := z + y; {(z + (u - 1) * y = x * y) and (u >= 1)} u := u - 1 {(z + u * y = x * y) and (u >= 0)} until u = 0 {(z + u * y = x * y) and (u >= 0) and (u = 0)} end. {{(z = a * b)} | ~ † ˆ   ® ^ ¼ f ţie a corectitudinii parţiale a unui algoritm de înmulţire a două numere naturale prin adunare repetată. Pentru instrucţiunea repeat…until u=0, specificaţia invariantă este (z+u*y=x*y) and (u>0). Exemplu. Notaţia: {Program R2} type natural = 0..maxinit; var x, y : natural; {(a > 0) and (b > 0) and (x = a) and (y = b)} begin repeat {(x > 0) and (b > 0) and (x > a) and (y > b)} while x > y do {(x > 0) and (y > 0) and ((a, b) = (x, y))} x := x - y; while y > x do {(x > 0) and (y > 0) and ((x, y) and (a, b))} y := y - x; {(0 < y <= x) and ((a, b) = (x, y))} until x = y {(0 < y <= x) and ((a, b) = (x, y)) and (x = y)} end. {x = (a, b)} este o demonstraţie a corectitudinii parţiale a unui algoritm de calcul al celui mai mare divizor comun (a, b) a două numere naturale. În demonstraţie s-au mai folosit următoarele specificaţii adevărate: (x > y) <= ((x, y) = (x - y, y)); (x, y) = (y, x); (x, x) = x. Exemplu. Următoarea notaţie: {program R3} type natural = 0..maxint; var x, s, t : natural; {(x = a) and (y = b) and (a >= 0) and (b >= 0)} begin s := 0; repeat {s = (a - x) * b} t := 0; repeat {(s = (a - x) * b + t) and (y = b)} s := s + 1; t := t + 1; until t = y; {(s = (a – x) * b + t) and (y = b) and (t = y)} {s = (a - x) * b + b) x := x - 1 {s = (a - x) * b} until x = 0 {( s =(a - x) * b) and (x = 0)} end. {x = a * b} este o demonstraţie a corectitudinii unui algoritm de înmulţire a două numere naturale folosind operaţiile succ şi pred. Exemplu. Se citeşte un număr natural n, mai mare sau egal cu 1. să se calculeze suma primelor n numere naturale. program sumă; var n, s, i: integer; begin write (‚n=’); readln (n); i := 1; s := 0; repeat s := s + i; i := i + 1 until i > n; writeln (‚s=’, s) end. Exemplu: se citeşte n, număr natural. Să se descompună în factori primi. program factp; var n, d, fm :integer; begin write (‚n=’); readln (n); d := 2; repeat fm := 0; {fm reprezintă multiplicitatea divizorului d} while n mod d = 0 do begin fm := fm + 1; n := n div d end; if fm <> 0 then writeln (d, ‚la puterea’, fm); d := d + 1 until n = 1 end. BIBLIOGRAFIE: Manual pentru clasa a IX-a de informatică. Editura L&S INFOMAT Limbajul Pascal. Concepte fundamentale. Volumul I ;editura TEHNICĂ Pagina PAGE 1 REPEAT ; UNTIL EXPRESIE LOGICĂ 쥁@