Você está na página 1de 10

Lesson:

The Structured Commands

Subject: Turbol Pascal Programming Language Teacher s name: Tran Thi Yen - Alisa

6/24/2011

The Structured Commands Include:




Branch Constructs:
 

The If Statement, The Case-Of Statement. The For, The While-do, The Repeat-Until.

Loop Constructs:
  

Now, it is time to learn about The If Statement. This programming construct is common in every programming language.

6/24/2011

1. Syntax and process of the If Statement




In Pascal, the 'if statement' should be written as follows:




Syntax 1:
If <logical expression> then <command>;

Syntax 2:
If <logical expression> then <command 1> Else <command 2>;

In which have elements as follows:




If, then, and else are keywords. Logical expression can get one of two values true or false. command is an action to control operation of computer.
3

6/24/2011

1. Syntax and process of the If Statement




Process:


With syntax 1: The first, computer will check value of logical expression . If logical expression is true then the command after then is executed else this command is skipped. With syntax 2: The first, computer will check value of logical expression . If logical expression is true then the command after then is executed else the command after else is executed.
4

6/24/2011

1. Syntax and process of the If Statement


* Notes:
 

Don t distinguish upper case and lower case letters in Pascal If more than one action is required after then or after else , this is called command block and it is put between begin and end . After any command in Pascal is always with a semicolon (;). But, the command before else keyword is without a semicolon (;). In case of command block, after end keyword which before else keyword is without a semicolon. Nested If statements: an if statement is nested in another as follow: If <logical expression 1> then If <logical expression 2> then else <command> else <command> etc
5

etc

6/24/2011

2. Examples


Example 1:
Write a program to input 2 integers a and b from the keyboard. Show the larger number?


Algorithm:


Compare number a and number b . If number a is larger than b then show it, else show number b .

6/24/2011

2. Examples

 Example 1:
Program Example_1; Uses crt; Var a, b: integer; Begin

Program

Write( Input a: ); Readln(a); Write( Input b: ); Readln(b); If a > b then Write( The larger number is: , a) { here without semicolon ; } Else Write( The larger number is: , b); Readln; End.
6/24/2011 7

2. Examples


Example 2:

Write a program to solve quadratic equation

with three arguments inputed from the keyboard? What is the quadratic equation?


ax2 + bx + c = 0 (a

0)

Algorithm:
 

( = b2

4ac

Check value of (:
 

If ( < 0 the equation have no root If ( = 0 the equation have double roots which are: x1 = x2 = -b/2a If ( > 0 the equation have 2 different roots:
x1 ! b ( 2a

x2 !

b  ( 2a
8

6/24/2011

2. Examples


Example 2: Program
Program Example_2; Uses crt; Var a, b, c: integer; x1, x2, delta: real; Begin Write( Input a: ); Readln(a); Write( Input b: ); Readln(b); Write( Input c: ); Readln(c); If a = 0 then write( This is not quadratic equation ) Else Begin delta := b*b 4*a*c; {delta less than 0} If delta < 0 then write( This equation doesn t have any root ) Else {delta equal 0} If delta = 0 then write( Double roots x1 = x2 = , -delta/(2*a)) Else {delta more than 0} write( x1 = , (-b+sprt(delta))/(2*a), x2 = ,(-b-sprt(delta))/(2*a)); End. Readln;

6/24/2011

End.

Assigments


Write 2 above programs of 2 examples in your computer and check results Write a program input 3 integers a , b , and c from the keyboard. Show the largest number?

6/24/2011

10

Você também pode gostar