Skip to content

Go dasturlash tili: if, for, va switchga misollari

if operatoriga oid misollar

// 1. Sonning musbat yoki manfiyligini tekshirish
if number > 0 {
    fmt.Println("Musbat")
} else {
    fmt.Println("Manfiy")
}

// 2. Juft yoki toq son
if number%2 == 0 {
    fmt.Println("Juft")
} else {
    fmt.Println("Toq")
}

// 3. Tenglikni tekshirish
if a == b {
    fmt.Println("Teng")
}

// 4. Yoshi kattami
if age >= 18 {
    fmt.Println("Balog'atga yetgan")
} else {
    fmt.Println("Hali balog'atga yetmagan")
}

// 5. Harorat sovuqmi
if temperature < 0 {
    fmt.Println("Sovuq")
}

// 6. Toifa aniqlash
if score >= 90 {
    fmt.Println("A")
} else if score >= 80 {
    fmt.Println("B")
} else {
    fmt.Println("C yoki past")
}

// 7. Oddiy if bilan massivdan qidirish
if len(array) > 0 {
    fmt.Println("Massiv bo'sh emas")
}

// 8. O'zgarmaslarni solishtirish
if constant1 != constant2 {
    fmt.Println("O'zgarmaslar teng emas")
}

// 9. Foydalanuvchi tizimga kirganmi
if isLoggedIn {
    fmt.Println("Tizimga kirilgan")
} else {
    fmt.Println("Tizimga kirmagan")
}

// 10. Mantiqiy if
if x > 10 && y < 20 {
    fmt.Println("Shart bajarildi")
}

for operatoriga oid misollar

// 1. Oddiy sikl
for i := 0; i < 10; i++ {
    fmt.Println(i)
}

// 2. Massivni aylanib chiqish
array := []int{1, 2, 3}
for _, value := range array {
    fmt.Println(value)
}

// 3. Sonlarni yig'indisini topish
sum := 0
for i := 1; i <= 5; i++ {
    sum += i
}
fmt.Println(sum)

// 4. Cheksiz sikl
for {
    fmt.Println("Bu cheksiz sikl")
    break
}

// 5. Shartli sikl
count := 0
for count < 5 {
    fmt.Println(count)
    count++
}

// 6. Massivning indekslari bilan ishlash
for i := range array {
    fmt.Println(i)
}

// 7. Qatorni aylantirish
for _, ch := range "salom" {
    fmt.Printf("%c\n", ch)
}

// 8. Ikki o'lchovli massiv
matrix := [][]int{{1, 2}, {3, 4}}
for _, row := range matrix {
    for _, col := range row {
        fmt.Println(col)
    }
}

// 9. Even raqamlarni chiqarish
for i := 0; i <= 10; i += 2 {
    fmt.Println(i)
}

// 10. Continue operatori
for i := 1; i <= 5; i++ {
    if i == 3 {
        continue
    }
    fmt.Println(i)
}

switch case operatoriga oid misollar

// 1. Foydalanuvchi darajasini aniqlash
switch userLevel {
case "admin":
    fmt.Println("Administrator")
case "user":
    fmt.Println("Oddiy foydalanuvchi")
default:
    fmt.Println("Noma'lum daraja")
}

// 2. Son juftmi yoki toq
switch number % 2 {
case 0:
    fmt.Println("Juft")
default:
    fmt.Println("Toq")
}

// 3. Haroratni baholash
switch {
case temperature > 30:
    fmt.Println("Issiq")
case temperature > 0:
    fmt.Println("Iliq")
default:
    fmt.Println("Sovuq")
}

// 4. Rang tanlash
switch color {
case "qizil":
    fmt.Println("Rang qizil")
case "yashil":
    fmt.Println("Rang yashil")
default:
    fmt.Println("Noma'lum rang")
}

// 5. Haftaning kunlari
switch day {
case 1:
    fmt.Println("Dushanba")
case 7:
    fmt.Println("Yakshanba")
default:
    fmt.Println("Boshqa kun")
}

// 6. Oddiy qiymat taqqoslash
switch x {
case 10:
    fmt.Println("x = 10")
case 20:
    fmt.Println("x = 20")
default:
    fmt.Println("x noma'lum qiymatga teng")
}

// 7. Vaqt oralig'ini aniqlash
switch {
case hour < 12:
    fmt.Println("Ertalab")
case hour < 18:
    fmt.Println("Kunduzi")
default:
    fmt.Println("Kechasi")
}

// 8. Ruxsatlarni tekshirish
switch hasPermission {
case true:
    fmt.Println("Ruxsat bor")
default:
    fmt.Println("Ruxsat yo'q")
}

// 9. Foydalanuvchi yoshi
switch age {
case 18:
    fmt.Println("Voyaga yetgan")
case 65:
    fmt.Println("Pensiya yoshi")
default:
    fmt.Println("Oraliq yosh")
}

// 10. Numeratsiyani solishtirish
switch number {
case 1, 2, 3:
    fmt.Println("1 yoki 2 yoki 3")
default:
    fmt.Println("Boshqa qiymat")
}

if, for va switch case birgalikda ishlatilgan misollar

// 1. Massivdagi elementni topish
for _, value := range array {
    if value == target {
        switch value {
        case 1:
            fmt.Println("Birlik")
        case 2:
            fmt.Println("Ikilik")
        default:
            fmt.Println("Boshqa qiymat")
        }
    }
}

// 2. Qo'shma tekshirish
for i := 0; i < 10; i++ {
    if i%2 == 0 {
        switch {
        case i == 4:
            fmt.Println("To'rt")
        default:
            fmt.Println("Juft son")
        }
    }
}

// 3. Harorat monitoringi
for _, temp := range temperatures {
    if temp > threshold {
        switch {
        case temp > 30:
            fmt.Println("Issiq")
        default:
            fmt.Println("Normal harorat")
        }
    }
}

// 4. Loginni tekshirish
for _, user := range users {
    if user.isLoggedIn {
        switch user.role {
        case "admin":
            fmt.Println("Admin foydalanuvchi")
        default:
            fmt.Println("Oddiy foydalanuvchi")
        }
    }
}

// 5. Hisoblash
for i := 1; i <= 10; i++ {
    if i%3 == 0 {
        switch i {
        case 6:
            fmt.Println("Olti")
        default:
            fmt.Println("Boshqa uchga bo'linuvchi")
        }
    }
}

// 6. Vaziyatni aniqlash
for _, event := range events {
    if event.status == "active" {
        switch event.type {
        case "alert":
            fmt.Println("Ogohlantirish")
        default:
            fmt.Println("Boshqa tur")
        }
    }
}

// 7. Xaridlar ro'yxati
for _, item := range shoppingList {
    if item.quantity > 0 {
        switch item.name {
        case "apple":
            fmt.Println("Olma")
        default:
            fmt.Println("Boshqa mahsulot")
        }
    }
}

// 8. Chegarani aniqlash
for i := 0; i <= 20; i++ {
    if i > 10 {
        switch {
        case i > 15:
            fmt.Println("15 dan katta")
        default:
            fmt.Println("10 dan katta")
        }
    }
}

// 9. Shartli xatoliklar
for _, err := range errors {
    if err != nil {
        switch err.code {
        case 404:
            fmt.Println("Topilmadi")
        default:
            fmt.Println("Xato")
        }
    }
}

// 10. Ko'p qatorli qiymatlarni tekshirish
for _, row := range matrix {
    if len(row) > 0 {
        for _, col := range row {
            switch col {
            case 0:
                fmt.Println("Nol")
            default:
                fmt.Println("Qiymat mavjud")
            }
        }
    }
}