1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=20, Y=10 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=20, Y=10 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=10, Y=10 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=10, Y=10 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=10, Y=10 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=10, Y=10 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=10, Y=30 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=10, Y=30 |
![]() |
1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Input: X=10, Y=30
Note: We just don't care about the non-executable |
![]() |
1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Input: X=20, Y=30 |
![]() |
1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Test Case 1: X=20, Y=10 |
![]() |
Short circuit boolean operators are ones which do not necessarily test each of their operands. For instance, given the following statement:
if (A && B) then
the truth value of B will only be checked if the truth value of A is true. If A is false, there is no need to check the truth value of B since the entire expression is guaranteed to be false. Likewise given the following statement:
if (A || B) then
the truth value of B will only be checked if the truth value of A is false. If A is true, there is no need to check the truth value of B since the entire expression is guaranteed to be true.
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=20, Y=10 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=20, Y=10 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=10, Y=10 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=10, Y=10 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=10, Y=10 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=10, Y=10 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=10, Y=30 |
![]() |
1 function P return INTEGER
2 begin
3 X, Y: INTEGER;
4 READ(X); READ(Y);
5 while (X > 10) loop
6 X := X - 10;
7 exit when X = 10;
8 end loop;
9 if (Y < 20 and then X mod 2 = 0) then
10 Y := Y + 20;
11 else
12 Y := Y - 20;
13 end if;
14 return 2 * X + Y;
15 end P;
Input: X=10, Y=30 |
![]() |
1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Input: X=10, Y=30
Note: We still cover statements like we did before, |
![]() |
1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Input: X=15, Y=30 |
![]() |
1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Test Case 1: X=20, Y=10
Note: We missed one edge but since it has to do |
![]() |
1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Input: X=20, Y=10 |
![]() |
1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Input: X=5, Y=30 |
![]() |
1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Input: X=21, Y=10 |
![]() |
1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Test Case 1: X=20, Y=10 |
![]() |
So, “if (x < y)” should be treated as
if (x < y) then … else if (x == y) then … else if (x > y) then … end
How many paths does the following program fragment have?
a << cin; b = 0; while (a > 0) { a--; b++; } if (b > 5) { printf("b > 5"); } else { printf("b <= 5"); }