Lesson #3 and #4. Conditions

Theory

Lection # 3 in pdf format

IF Statements

  • If statements are concerned with Boolean logic. If the statement is true, the block of code associated with the if statement is executed. If the statement is false, control either jumps to the line after the if statement, or after the end keyword of an if statement block.
  • var numb:= 1;
    if numb = 1 then 
      begin
        // statements that will execute if the value of the numb variable is 1, 
        // will be placed here.
      end;
  • You can remove the begin and end keywords if your statement to execute is a single line statement. PascalAbc understands that if no begin and end are used, the line immediately after the if (condition) will be executed if the condition is true.
  • Else clauses
  • IF statements can also have associated else clauses. The else clause executes when the if statement is false:
  • var numb:= 1;
    if numb = 1 then
      begin
          // Block of code executes if the value of the numb variable is 1.
      end
    else
      begin
          // Block of code executes if the value of the numb variable is not 1.
      end
    Else if clauses (or Chained If statements)
  • If statements can also have associated else if clauses. The clauses are tested in the order that they appear in the code after the if statement. If any of the clauses returns true, the block of code associated with that statement is executed and control leaves the block of code associated with the entire if construct.
  • var numb:= 1;
    if numb = 1 then
      begin
          // Block of code executes if the value of the numb variable is 1.
      end
    else if numb = 0 then
      begin
        // Block of code executes if the value of the numb variable is 0.
      end
    else
      begin
        // Block of code executes if the value of the numb variable is neither above answers.
      end
  • The semicolon before else is not needed!
  • You can create as many else if blocks as necessary.
  • Sample:
    To do: A number of season (Winter is the first) is given. Output a name of the entered season number. It is better to use the chained If statements.

    ✍ Algorithm:

Logical (boolean) operations

    We can define variables of boolean type:

    var A,B: boolean;
    A := True;
    B := False;
    Print(A and B);
    Print(A or B);
    Print(not A);
  • A and B is True if A is True and B is True at the same time. In other cases A and B is False
  • A or B is False if A is False and B is False. In other cases A or B is True
  • not A has opposite value: not A is True if A is False
  • Logical (boolean) operations in if statements:
  • Conditions may consist of logic operations: not, or, and. If there are more than one condition, so each condition must be surrounded by round brackets.
  • For example if we must check two conditions:

    if (year < 20) or (year > 18) then
    begin
      // if body
    end
    If one of the conditions or both conditions are True then if body executes.
  • Or another example:
  • var a: = 5;
    if (not (a<4)) and (7>5) then // ← True
    begin
      // if body
    end
  • The statements write and print can also return True or False:
  • //...
    a = 5;
    write (a >= 5); // returns True
    // ...
The «most popular» errors:
  • You don’t need to write semicolon before else clause.
  • Don’t forget about begin..end in the cases when there is more than one lines of code after then or else.

Case statement

If there are too many else if statements, code can become difficult to understand. In this case, a better solution is to use a switch statement:

var numb:= 1;
case numb of  
1,2 : writeln ('1 or 2'); //  Block of code executes if the value of numb is 1 or 2.
 3: writeln('3'); //  Block of code executes if the value of numb is 2.
 4: writeln('4'); //  Block of code executes if the value of numb is 3.
 5: writeln('5'); //  Block of code executes if the value of numb is 5.
 else writeln('nothing matches'); //  Block of code executes if nothing matches.
end
  • A block labeled else will execute when none of the other blocks match.

Using String type

Case statement can check the variable of String type.

Lab 1 Example:
Given: the english words ‘dog’, ‘use’, ‘find’.
To do: Output translations of the words into russian.

✍ Algorithm:

Using ranges and enumerations

Lab 2 Example:
Given: Month ordinal.
To do: Output corresponding name of a season.

✍ Algorithm:

Labs and tasks

True or false?

{0.3 points} Task 1:
To do: Two integers are given. Check if the following statement is True: the first number is greater than the second (the program must return True if it is true, and False otherwise).

Expected output:

please enter two integers
>>> 23
>>> 1
23 is greater than 1 is True 

[Program name: L3task01.pas]

{0.3 points} Task 2:
To do: Two integers are entered. Check the truth of the statement: the first number is not equal to the second (the program must return True if it is true and False otherwise).

Expected output:

please enter two integers
>>>5 
>>>5
5 is not equal to 5 is False

[Program name: L3task02.pas]

{0.3 points} Task 3:
To do: Three integers are given: the values of variables A, B, C. Check the truth of the double inequality A < B < C. Make sure that your program is correct with at least two input data sets, give the log of the program in the form of a comment.

Expected output:

please enter three integers
>>>3 >>>6 >>>2
3 is less than 6 less than 2 is False
please enter three integers
>>>2 >>>5 >>>7
2 is less than 5 less than 7 is True

[Program name: L3task03.pas]

{0.6 point} Task 4:
To do: Three-digit integer is given. Check the truth: the first digit (left digit) of the number is less than the second (middle) and third (right).

Expected output:

please enter a three digit number
>>> 854
8 is less than 5 and 4 is : False

[Program name: L3task04.pas]

{0.6 point} Task 5:
To do: Two integers are entered. Check the truth of the statement: at least one of these numbers is odd.

Note. Use the odd standard function:

// The function odd returns True when its argument is odd integer:
print (odd(5)); // true 
print (odd(6)); // false .

Check the following:

-5,  8 => True
 12, 0 => False
 6, -1 => True
 11, 7 => True

Expected output:

please enter two integer numbers
>>> 9 >>> 2
Either 9 or 2 is an odd number. This is : True

[Program name: L3task05.pas]


If statement

{0.3 points} Task 6:
To do: An integer is entered. If this is positive number, you should add 1 to it. Output the result.

Check the following:

-3 => -3
 0 =>  0
 1 =>  2
 5 =>  6

Expected output:

please enter an integer number
>>> -77
The result is -77

[Program name: L3task06.pas]

{0.3 points} Task 7:
To do: An integer is given. If this is even number then multiply it by 10. Output the result.

Check the following:

2   => 20
1   =>  1
-10 => -100

Expected output:

please enter an integer number
>>> 8
The result is 80

[Program name: L3task07.pas]

{0.5 points} Task 8:
To do: An integer is given. If this is an even number, then multiply it by 3, if it is not even, then divide it by 3. Output.

Check the following:

4 => 12
9 =>  3
-10 => -30

Expected output:

please enter an integer number
>>> 12
The result is 36

[Program name: L3task08.pas]

{0.5 points} Task 9:
To do: An integer is given. If this is a positive number then add 1 to it; otherwise subtract it by 2. Output.

Check the following:

-3 => -5
 0 =>  1
 1 =>  2
 5 =>  6

Expected output:

please enter an integer number
>>> 48
The result is 49

[Program name: L3task09.pas]

Chained If statements and Logical operations in if statements

{0.5 points} Task 10:
To do: An integer is given (age of the person). If it is greater than or equal to 18, then output "you can watch this movie"; if the number is less than 10, then output "you should watch the cartoon". Otherwise, print "you can take a walk".

Expected output:

how old are you?
>>> 8
you should watch the cartoon
how old are you?
>>> 15
you can take a walk

[Program name: L3task10.pas]

{0.5 points} Task 11:
To do: The student received a grade. If it is 2 points, then the program should output "it's very bad"; if it is a 3 - program should print "it's bad"; if it is 4 - "it's good", in case of 5 - "it's excellent", otherwise - "there are no such marks".

Expected output:

what's your grade?
>>> 2
"it's very bad"
what's your grade?
>>> 4
"it's good"

[Program name: L3task11.pas]

{0.5 points} Task 12:
To do: The program must request the time of a day in hours (from 1 to 24). Depending on the time entered, display a message indicating what time of a day the entered hour belongs to (midnight (24), night (1-4), morning (5-11), day (12-16), evening (17-13)).

Expected output:

what's time of the day?
>>> 2
"night"
what's time of the day?
>>> 24
"midnight"

[Program name: L3task12.pas]

{0.5 points} Task 13:
To do: Two integer values are given (set them to variables A and B). Assign a sum of these values to each variable if their values are not equal; otherwise (if they are equal) assign zero to the variables. Output the new values of variables A and B.

Expected output:

please enter two integer numbers
 3 5
The result is A = 8, B = 8
please enter two integer numbers
4 4
The result is A = 0, B = 0 

[Program name: L3task13.pas]

{0.5 points} Task 14:
To do: An two-digit integer N is entered, |N| ∈ [10;99].
‒ If it is even and divided by 4, then "add" 4 digit from the left (that is, to form a new number, which in the category of hundreds has 4, and the digits of tens and units are left as in the original number);
‒ If it is even, but is not divided by 4, then "add" 2 digit from the left of the number;
‒ If it is odd, then "add" 0 digit from the right of the number. Print the resulting number.

Expected output:

N: 
>>> 16 
result =  416
N: 
>>> 86
result = 286
N:
>>> 31
result = 310 

[Program name: L3task14.pas]

{0.5 points} Task 15:
To do: Integer number x is entered. Calculate the value of the function f:

Note: The sign means boolean or.

Expected output:

Enter an integer: 
>>> 4
Result is 8

[Program name: L3task15.pas]

{0.5 points} Task 16:
To do: Real number x is entered. Calculate the value of the function f:

Expected output:

please, enter an x value: -2
result of 6-x =  8 
please, enter an x value: 5
result of 2sin(x) =  -1.91784854932628 

[Program name: L3task16.pas]

{0.5 points} Task 17:
To do: A three-digit integer a is entered. Print True if there is a permutation of digits that makes number which is divided by 10, and False otherwise (e.g. 602 => True [620, 260]).

Note. It is allowed to use no more than three operators div and mod (in total).

Expected output:

Enter three-digit integer:
>>> 602 
True 
Enter three-digit integer:
>>> 311 
False
Enter three-digit integer:
>>> -100
True

[Program name: L3task17.pas]


Switch statement

{0.3 points} Task 18:
To do: Perform the task using the Case statement. The student received a grade. If it is 2 points, then the program should output "it's very bad"; if it is a 3 - program should print "it's bad"; if it is 4 - "it's good", in case of 5 - "it's excellent", otherwise - "there are no such marks".

Expected output:

what's your grade?
>>> 2
"it's very bad"
what's your grade?
>>> 4
"it's good"

[Program name: L3task18.pas]

{0.3 points} Task 19:
To do: Perform the task using the Case statement. An integer in the range [1;7] is entered. Output a name of the day corresponding to this number (1 - “Monday”, 2 - “Tuesday”, etc.).

Expected output:

please enter an integer from 1-7, for day of the week
4
Thursday
please enter an integer from 1-7, for day of the week
9
such day does not exist

[Program name: L3task19.pas]

{0.3 points} Task 20:
To do: Perform the task using the Case statement. Arithmetic operations are numbered as follows: 1 - addition, 2 - subtraction, 3 - multiplication, 4 - division. Ask user to enter number - arithmetic operation (integer in the range of [1;4]) and two reals A and B (B is not = 0). Output the result of the specified arithmetic operation with the given numbers.

Expected output:

Enter arithmetic operation, please (from 1 till 4):
>>> 1  
Enter two real numbers:  
>>> 2.2  >>> 5.0 
the result is 2.2 + 5.0 = 7.2
Enter arithmetic operation, please (from 1 till 4):
>>> 2  
Enter two real numbers:
>>> 5.3  >>> 3.2 
the result is 5.3 - 3.2 = 2.5

[Program name: L3task20.pas]

{0.3 points} Task 21:
To do: Perform the task using the Case statement. Program requests to enter a number of the mass units (1 means kilogram, 2 means ounce, 3 - gram, 4 - ton, 5 - pound). Then the program requests body weight in one of these units. Output body weight in kilograms (1 ounce = 0.0283 kilograms, 1 gram = 0.0010 kilograms, 1 ton = 1000 kilograms, 1 pound = 0.4536 kilograms).

Expected output:

enter body weight 
>>> 4
enter mass 1..5 
>>> 4
in kilograms = 4000 

[Program name: L3task21.pas]

{0.5 points} Task 22:
To do: Perform the task using the Case statement. The commands are numbered as follows:

1 — check if the number is negative;
2 — check if the number is odd;
3 — check if the number is the divided by entered number D (the number D is entered by the user when this command is selected).

You must ask the user to input an integer N - the sequence number of the C command, and then to output the result. For example, for the command "2" output True if the number is odd and False otherwise.

Note: In response to entering an incorrect command, you should display the message "Command is unknown!".

Expected output:

N :
>>> -23 
C : 
>>> 1
True
N :
>>> 103
C :
>>> 3
D : 
>>> 7 
False

[Program name: L3task22.pas]


Using a String type

{0.2 points} Task 24:
To do: Perform the task using Case statement: The program must request the time of a day: midnight or night or morning or day or evening. The program must display a range of hours belonging to entered time of a day (midnight (24), night (1-4), morning (5-11), day (12-16), evening (17-23)).

Expected output:

enter the time of the day
>>> night
result: a range of hours for night is 1..4

[Program name: L3task24.pas]


Using ranges and enumerations

{0.2 points} Task 25:
To do: Perform the task using Case statement with ranges: The program must request the time of day in hours (from 1 to 24). Depending on the time entered, display a message indicating what time of day the entered hour belongs to (midnight (24), night (1-4), morning (5-11), day (12-16), evening (17-13)).

Expected output:

Enter a time of a day in hours (from 1 to 24), please: 
>>> 14 
result: 14 hours belongs to day
Enter a time of a day in hours (from 1 to 24), please: 
>>> 24
result: 24 hours belongs to midnight

[Program name: L3task25.pas]

{0.2 points} Task 26:
To do: Perform the task using Case statement with ranges: The program must request number - the age. Depending on the entered number display a message indicating the age of person in words (infancy: from 0 to 1 year old, early childhood: 2-4 years old, preschool: 5-7 years old, school age: 8 - 12 years old, youth: 13-19 years old, second youth: 20–35 years old, adulthood: 36-65 years, old age: over than 66 years).

Expected output:

how old are you?
>>> 6 
preschool
how old are you?
>>> 37 
adulthood

[Program name: L3task26.pas]


Tasks for self-solving

Note: tasks should be saved in a file with the name of the task, and be sure to insert a comment with the statement of the task in the code.

CASE

  1. Case6. The units of length are numbered as: 1 — decimeter, 2 — kilometer, 3 — meter, 4 — millimeter, 5 — centimeter. The order number N of a unit of length and also the length L of a segment are given (N is an integer in the range 1 to 5, L is a real number). Output the length of the segment in meters.
    Expected output:
    << dm =3.15
    results:
    m = 0.315
    
  2. Case12. Elements of a circle are numbered as: 1 — radius R, 2 — diameter D = 2·R, 3 — length L = 2·π·R of the circumference, 4 — area S = π·R2. The order number of one element and its value (as a real number) are given. Output values of other elements in the same order. Use 3.14 for a value of π.
    Expected output:
    << 2 D = 0.79
    results:
    R = 0.40  L = 2.48  S = 0.49
    
  3. Case15. The suits of playing cards are numbered as: 1 — spades, 2 — clubs, 3 — diamonds, 4 — hearts. Card values "Jack", "Queen", "King", "Ace" are numbered as 11, 12, 13, 14 respectively. A card value N (as an integer in the range 6 to 14) and a suit M (as an integer in the range 1 to 4) are given. Output the card description as: "six of diamonds", "queen of spades", etc.
    Expected output:
    <<  Card number N = 9,  Card value M = 3
    results:
    "diamonds nine"
    

Hometask

Complete this week's tasks that you didn't have time to do in class. The file names are specified in the tasks. You should upload your files to Microsoft Teams.