Anasayfa C Dili Döngülerde Break ve Continue Komutları

C Dili Döngülerde Break ve Continue Komutları

Okan

Break ve continue komutları döngü komutlarının akışını kontrol etmek için kullanılır. Kısaca, break komutu yapıyı sonlandırırken continue komutu da döngüde belli bir şarta uyan kısmın atlanmasını sağlar.

Break Komutu

Bir C programında, bir işlem gerçekleştirilirken, işlemin sona erdirilmesi bu komut ile yapılır.

Break komutu while, do-while ve for döngülerinde oldukça kullanılır.

Şimdi break komutunu programımızda uygulayalım.

#include <stdio.h>

main()
{
	for(int i = 0; i < 5; i++)
	{
		if(i == 2){
			break;
		}
		printf("Kod Bloklari\n");
	}
	
	printf("Dongu sonlandi ve program dongunun sonundan devam ediyor.");
}

Yukarıdaki programda normalde 5 defa “Kod Bloklari” yazısını yazdırması gerekirken, biz “i”nin değeri 2 olduğu zaman break komutunu kullanarak döngüyü sonlandırdık.

Kodun ekran çıktısı aşağıdaki şekildedir:

break ve continue kod-1

Şimdide switch-case yapısındaki break komutlarını inceleyelim. C Dilinde Switch-Case Yapısı sayfasında yaptığımız hesap makinesi örneğimizi kullanalım.

#include <stdio.h>

main()
{
	int sayi1,sayi2;
        int islem;
	
	printf("1.Sayiyi giriniz:");
	scanf("%d",&sayi1);
	printf("2.Sayiyi giriniz: ");
	scanf("%d",&sayi2);
	

	printf("\n\n1.Toplama\n");
	printf("2.Cikarma\n");
	printf("3.Bolme\n");
	printf("4.Carma\n");
	printf("5.Programdan cik.");
	
	printf("\nIslemi seciniz:");
	scanf("%d",&islem);
	
	switch(islem){
		case 1:
			printf("Toplama isleminin sonucu : %d",sayi1 + sayi2);
			break;
		case 2:
			printf("Cıkarma isleminin sonucu : %d",sayi1 - sayi2);
			break;
		case 3:
			printf("Bolme isleminin sonucu : %f", (float) sayi1 / (float) sayi2);
			break;
		case 4:
			printf("Carpma isleminin sonucu : %d", sayi1 * sayi2);
			break;
		case 5:
			break;
		default:
			printf("Lutfen gecerli bir sayi giriniz..");
	}
}

Burada biz işlemimizi seçtikten sonra bizim seçimimize göre bir case ifadesine giriyor ve işlemi yaptıktan sonra break komutuyla döngüyü sonlandırıyor.

Kodun çıktısı da aşağıdaki şekildedir:

break ve continue -kod-2

Burada işlem değişkenine 5 sayısını girdiğim için case 5: ifadesinin içersindeki komutları çalıştırdı fakat içinde break dan başka bir komut olmadığı için sadece break komutunu kullanarak döngüyü sonlandırdır.

Eğer case ifadelerinden sonra break komutu kullanmasaydık, bir case ifadesini sağladıktan sonra altındaki tüm komutları çalıştırırdı.

Aşağıdaki örnekte, yukarıda yaptığımız hesap makinesi örneğindeki case ifadelerindeki break komutunu kullanmayacağım. Bakalım ne gibi değişikler olacak..

#include <stdio.h>

main()
{
	int sayi1,sayi2;
        int islem;
	
	printf("1.Sayiyi giriniz:");
	scanf("%d",&sayi1);
	printf("2.Sayiyi giriniz: ");
	scanf("%d",&sayi2);
	

	printf("\n\n1.Toplama\n");
	printf("2.Cikarma\n");
	printf("3.Bolme\n");
	printf("4.Carma\n");
	printf("5.Programdan cik.");
	
	printf("\nIslemi seciniz:");
	scanf("%d",&islem);
	
	switch(islem){
		case 1:
			printf("Toplama isleminin sonucu : %d\n",sayi1 + sayi2);
			//break;
		case 2:
			printf("Cikarma isleminin sonucu : %d\n",sayi1 - sayi2);
			//break;
		case 3:
			printf("Bolme isleminin sonucu : %f\n", (float) sayi1 / (float) sayi2);
			//break;
		case 4:
			printf("Carpma isleminin sonucu : %d", sayi1 * sayi2);
			break;
		case 5:
			break;
		default:
			printf("Lutfen gecerli bir sayi giriniz..");
	}
}

Kod kısmındaki break komutlarını açıklama satırı haline getirdim. Yani program tarafından okunmuyor.

Kodun ekran çıktısı aşağıdaki şekildedir:

break kod-2

Yukarıda bahsettiğim gibi, case ifadelerinin sonunda break komutunu kullanmadığım için program case1: ifadesine girdikten sonra break komutu göremediği için çalışmasını sürdürdü.

Continue Komutu

Bir döngü içerisinde continue komutu ile karşılaşılırsa, ondan sonraki komutları atlar ve döngünün bir sonraki değerinden devam eder.

Şimdi basit bir örnek üzerinde continue komutunu uygulayalım.

#include <stdio.h>

main()
{
	for(int i = 0; i < 5; i++)
	{
		if(i == 2){
			continue;
		}
		printf("KOD BLOKLARI\n");
	}
}

Bu döngü normal koşullarda ekrana 5 defa “Kod Bloklari” yazdırması gerekirken “i = 2” değeri olunca program 8.satırdaki continue komutunu görür ve direk 5.satırdan yani döngünün bir sonraki değerinden devam eder. Bu yüzden ekrana 4 defa yazı yazdırmış olur.

Kodun ekran çıktısı aşağıdaki şekildedir:

continue kod-1

Şimdi yine continue komutunu kullanarak başka bir örnek yapalım.

1 den 10 a kadar olan sayıları döngü yardımıyla toplayalım ama arasından 4 değerini bu toplamaya katmayalım.

Bunun için döngü 4 değerini aldığı zaman toplama işlemini gerçekleştirmeden bir sonraki değerden devam etmesini istiyoruz. Bunuda continue komutuyla yapacağız.

#include <stdio.h>

main()
{
	int toplam = 0;
	
	for(int i = 1; i < 10; i++)
	{
		if(i == 4){
			continue;
		}
		toplam += i;
	}
	printf("Toplam = %d",toplam);
	
}

Yukarıdaki kod da program 10.satıra geldiği zaman continue ifadesini gördüğü için alt satırdaki toplama işlemini yapamadan döngünün başına dönüyor. Bu şekilde 4 değerini aralarına katmadan 1 den 10 a kadar olan sayıların toplamını bulmuş olduk.

Kodun ekran çıktısı aşağıdaki şekildedir:

break ve continue kod-4

Break ve continue komutlarını farklı örneklerde kullanabiliriz. C Programlama Dili Örnekleri sayfasını ziyaret ederek farklı örnekleri inceleyebilirsiniz.

15 Yorum

vorbelutr ioperbir 20 Haziran 2023 - 02:42

Great post. I was checking continuously this blog and I’m impressed! Extremely helpful info specifically the last part :) I care for such information a lot. I was looking for this particular info for a very long time. Thank you and good luck.

Cevapla
http://mobileautodetailingkc.com 11 Ağustos 2023 - 20:44

Great, thanks for sharing this post.Much thanks again. Really Cool.

Cevapla
Ethereum Contract Online Tool 23 Ağustos 2023 - 23:55

I truly enjoy looking through on this internet site, it has good articles.

Cevapla
Socks 11 Aralık 2023 - 15:51

hello there and thank you on your info ? I?ve definitely picked up something new from proper here. I did then again experience several technical issues using this web site, as I experienced to reload the site a lot of times prior to I could get it to load properly. I had been considering if your web host is OK? Not that I am complaining, however sluggish loading cases times will often affect your placement in google and can injury your high quality rating if ads and ***********|advertising|advertising|advertising and *********** with Adwords. Anyway I am including this RSS to my email and could look out for much extra of your respective exciting content. Ensure that you update this again soon..

Cevapla
tlovertonet 17 Aralık 2023 - 00:33

Utterly composed content, Really enjoyed examining.

Cevapla
Toronto Female Escorts 20 Ocak 2024 - 06:26

I have recently started a blog, the information you offer on this site has helped me tremendously. Thanks for all of your time & work.

Cevapla
Tenorshare coupon 22 Şubat 2024 - 20:49

You really make it appear so easy along with your presentation but I find this topic to be actually one thing which I feel I would never understand. It kind of feels too complex and extremely huge for me. I’m looking ahead on your next publish, I will try to get the dangle of it!

Cevapla
Zemits machine 23 Şubat 2024 - 12:04

I can’t express how much I value the effort the author has put into writing this exceptional piece of content. The clarity of the writing, the depth of analysis, and the plethora of information offered are simply impressive. Her enthusiasm for the subject is apparent, and it has undoubtedly resonated with me. Thank you, author, for sharing your insights and enlightening our lives with this extraordinary article!

Cevapla
Chay Campbell 25 Mart 2024 - 16:32

I really like and appreciate your blog article.Really looking forward to read more.

Cevapla
rodes wireless go 1 Nisan 2024 - 16:16

I enjoy the efforts you have put in this, thanks for all the great blog posts.

Cevapla
28b55a 2 Nisan 2024 - 07:26

An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers

Cevapla
Erec Prime 10 Nisan 2024 - 09:59

I have been surfing on-line more than 3 hours these days, but I by no means discovered any interesting article like yours. It is lovely price sufficient for me. In my view, if all webmasters and bloggers made excellent content as you probably did, the internet might be a lot more useful than ever before. “No one has the right to destroy another person’s belief by demanding empirical evidence.” by Ann Landers.

Cevapla
professional cell phone hackers 11 Nisan 2024 - 07:57

Yeah bookmaking this wasn’t a risky conclusion great post! .

Cevapla
Kerabiotics review 14 Nisan 2024 - 13:02

You are a very bright individual!

Cevapla
Ikaria lean belly juice reviews 14 Nisan 2024 - 13:13

Hello, you used to write great, but the last few posts have been kinda boring… I miss your tremendous writings. Past several posts are just a little out of track! come on!

Cevapla

Yorum Yap

Bu web sitesi deneyiminizi geliştirmek için çerezler kullanmaktadır. Kabul Et Tamamını Oku

Gizlilik Politikası