If without Else what happens?

I'm currently learning Python and wonder what happens if I define an if in a for loop but no else

if the if condition is not met

the loop is then repeated as soon as the if is fulfilled or simply directly

Sorry for the beginner question, I started this week

(2 votes)
Loading...

Similar Posts

Subscribe
Notify of
5 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
anonym321384
8 months ago

for each pass is checked whether the condition for if is fulfilled

-if yes, first what is done in if and then what is done outside if but inside for loop

if no, only what is outside if but within loop is executed

cleanercode
8 months ago

A loop has several iterations (throughs) you define.

Let’s say you’re about the word “good question”.

There are 9 runs. What happens per pass in the loop does not interest the loop as long as you do not break angibst (or other statements that leave the so-called stack). This can also be an exception or, depending on the paradigm, the return or even an exit().

So you have to intervene in the condition and manipulate the current passage – whether with if, if-else, try-except, match-case…., completely irrelevant.

ohwehohach
8 months ago

The rules are simple:

  • The If branch is only executed if the condition applies. Otherwise the Else branch. After that, the next statement will be made after the If-Else construct.
  • If there is no Else branch, the next statement will proceed directly after the If block if the condition for the If block does not apply.
  • A for-loop runs as long as its condition is fulfilled.

That as a prerequisite.

Thus, the For-Loop runs just as long as it is to run independently of the If-Statement. What happens in his block doesn’t matter to the For-Loop. And if the condition of the If applies, the If block is executed once. If not, the For-Loop will continue with the next iteration.

NoHumanBeing
8 months ago

The code within the loop is executed in each passage.

The code within the conditional block is executed when the condition (within the respective passage) is fulfilled.

priesterlein
8 months ago

The loop does not care about the if, because it may be within the loop is just a query without action. Without specific code, you can’t say what happens, but a missing else doesn’t matter. Then there is no specified alternative to fulfilling the if condition.

You should find help that helps you learn basic knowledge at least.