Last Updated : 21 Nov, 2022
Comments
Improve
Break statement in MATLAB is used for breaking out of an iterative loop, for or while loop. The break in MATLAB is similar to the break statements in other programming languages such as C, C++, Python, etc. We will see how to break out of a single for or while loop and the nested implementation of the same.
Syntax:
- For loop
for <iteration condition>
statement 1
————–
break conditional
break
end
See Also7.7: for loops with break Logic
- While loop
while <condition>
statement 1
————–
break conditional
break
end
We will use a loop that returns the first complete divisor of a number in the specified range.
Example 1:
S
Output:
We will use the same case with the while loop to see the usage of a break in the while loop.
Example 2:
Matlab
% MATLAB code for break in the while loop.
% Number whose first divisor is to be calculated
num = 35;
i=2;
%starting the while loop
while
true
rem = mod(num,i);
% Condition for break
if
(rem==0)
break
end
%incrementing i
i=i+1;
end
% Displaying the divisor
disp(i)
Output:
Using the break statement with nested loops.
We will find the index of number 23 in magic square of 5×5 using nested loops.
Example 3:
Matlab
% MATLAB code for update
arr = magic(5);
index=[-1 -1];
% Finding if 23 exits in arr and returning the index else, return -1
for
i=1:5
for
j=1:5
% Checks if arr(i,j) is 23
if
(arr(i,j) == 23)
index = [i j];
break
% Breaks the inner loop if 23 is found
end
end
% Checks if value of row is changed
if
(index(1)~=-1)
% Breaks the outer loop if row is not -1
break
end
end
% Displays the index of 23 in magic(5)
disp(
"index is = "
)
disp(index)
Output:
As can be seen, 23 is located in the 1st column of the 2nd row thus, the index is 2,1.
${secondsToHms(duration)}
${getTagsString(tags)}
'; } else{ let view_all_url = `${GFG_SITE_URL}videos/`; videos_content+=`
View All
`; } // videos_content+= '
'; } } return videos_content; } //function to show main video content with related videos content async function showMainVideoContent(main_video, course_link){ //Load main video $(".video-main").html(`
`); require(["ima"], function() { var player = videojs('article-video', { controls: true, // autoplay: true, // muted: true, controlBar: { pictureInPictureToggle: false }, playbackRates: [0.5, 0.75, 1, 1.25, 1.5, 2], poster: main_video['meta']['largeThumbnail'], sources: [{src: main_video['source'], type: 'application/x-mpegURL'}], tracks: [{src: main_video['subtitle'], kind:'captions', srclang: 'en', label: 'English', default: true}] },function() { player.qualityLevels(); try { player.hlsQualitySelector(); } catch (error) { console.log("HLS not working - ") } document.getElementById('article-video-tab-content').style.display = 'block'; } ); const video = document.querySelector("video"); const events =[ { 'name':'play', 'callback':()=>{videoPlayCallback(main_video['slug'])} }, ]; events.forEach(event=>{ video.addEventListener(event.name,event.callback); }); // error handling for no compatible source player.on('error', function() { var error = player.error(); console.log("Video Error: ", error); if (error && error.code === 4) { console.log("No compatible source was found for this media."); hideVideoPlayer(); } }); }, function(err) { var player = videojs('article-video'); player.createModal('Something went wrong. Please refresh the page to load the video.'); hideVideoPlayer(); // hiding video in case of timeout (requirejs) console.log(err); }); // function to hide the video player function hideVideoPlayer() { var videoPlayer = document.getElementById('article-video'); if (videoPlayer) { videoPlayer.parentNode.removeChild(videoPlayer); } } /*let video_date = main_video['time']; video_date = video_date.split("/"); video_date = formatDate(video_date[2], video_date[1], video_date[0]); let share_section_content = `
${video_date}
`;*/ let hasLikeBtn = false; // console.log(share_section_content); var data = {}; if(false){ try { if((loginData && loginData.isLoggedIn == true)){ const resp = await fetch(`${API_SCRIPT_URL}logged-in-video-details/${main_video['slug']}/`,{ credentials: 'include' }) if(resp.status == 200 || resp.status == 201){ data = await resp.json(); share_section_content+= `
`; hasLikeBtn = true; } else { share_section_content+= `
`; } } else { share_section_content+= `
`; } //Load share section // $(".video-share-section").html(share_section_content); // let exitCond = 0; // const delay = (delayInms) => { // return new Promise(resolve => setTimeout(resolve, delayInms)); // } // while(!loginData){ // let delayres = await delay(1000); // exitCond+=1; // console.log(exitCond); // if(exitCond>5){ // break; // } // } // console.log(loginData); /*if(hasLikeBtn && loginData && loginData.isLoggedIn == true){ setLiked(data.liked) setSaved(data.watchlist) }*/ } catch (error) { console.log(error); } } //Load video content like title, description if(false){ $(".video-content-section").html(`
${main_video['title']}
${hideMainVideoDescription(main_video['description'], main_video['id'])}
${getTagsString(main_video['category'])} ${(course_link.length)? `
View Course
`:''} `); let related_vidoes = main_video['recommendations']; if(!!videos && videos.length>0){ //Load related videos $(".related-videos-content").html(getvideosContent()); } } //show video content // element = document.getElementById('article-video-tab-content'); // element.style.display = 'block'; $('.spinner-loading-overlay:eq(0)').remove(); $('.spinner-loading-overlay:eq(0)').remove(); } await showMainVideoContent(video_data, course_link); // fitRelatedVideosDescription(); } catch (error) { console.log(error); } } getVideoData(); /* $(window).resize(function(){ onWidthChangeEventsListener(); }); $('#video_nav_tab').click('on', function(){ fitRelatedVideosDescription(); });*/ });