Free YouTube Transcribe

Video transcript

Ключевое слово break. Оператор break. Описание. Пример. Синтаксис. Урок #18.

#SimpleCode · 1,407 words · 7 min read

Want to search this transcript, jump the video from any line, or download it as TXT, SRT, or VTT?

Open in the transcript tool

Full transcript

0:00Greetings , friends . My name is Sergey ,

0:02and we are continuing to learn

0:03programming using the C ++ language .

0:06Our topic for today will be the break

0:08keyword . We will talk about what it is ,

0:11why it is needed , and how to use it . So

0:13, to study the break keyword , we will

0:16need our code from the last lesson . If

0:19you haven't watched it , be sure to

0:21watch it . In that very lesson , I showed

0:24a certain feature of the for loop where

0:27it acts as an infinite loop . And it is

0:30precisely to solve this problem that we

0:33need the break keyword . Before we start

0:36solving this problem , let's recall what

0:38we had there and see what code we have

0:41now . I modified it a little bit to make

0:44it more illustrative . Let's run it

0:46first , and with the code running , I

0:48will explain to you what's what . So , on

0:51line eleven and line eighteen , we have

0:53two inscriptions . These are the start

0:55of the loop and the end of the loop .

0:56This is so that in the console , at the

0:59start and end of the loop , we can

1:01clearly see that it has begun and ended

1:03. And we also have the for loop itself .

1:06And it works as long as the variable i

1:09is less than 10 . Under this condition ,

1:13our loop runs 10 times and completes

1:16successfully . But in the last lesson , I

1:19told you that this part of the for loop

1:21is optional . That is , without it , it

1:26will also work , but it will act as if

1:29it is always true here . In that case ,

1:33our loop will run infinitely . Let's

1:36take a look . So , we won't see the end

1:39of the loop . Likewise , if we remove it

1:41from here , it makes no difference . This

1:45thing works in the exact same way . And

1:48it is in this case that we can clearly

1:51see how the break keyword works . To see

1:54how it works , we will need a condition

1:58that performs some kind of check . Let's

2:02say we now have a simple condition that

2:05will simply check our variable i , which

2:08is also the control variable of our

2:11loop , our loop iterator , for equality

2:14with five . If our condition in the if

2:18statement is met , that is , if the

2:20variable i equals five , we will enter

2:23the body of the loop . Right here , we

2:25can write the break keyword . What will

2:29happen in this case ? In this case , we

2:32will exit the loop . So , the break

2:35keyword is used to interrupt the

2:37execution of a loop and exit it . That

2:40is , before any condition that could

2:43have been placed here is met . In this

2:46case , the condition in the loop itself

2:48is always true , so it’s an infinite

2:49loop , as I’ve already shown you . But

2:52by using the " break " keyword with this

2:55check for five , we can exit the loop .

2:58Let's run our program and see more

3:00clearly what happens here . So , as soon

3:04as the variable i in our loop became

3:06equal to five , this condition became

3:09true , we went inside , hit the " break "

3:12keyword , and exited . Our program

3:16continued to execute outside the loop .

3:18Thus , once again , the " break " keyword

3:20is used to interrupt the execution of a

3:23loop under specific conditions . We can ,

3:26in principle , leave our " i is less than

3:28" condition here , right ? What is going

3:31on ? " i is less than 10 " . Let's comment

3:35out this part of the code for now .

3:38Let’s run it again . So , now our loop

3:44runs as long as our variable i is less

3:46than 10 , executing 10 times . But if we

3:50uncomment the condition with our " break

3:54" keyword and run our program again ,

3:57our loop will still only run five times

4:01. Note that although in the loop

4:06condition our variable i could reach 10

4:09, this condition was met sooner ,

4:12meaning i became five before this

4:14condition was fulfilled . We entered the

4:18if statement , the " break " keyword was

4:20executed , and we exited , we exited the

4:23loop . To help you understand : it's a

4:27simple condition here , but there could

4:30be a lot of code , and your loop should

4:33run until , say , once in a million years

4:37, a specific condition is met that

4:39returns true , and then you break out of

4:43the loop . This way of working is also

4:46possible . The " break " keyword works the

4:49same way with " while " loops and " do

4:51while " loops . I will show you with a "

4:54while " example . So , our " while " loop

4:56condition will be true . Meaning the

4:58condition in the parentheses will

5:00always be true . We’ll just set it to

5:01true directly . Therefore , our loop will

5:04run indefinitely . Let’s comment out

5:06this part of the code . So it won’t be

5:09executed right now . It’s simply not

5:11here . I’ll probably just delete it

5:12from here . Move it somewhere else ,

5:15right ? Paste it like this . We’ll only

5:19need this bit of text . Okay , that’s

5:21it . We don’t need this part . In this

5:23case , our loop will run indefinitely .

5:29True , we don’t have an i variable in

5:31our " while " loop , so we need to create

5:33it . We’ll create it right now .

5:36Initialize it to zero . And we’ll also

5:40add an increment here so it increases

5:43with every iteration of the loop .

5:47Basically , this construction is almost

5:49the same as what we had with the for

5:50loop , just written a little differently

5:52. Let's run it . It works exactly the

5:55same way , meaning it is looped . And

5:58here we can also add our check to see

6:02if variable a is equal to , let's say ,

6:06six now . Alright . And if our variable i

6:11becomes equal to six , then we will exit

6:14the loop thanks to the break keyword .

6:17Let's take a look . Well , that is

6:20exactly what happened . Only we see the

6:25message variable i = 5 here , not

6:28variable i = 6 . Because first , we

6:32printed the record with our variable i

6:36when it was equal to five . Then

6:38variable i increased by one thanks to

6:41the increment . And we entered this

6:43condition . So the variable i was

6:45already equal . Therefore , we exited the

6:47loop , and the record that our variable

6:50i is already 6 was not displayed . That

6:52is roughly how it works . In a while

6:54loop , it is the same . Also , the break

6:56keyword works not only with loops .

6:59Perhaps , if you watched my previous

7:02videos carefully , you remember I had a

7:05lesson about the Switch structure , and

7:08there were also case and default blocks

7:11that also worked with the break

7:13operator . But I won't repeat myself

7:16here now . If you haven't seen that

7:19video , then at the end of this video

7:21there will be a link to it . And also in

7:23the description below this video , I

7:25will leave a link to it as well . You

7:26can take a look . I explain everything

7:28in detail there . Well , actually , that's

7:30all I have for now . If you liked this

7:32video , if it was useful and interesting

7:34to you , then hit the like button . And

7:36also subscribe to the channel if you

7:37haven't yet , so you don't miss the next

7:39lessons . Thank you all for your

7:41attention . Bye . M.

More from #SimpleCode

Recently added transcripts

Browse the whole transcript library

This transcript was generated from the captions YouTube publishes for this video. Get the transcript of any YouTube video atfreeyoutubetranscribe.com, free, unlimited, no sign-up.