Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional statements (2)

Similar presentations


Presentation on theme: "Conditional statements (2)"— Presentation transcript:

1 Conditional statements (2)
IF mark>=50 THEN writeln(‘pass’) ELSE writeln(‘work hard’);

2 ????? Consider the following flowchart: Get mark N Y mark>=50?
Display ‘pass’ Display ‘fail’ Display ‘Bye Bye’ END

3 Get mark N Y mark>=50? Display ‘pass’ Display ‘fail’ Display ‘Bye Bye’ END

4 readln(mark) writleln(‘fail’) writeln(‘pass’) writeln(‘Bye Bye’);
END

5 write(‘Please enter your mark:’);
readln(mark); if (name >= 50) then writeln(‘pass’) else writeln(‘fail’); writeln(‘Bye Bye!’);

6 program pass; var mark:integer; begin write(‘Please enter your mark:’); readln(mark); if (name >= 50) then writeln(‘pass’) else writeln(‘fail’); writeln(‘Bye Bye!’); end.

7 (Alternative) program pass; var mark:integer; begin
write(‘Please enter your mark:’); readln(mark); if (name >= 50) then writeln(‘pass’); if (name <50) then writeln(‘fail’); writeln(‘Bye Bye!’); end.

8 Syntax Example if <<Boolean expression>> then
<<statement>> else <<statement>>; Example if (sex=‘F’) or (sex=‘M’) then number:=number + 1 else writeln(‘invalid input’);

9 Exercise 1 Please enter a number:19 It is not divisible by 4.
Write a program to get a number from the user and tell the user whether it is divisible by 4. Make use of if…then…else statement. Sample output: Please enter a number:19 It is not divisible by 4. Please enter a number:36 It is divisible by 4.

10 Answer program divisible; var number:integer; begin
write(‘Please enter a number:’); readln(number); if (number mod 4)=0 then writeln(‘It is divisible by 4’) else writeln(‘It is not divisible by 4’); end.

11 Exercise 2 Please enter your mark:85 Your grade is B
Write a program to get a mark from the user and display the corresponding grade according table: => A 81-90 => B 66-80 => C 51-65 => D 0-50 => F Make use of if…then…else statement. Sample output: Please enter your mark:85 Your grade is B

12 Answer program divisible; var mark:integer; grade:char; begin
write(‘Please enter your mark:’); readln(mark); if (mark<=100) then if (mark>=91) then grade:=‘A’ else if (mark>=81) then grade:=‘B’ else if (mark>=66) then grade:=‘C’ else if (mark>=51)then grade:=‘D’ else if (mark>=0)then grade:=‘F’; writeln(‘Your grade is ‘,grade); end.


Download ppt "Conditional statements (2)"

Similar presentations


Ads by Google