Programming Structures: Conditional/Repeat

Published  . 0 views
↓ Download
Programming Structures: Conditional/Repeat
1 / 1
Programming Structures: Conditional/Repeat - slide 1 of 10 Programming Structures: Conditional/Repeat - slide 2 of 10 Programming Structures: Conditional/Repeat - slide 3 of 10 Programming Structures: Conditional/Repeat - slide 4 of 10 Programming Structures: Conditional/Repeat - slide 5 of 10 Programming Structures: Conditional/Repeat - slide 6 of 10 Programming Structures: Conditional/Repeat - slide 7 of 10 Programming Structures: Conditional/Repeat - slide 8 of 10 Programming Structures: Conditional/Repeat - slide 9 of 10 Programming Structures: Conditional/Repeat - slide 10 of 10
Description: Programming Structures: ConditionalRepeat Statements Programming Constructs if .. else while loop for loop C code to Pseudo-C C allows goto as means of transferring control Closer to machine-level programming style Generally considered

Related Topics

Download Presentation

"Programming Structures: Conditional/Repeat" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentation Transcript

slide1. Programming Structures:
Conditional/Repeat Statements Programming Constructs
if .. else
while loop
for loop<br>
slide2. C code to Pseudo-C C allows “goto” as means of transferring control
Closer to machine-level programming style

Generally considered bad coding style

But, essential for assembly programming
More explicit control
Directly mapped to assembly instructions<br>
slide3. if (t)
then-statement;
else
else-statement; Summary 1 while (Test)
Body loop:
if (!Test) goto done;
Body
goto loop
done: if - else while if (!t) goto false;
then-statement;
goto done;
false:
else-statement;
done: Pseudo-C<br>
slide4. Summary 2 Init;
loop: if (!Test) goto done;
Body
Update ;
goto loop;
done: for (Init; Test; Update )
Body Init;
while (Test ) {
Body
Update ;
} for loop

equivalent to:<br>
slide5. if (t)
then-statement;
else
else-statement; //t = test_expr;
if (!t) goto else;
then-statement;
goto done;
else:
else-statement;
done: int goto_max(int x, int y){
int rval;
if (x <= y)goto else;
rval = x;
goto done
else:
rval = y;
done:
return rval;
} int max(int x, int y){
if (x > y)
return x;
else
return y;
} General if-else translation<br>
slide6. int goto_max(int x, int y){
int rval = y;
if (x <= y)goto done;
rval = x;
done:
return rval;
} int max(int x, int y){
int rval = y;
if (x > y)
rval = x;
} General if-else translation int max(int x, int y){
if (x > y)
return x;
else
return y;
}<br>
slide7. C Code int fact_while(int x){
int result = 1;
while (x > 1) {
result *= x;
x = x-1;
};
return result;
} Pseudo-C Version int fact_while_goto(int x){
int result = 1;
loop:
if (!(x > 1)) goto done;
result *= x;
x = x-1;
goto loop;
done:
return result;
} “While” Loop while (Test)
Body loop:
if (!Test) goto done;
Body
goto loop
done:<br>
slide8. “For” “While” for (Init; Test; Update )
Body Init;
while (Test ) {
Body
Update ;
} Init;
loop: if (!Test) goto done;
Body
Update ;
goto loop;
done: While Version For Version<br>
slide9. int result;
for (result = 1;
p != 0;
p = p>>1) {
if (p & 0x1)
result *= x;
x = x*x;
} result = 1;
loop: if (p == 0) goto done;
if (p & 0x1)
result *= x;
x = x*x;
p = p >> 1;
goto loop;
done: for (Init; Test; Update )
Body Init;
loop: if (!Test) goto done;
Body
Update ;
goto loop;
done:<br>
slide10. C Code int fact_do(int x)
{
int result = 1;
do {
result *= x;
x = x-1;
} while (x > 1);
return result;
} Pseudo-C Version int fact_goto(int x)
{
int result = 1;
loop:
result *= x;
x = x-1;
if (x > 1)
goto loop;
return result;
} “Do” Loop Use backward branch to continue looping
Only take branch when “while” condition holds do
Body
while (Test); loop:
Body
if (Test)
goto loop<br>