Free YouTube Transcribe

Video transcript

Оператор перехода goto. Когда код становится непонятен даже создателю. Урок #22.

#SimpleCode · 874 words · 4 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:11Greetings , friends . My name is Sergey .

0:13We are continuing to learn programming

0:16using the C ++ language . And today we

0:19will study the goto keyword . Before we

0:22begin , I want to warn you that many

0:24programmers strongly advise against

0:26using this word . Many books speak very

0:30negatively about it , and for good

0:32reason . When used , the code becomes

0:35very difficult to maintain , read , and

0:38understand what is happening and why .

0:41Therefore , it must be used very , very

0:44carefully . I won't say it should never

0:48be used , though there is an opinion

0:49that it's better not to use it at all .

0:53Perhaps there are situations where you

0:55can't do without it , but if you can

0:57avoid it , it is better to do so . Now ,

1:00let's find out what kind of beast this

1:02is . So , we currently have a program

1:06that simply prints five words using

1:08five cout statements . 1 2 3 4 5 . This

1:13is how it works . What can we do with

1:16this keyword ? We can interrupt the

1:20program's execution at any line of code

1:23, jump to any other part of the program

1:25, and start executing there . That , in

1:29fact , is where the danger of this

1:31keyword lies . Let's test how it works .

1:36So , let's say after message one is

1:38printed , we want to skip this code that

1:41prints 2 and 3 and jump straight to the

1:43code that prints 4 and 5 . What do we

1:48need to do for this ? We need to write

1:50some kind of name . Not even a variable

1:54name , but rather , let's say , a label

1:56that our program will jump to . Let's

2:00call our label with the English word

2:01link . And after writing its name , we

2:05place a colon at the end . That is

2:07basically it . We have a certain label

2:11in our code that we can jump to if we

2:13reference this label . And we can

2:17reference this label using the goto

2:19keyword . So , let's say we want to skip

2:22these two lines after message one is

2:25printed and land here , right ? Then 4

2:27and 5 will be printed . To do this , we

2:32need to write goto here and , naturally ,

2:34write our link , this label that we

2:36declared here as link . Let's just copy

2:40it exactly like that . We will put a

2:42semicolon at the end . Now , when our

2:44program reaches the thirteenth line of

2:46code , it will skip everything here and

2:48jump straight here . Let's run it and

2:51see how it works . As you can see , it

2:54printed 1 4 5 , which is how it worked .

2:58Let's take a closer look in the

3:00debugger . Starting the debugger . We can

3:05see we stopped at line eleven of the

3:07code . And let's go ahead and output all

3:10this step by step . Taking a step . We

3:12got message one printed . Next , we land

3:14on line thirteen of the code , where we

3:17have a link . So now , when we take a

3:19step , even if we just put the cursor

3:21here , our studio highlights another

3:24link for us . Meaning we will jump from

3:26here to there . Taking a step . We jumped

3:30. Right after line twenty , execution

3:32continues at line twenty-two . Now , four

3:35will be printed to the screen . And

3:37that's how it happens . And five . Why is

3:39this thing so disliked ? Why do people

3:42hate goto ? First of all , there might be

3:44a lot of code here where your link will

3:46simply get lost , and it’s not very

3:48easy to find . You might make some kind

3:51of error in this code . So , if someone

3:53opens your code after you , or if you

3:55have several programmers working on a

3:57project , they won't be able to figure

3:59it out . That is exactly why this is a

4:01very dangerous thing . So , I repeat ,

4:03it’s certainly better to avoid using

4:06it . I don't even know when you might

4:08need to use it . For instance , in my own

4:10practice , I have never once had a case

4:13where this thing came in handy . That is

4:15, you can get by with either loops or

4:17standard logic using if statements .

4:21This makes the code much more readable ,

4:23everything is clear , and it works

4:25perfectly without that keyword . But in

4:27any case , you should know what it is

4:29and how it works . Well , that's actually

4:31all I have for now . If you liked this

4:34video , found it useful or interesting ,

4:36please give it a like and subscribe to

4:38the channel so you don't miss the next

4:40lesson if you haven't subscribed yet .

4:42Thank you all for your attention . Bye .

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.