 Syntax of if-else Statement
Syntax of if-else Statement
Let's briefly discuss the syntax of the if-else construct.
- The syntax of the ifstatement remains unchanged;
if condition:
    action
- Write the elsekeyword. Note that it is case sensitive, so it is not the same as Else so if you use Else you will get an error because it is not correct;
if True condition:
    action
else:           # if False condition
    other action
- Furthermore, you don't need to specify a condition when using elsesince it inherently implies the opposite condition of the if part;
- Simply use a colon;
- Then, proceed with code indentation for the instructions executed within the else-block. These instructions are executed if our condition in theifstatement evaluates toFalse.
Practice Time!
Uppgift
Swipe to start coding
You have to find the greatest number of three numbers using an if-else statement.
Fill in the blanks.
Lösning
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 2. Kapitel 2
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Suggested prompts:
Sammanfatta detta kapitel
Explain code
Explain why doesn't solve task
Awesome!
Completion rate improved to 5.88 Syntax of if-else Statement
Syntax of if-else Statement
Svep för att visa menyn
Let's briefly discuss the syntax of the if-else construct.
- The syntax of the ifstatement remains unchanged;
if condition:
    action
- Write the elsekeyword. Note that it is case sensitive, so it is not the same as Else so if you use Else you will get an error because it is not correct;
if True condition:
    action
else:           # if False condition
    other action
- Furthermore, you don't need to specify a condition when using elsesince it inherently implies the opposite condition of the if part;
- Simply use a colon;
- Then, proceed with code indentation for the instructions executed within the else-block. These instructions are executed if our condition in theifstatement evaluates toFalse.
Practice Time!
Uppgift
Swipe to start coding
You have to find the greatest number of three numbers using an if-else statement.
Fill in the blanks.
Lösning
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 2. Kapitel 2
single