timer1

Fullscreen mode

Just press »F« on your keyboard to show your presentation in fullscreen mode. Press the »ESC« key to exit fullscreen mode.

Overview mode

Press "Esc" or "o" keys to toggle the overview mode on and off. While you're in this mode, you can still navigate between slides, as if you were at 1,000 feet above your presentation.


Python and The Basics of Programming.

Branches ( if, else, then, case, match, ternary operation, Elvis operator )

Lesson 2
Author: Egoshkin Danila Igorevich

Branches:

  • if, else, then
  • case, match
  • ternary operation
  • Elvis operator

If and Else

What is the "If and Else" Branches?

What is the "If and Else" Branches?

What is the "If and Else" Branches?

What is the "If and Else" Branches?

If and Else - Examples

If and Else


                            if(condition == true)
                            {
                            
                            }
                            else
                            {

                            }
                        

If and Else - ex.Max


int a, b, max;
cout << "Enter a: "; 
cin >> a;
cout << "Enter b: "; 
cin >> b;
if(a > b)
{
    max = a;
}
else
{
    max = b;
}
cout << "Maximum: " << max << endl;
                        

Can we use If without Else?

Can we use If without Else?

Yes!!!

if(condition == true)
{
    // ToDo
}
                        

If without Else - ex.Number


int number;
cout << "Enter number [0,+inf]: ";
cin >> number;
if(number == 0)
{
    cout << "The Number is equal to 0" << endl;
}
if(number > 0)
{
    cout << "The Number more than 0" << endl;
}
if(number > 25)
{
    cout << "and more than 25" << endl;
}
if(number > 50)
{
    cout << "and bigger than 50" << endl;
}
if(number > 100)
{
    cout << "and larger than 100" << endl;
}
                        

If without Else - ex.Max


int a, b, max;
cout << "Enter a: "; 
cin >> a;
cout << "Enter b: "; 
cin >> b;
max = a;
if(b > a)
{
    max = b;
}
cout << "Maximum: " << max << endl;
                        

If without Else - ex.Max

Is it looks good?

void encoder(char *text, int text_len){
    for (int i = 0; i < text_len; i++) {
        char letter = text[i];
        char part1 = (letter & 0xf0) >> 4;
        char part2 = letter & 0xf;
        char part1_1 = (part1 >> 3) << 2;
        char part1_2 = ((part1 >> 2) & 0x1) << 3;
        char part1_3 = ((part1 >> 1) & 0x1);
        char part1_4 = (part1 & 0x1) << 1;
        part1 = part1_1 | part1_2 | part1_3 | part1_4;
        part2 = part1 ^ part2;
        text[i] = (part1 << 4) | part2;
    }
}

bool encodeString(char *text, int text_len) {

    // What is wrong with encodeString function

    if (text != NULL) {
        encoder(text, text_len);
        cout << "Encrypted: " << text << endl;
    }
 
    return true;
}
                        

If..Else inside If..Else


if() {
    if() {
    }
    else {
    }
}
else {
    if() {
    }
    else {
    }
}
                        

If..Else inside If..Else

If..Else inside If..Else

If..Else inside If..Else


if(Rainy) {
    if(Windy) {
        if(Cold) {
            cout << "Stay Home and drink a tea" << endl;
        } else {
            cout << "Take umbrella and coat" << endl;
        }
    } else {
        cout << "Take umbrella" << endl;
    }
} else {
    cout << "Go for a walk" << endl;
}
                        

If and Else - ex.Max a == b


int a, b, max;
cout << "Enter a: "; 
cin >> a;
cout << "Enter b: "; 
cin >> b;

if(a == b) {
    cout << "a and b are the same" << endl;
    max = a;
}
else {
    if(a > b) {
        max = a;
    }
    else {
        max = b;
    }
}
cout << "Maximum: " << max << endl;
                        

else if - elif

If, Else If and Else - ex.Max a == b


if(condition1) {
}
else {
    if(condition2) {
    }
    else {
        if(condition3) {
        }
        else {
        }
    }
}
                        

if(condition1) {
}
else if(condition2) {
}
else if(condition3) {
}
else {
}

                        

If, Else If and Else - ex.Max a == b


int a, b, max;
cout << "Enter a: "; 
cin >> a;
cout << "Enter b: "; 
cin >> b;

if(a == b) {
    cout << "a and b are the same" << endl;
    max = a;
}
else {
    if(a > b) {
        max = a;
    }
    else {
        max = b;
    }
}
cout << "Maximum: " << max << endl;
                        

int a, b, max;
cout << "Enter a: "; 
cin >> a;
cout << "Enter b: "; 
cin >> b;

if(a == b) {
    cout << "a and b are the same" << endl;
    max = a;
}
else if(a > b) {
    max = a;
}
else {
    max = b;
}

cout << "Maximum: " << max << endl;
                        

Switch...Case - Match...Case

Switch...Case - Match...Case


if(color == COLOR_GREEN) {
    cout << "Go, go, go... But don`t RUN" << endl;
}
else {
    if(color == COLOR_YELLOW) {
         cout << "Hold on, steady, wait" << endl;
    }
    else {
        if(color == COLOR_RED) {
            cout << "Stay and wait" << endl;
        }
        else {
            cout << "Stop digging your nose and look at the color" << endl;
        }
    }
}
                        

enum Colors {
	COLOR_GREEN,
	COLOR_YELLOW,
	COLOR_RED
};

switch (color)
{
    case COLOR_GREEN:
        cout << "Go, go, go... But don`t RUN" << endl;
        break;
    case COLOR_YELLOW:
        cout << "Hold on, steady, wait" << endl;
        break;
    case COLOR_RED:
        cout << "Stay and wait" << endl;
        break;
    default:
        cout << "Stop digging your nose and look at the color" << endl;
        break;
}

                        

Switch...Case - Match...Case

Switch...Case - Match...Case

Only in python


match (object):
    case objectCase1 | objectCase2 | objectCase3:
        ...
    case _:
        ...
                        

Switch...Case - Match...Case

Only in python (Structural pattern matching is coming in Python 3.10)
Ternary operation ???

Ternary operation


Crown of thorns - Терновий вінець

https://en.wikipedia.org/wiki/Prunus_spinosa

Ternary operation in C++ and other

Ternary operation in Python and other

Ternary operation

Ternary operation

Ternary operation


looks lambda...
x -> M
f(x) -> M
but: (M is an expression that uses x) https://en.wikipedia.org/wiki/Lambda_calculus

Elvis operator

https://en.wikipedia.org/wiki/Elvis_operator

Elvis operator

Elvis operator - aka (also known as) null coalescing operator https://en.wikipedia.org/wiki/Null_coalescing_operator

In python can be made by Ternary operation


now() if time is None else time
                        

Branches usage???

Branches:

  • if, else, then
  • case, match
  • ternary operation
  • Elvis operator

AI

AI

Simple expert system on rules

Tic Tac Toe Game

Other usage:
AI usage, filtering
TODO

Links:

Thanks for your attention :)