Перейти к основному содержанию
EDU-MMCS
Вы используете гостевой доступ (Вход)

cs101 ОП / Basics of programming ENG

  1. В начало
  2. Курсы
  3. Осенний семестр
  4. Фундаментальная информатика и ИТ
  5. cs101 ENG
  6. 1 topic. Basic constructions and loops
  7. Lesson #4. Condition operator & branches. IF s...

Lesson #4. Condition operator & branches. IF statement Part I (19/09)

Требуемые условия завершения
Открыто: Friday, 13 September 2019, 08:00

Lections 

Short form of IF-statement:
if condition then
   some operator
...

some operator will be done if the condition is True. If the condition is False pascal will skip some operator.

E.g:
var a:=5;
if a > 7 then
   print(a-1)
print(a) 
// the value of a (5) will be displayed

The statements after the if statement will run regardless if the condition is true or false: 

print(a)

Full form of IF-statement:

The else statement runs a block of code if none of the conditions are True before this else statement:

if condition then
   operator1
else
   operator2

operator1 will be done if the condition is True; in this case operator2 will be not executed. If the condition is False pascal will skip operator1 and will execute operator2. Before else-block semicolon is not needed.

E.g:
var a:=5;
if a > 7 then
   print(a-1)
else
   print(a+1)
print(a) 
// at first the value of a+1 (6) will be displayed and then the value of a (5)

The semicolon before else is not needed!

Also there can be not one but two or more conditions. Each condition must be in round brackets.

E.g:
if(year<20) or (year>18) then 
    …
// if one of the conditions or both conditions are true then ...
After if-block and else-block may go not one but some operators. In this case we have to use begin..end block:
if condition then
begin
   operator1
   operator2
end
else
begin
   operator3
   operator4
end

The most popular errors:

  • You don't need to write semicolon before else.
  • Don't forget about begin..end in the cases when there is more than one operand after then or else.
  • Examples

    • If the variable is odd or even:
    • 1
    • If the variable is not greater than 2:

    How to Swap Values of Variables?

    1 solution:


    2 solution:


    Tasks (if-statement)

    Max points for all tasks = 4

    1. {0.5 points}[task-01-if.pas] An integer is given. If it is positive then add 1 to it. Output the result.

    Example:
    -3 >>> -3
     0 >>>  0
     1 >>>  2
     5 >>>  6
    

    2. {0.5 points}[task-02-if.pas] An integer is given. If it is even number then myltiply it by 10. Output the result.

    Example:
    2 >>> 20
    1 >>>  1
    -10 >>> -100
    

    3. {0.5 points}[task-03-if.pas] An integer is given. If it is even number then myltiply it by 3, if it is not even then devide it by 3. Output the result.

    Example:
    4 >>> 12
    9 >>>  3
    -10 >>> -30
    

    4. {0.8 points}[task-04-if.pas] An integer is given. If it is positive then add 1 to it; otherwise subtract it by 2. Output the number at the end of the program.

    Example:
    -3 >>> -5
     0 >>> -2
     1 >>>  2
     5 >>>  6
    

    5. {0.8 points}[task-05-if.pas] An integer is given (the age of a person). If it is grater 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 output "you can take a walk".

    Example:
    18 >>> you can watch this movie
     8 >>> you should watch the cartoon
     15 >>>  you can take a walk
    6. {0.8 points}[task-06-if.pas] Student has got a mark. If it is 2 mark then the program has to output "it's very bad"; if it is 3 - program has to output "it's bad"; if it is 4 - "it's good", in the case of 5 - "it's excellent", otherwise - "such marks do not exist" . 

    Example:
    2 >>> "it's very bad"
    4 >>> "it's good"

    7. {0.8 points}[task-07-if.pas]The program must request the time of day in hours (from 1 up 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)).

    Example:

    2 >>> "night"
    24 >>> "midnight"

    8. {0.8 points}[task-08-if.pas] Two integer values are given: for variables A and B. Assign a sum of these values to each variable if their values are not equal; otherwise (if equal) assign zero to the variables. Output new values of variables A and B.

    Example:
    A = -6, B = 10 >>> A = 4, B = 4
    A =  2, B =  2 >>> A = 0, B = 0
    A =  0, B =  0 >>> A = 0, B = 0
    

    Check. Check the correctness of your program with at least two input sets: for the same and different values of the variables A, B. Give the log of the program with these sets in the form of a comment.

    9. {0.3 points}[task-09-if.pas] Two integer values are given: for variables A and B. Swap the values of the variables and output them.

    Example:
    A = -6, B = 10 >>> A = 10, B = -6

    10. {0.5 points}[task-10-if.pas] Two integer values are given: for variables A and B. If the value of A is greater than the value of B - swap the values of the variables and output them.

    Example:
    A = 6, B = 2 >>> A = 2, B = 6
    ◄ Lesson #3. Boolean Expressions And Conditions
    Lesson #5. IF statement Part II. Max and Min ►
    Пропустить Навигация
    Навигация
    • В начало

      • Страницы сайта

        • Мои курсы

        • Теги

      • Мои курсы

      • Курсы

        • Осенний семестр

          • Прикладная математика и информатика

          • Фундаментальная информатика и ИТ

            • Compiler Development

            • КПР

            • АЗПК

            • Frontend

            • ТеорЯП

            • Ruby Eng

            • EngCA&OS

            • CS201e

            • Компиляторы - лекции

            • CS202

            • CS211 C++ ENG

            • cs101 ENG

              • Общее

              • 1 topic. Basic constructions and loops

                • ЗаданиеLesson #1. Introduction to PascalABC.NET

                • ЗаданиеLesson #2. Integers, div and mod operators

                • ЗаданиеLesson #3. Boolean Expressions And Conditions

                • ЗаданиеLesson #4. Condition operator & branches. IF s...

                • ЗаданиеLesson #5. IF statement Part II. Max and Min

                • ЗаданиеHometask (until 6/10)

                • ЗаданиеFormatted output using WritelnFormat

                • ЗаданиеLesson #6. Chained IF statements and Case statemen...

                • ЗаданиеLesson #7. Loops

                • ЗаданиеLesson #8. For Loop

                • ЗаданиеLesson #9. Sum, Accumulators, Product and Counter....

                • ЗаданиеLesson #10. Minimum or Maximum value of N numbers

                • ЗаданиеLesson #11. While & Repeat Loops

                • ЗаданиеLesson #12. While & Repeat Loops: digits of a ...

                • ЗаданиеLesson #13. Procedures

                • ЗаданиеLesson #14. Functions

                • ЗаданиеTest Loops (control test)

              • Тема 2

              • Тема 3

              • Тема 4

              • Тема 5

              • Тема 6

              • Тема 7

              • Тема 8

              • Тема 9

              • Тема 10

          • Математика, механика

          • Педагогическое образование

          • Магистратура

            • Разработка мобильных приложений и компьютерных игр

          • Аспирантура

          • Вечернее отделение

          • Другое

        • Весенний семестр

          • Прикладная математика и информатика

          • Фундаментальная информатика и ИТ

          • Математика, механика

          • Педагогическое образование

          • Магистратура

            • Разработка мобильных приложений и компьютерных игр

          • Аспирантура

          • Вечернее отделение

          • Другое

        • Воскресная компьютерная школа

          • Пользователь компьютера плюс

          • Пользователь прикладных программ

          • Программирование I ступень

          • Программирование II ступень

          • Программирование III ступень

          • Архив

        • Воскресная математическая школа

          • Олимпиадная математическая школа

          • Открытое тестирование - 2023 г.

          • Открытое тестирование - 2022 г.

          • Повышение квалификации

          • Архив

          • Доступная математика

          • ВМШ - 22

        • Государственная итоговая аттестация

        • Дополнительное образование

        • Олимпиады

        • Видеолекции

        • Разное

        • Архив курсов

    Вы используете гостевой доступ (Вход)
    cs101 ENG
    Сводка хранения данных
    Скачать мобильное приложение Яндекс.Метрика