Skip to content

Latest commit

 

History

History
96 lines (42 loc) · 1.76 KB

4. Control Structures - Part I.md

File metadata and controls

96 lines (42 loc) · 1.76 KB

Controle structures - Part I

What is program?

  • Procedure

  • Sequence of steps that should be performed.

  • Well designed program consists of smaller independent parts.

  • These independent parts should be executed in some order.

What is control structure?

  • Control structure defines in which order program is going to be executed.

  • First do this, then do this, after do that…

  • If some condition is met, do this. Otherwise do that.

Controle structure types

  • Sequence logic, or sequential flow

  • Selection logic, or conditional flow

  • Iteration logic, or repetitive flow

How is program executed?

  • Top to bottom

Sequence logic - Sequence flow

  • Sequence of steps that should be executed

Selection logic - Conditional flow

  • If statement

  • If-else statement

  • Switch - does not exist in python. Can be replicated by else-if statement.

Iteration logic - Repetitive flow

  • For loop

  • While loop

  • Do loop - does not exist in python. Can be made using for or while statement.

Controle structure

  • Every control structure consists of two parts:

    • Head
    • Body
  • How are head and body separated?

  • Can there be more than one head/body?

  • Head and body are separated by colon and indentation.

  • There can be more than one head/body.If-else statement.

  • Head

    • Contains sth that evaluates to boolean
  • Body

    • Contains set of instructions that should be performed if head evaluates to true

Break, continue, pass

  • Break: stop loop cycle when condition is met
  • Continue: just continue execution of program to next interation. Code after 'continue' keyword will be ignored
  • Pass: Do sth when condition is met, but code after 'pass' keyword will not be ignored