Linguagem C - Aula 5.4 - Conheça os comandos de Controle de Fluxo break e continue (2022)
Understanding Break and Continue in Loops
Introduction to Loop Structures
- The video discusses the final lesson on loop structures, focusing on the
breakandcontinuecommands in programming.
- It emphasizes that both
forandwhileloops can be utilized interchangeably in programming languages like C.
The Break Command
- The
breakcommand is introduced as a way to completely terminate a loop sequence at any point.
- An example is provided where printing numbers from 1 to 10 can be interrupted using
break, allowing for only numbers up to 5 to be printed.
The Continue Command
- Unlike
break, thecontinuecommand does not end the loop but skips the current iteration, moving directly to the next one.
- This allows for selective skipping of certain iterations based on conditions without terminating the entire loop.
Practical Example with Break
- A practical coding example illustrates how a loop initialized at 1 continues until it reaches 10, but can print only up to 5 due to an internal condition checking for equality with 5.
- If the condition is met (i.e., when i equals 5), a
breakstatement is executed, halting further iterations of the loop.
Observations on Loop Behavior
- As each number is printed, checks are made against the value of i. When i equals 5, execution jumps out of the loop due to
break.
- Removing or altering this break condition changes output behavior significantly; without it, all numbers from 1 through 10 would print normally.
Conclusion on Break Functionality
- The video concludes by reiterating that once a break statement is encountered within a loop structure, all subsequent code within that block will be ignored.
Understanding Break and Continue in Loops
The Role of Break in Loop Execution
- The
breakstatement is used to exit a loop, ignoring any subsequent code within the loop after its invocation.
- If a print statement is placed outside the loop, it will execute regardless of the
break, demonstrating thatbreakonly affects the current iteration.
Exploring Continue Functionality
- The
continuestatement allows skipping the current iteration of a loop when certain conditions are met, such as checking if a variable equals 5.
- When using
continue, if the condition is true (e.g., i = 5), it skips to the next iteration without executing further commands in that cycle.
Iteration Mechanics with Continue
- As iterations progress, values are incremented. For instance, when i reaches 5, it triggers
continue, preventing printing of that value.
- After invoking
continue, control moves directly to the next iteration; thus, no output occurs for skipped values.
Incrementing Values and Conditions
- Each time through the loop, values are checked against conditions (e.g., less than or equal to 10). If not satisfied (like i = 11), execution ends.
- The process continues until all numbers from 1 to 10 are printed except for those equal to 5 due to the use of
continue.
Summary of Loop Behavior
- The final output includes numbers from 1 to 10 but omits number 5 because it was skipped by
continue.
- It’s emphasized that while
breakexits loops entirely,continuemerely skips individual iterations based on specified conditions.
Conclusion and Next Steps
- In this lesson, key concepts about how both commands (
breakandcontinue) function within loops were discussed.