Bir Deyim ve İfade Olarak if
Tek bir biçimde dallanıp değer döndürün.
Bir Deyim ve İfade Olarak if, CoddyKit'te ücretsiz bir Zig Academy dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Zig Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Zig Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Branching with if
Use if to choose between paths. Zig runs the first block when the condition is true and skips it otherwise. Simple and explicit. 🌿
if (score > 50) {
std.debug.print("Pass\n", .{});
}Conditions Must Be bool
The condition in parentheses must be a real bool. Zig refuses integers or pointers here, so there is no truthy-or-falsy guessing.
Adding an else
Pair if with else to handle the false case. Exactly one of the two blocks runs, never both and never neither.
if (ready) {
start();
} else {
wait();
}Chaining else if
Stack tests with else if to pick one branch from several. Zig checks them top to bottom and stops at the first match.
if (n < 0) {
sign = -1;
} else if (n > 0) {
sign = 1;
} else {
sign = 0;
}if Is an Expression
In Zig, if can also produce a value. Both branches return something, and the whole if becomes that result you can assign.
const max = if (a > b) a else b;Both Branches Must Match
When if yields a value, both arms must share one type. The compiler reconciles them so the result has a single, predictable type.
No Ternary Needed
Zig has no ternary operator. The expression form of if fills that role cleanly, keeping just one way to write a choice.
Unwrapping an Optional
An if can test and unwrap an optional in one move. The captured name holds the inner value only when it is present.
if (maybe_user) |user| {
greet(user);
}Capturing Errors with if
An if can also peel apart an error union. The else branch can capture the error value when the expression fails.
if (parse(text)) |value| {
use(value);
} else |err| {
report(err);
}Braces Are Required
Zig always needs braces around if bodies, even for a single line. This removes a whole class of dangling-statement bugs.
Returning Early
A common pattern is an early return inside an if. Handle the special case first, then let the rest of the function read straight down.
if (list.len == 0) return;Quick Check
You want max to hold the larger of a and b in one line. Which Zig form works?
Recap
You met if as both a statement and an expression: bool conditions, else and else if, value-returning branches, and optional or error capturing. 🎯
Sıkça Sorulan Sorular
“Bir Deyim ve İfade Olarak if” dersi ücretsiz mi?
Evet — “Bir Deyim ve İfade Olarak if” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Zig Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Zig Academy kursu toplamda 4 dersten oluşur.
“Bir Deyim ve İfade Olarak if” dersinde ne öğreneceğim?
Tek bir biçimde dallanıp değer döndürün. Zig Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Zig Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Zig Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.
“Bir Deyim ve İfade Olarak if” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Zig Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Zig Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Bir Deyim ve İfade Olarak if
- while Döngüleri ve continue İfadeleri
- Aralıklar ve Öğeler Üzerinde for Döngüleri
- break, continue ve Etiketli Döngüler