Tarmoqlanuvchi algoritm Masala. Ikkita butun sonni kiriting va ulardan kattasini ekranga chiqaring.
Yechish g‘oyasi: Ekran birinchi son chiqishi kerak, agarda u ikkinchi sondan katta bo‘lsa, yoki ikkinchi son chiqishi kerak, agarda u birinchi sondan katta bo‘lsa.
1-variant. Blok - sxema boshlash
max = a;
kiritish a,b
chiqarish max
a > b?
max = b;
tamom
ha
yo‘q
Tarmoqning to‘liq ko‘rinishi
«shart» blogi
Agar a = b? ?
`
1-вариант. Дастур #include int main() { int a, b, max; cout<<«Ikkita butun sonni kiriting\n"); cin>>a>>b ); if (a > b) {
} else {
}
cout<<“Katta son”<< max; return 0; } max = a;
max = b;
Shartli operatorning to’liq ko’rinishi
Shartli operator if ( ifoda )
{ // u nolga teng bo’lmasa (yoki true
//qiymatga ega bo’lsa) bajariladi
}
else
{ // u nolga teng bo’lsa (yoki false
//qiymatga ega bo’lsa) bajariladi
}
O‘ziga xosligi:
Ikkinchi (else …) qism qatnashmasligi mumkin (to‘liqmas shakl)
Xatolar qayerda? if a > b {
a = b;
}
else
b = a;
if ( a > b ) {
a = b;
else
b = a;
if ( a > b )
else
b = a;
if ( a > b )
a = b;
c = 2*a;
else
b = a;
}
(
)
}
{
a = b;
2-variant. Blok-sxema Tarmoqning to‘liqmas shakli
Boshlash
max = a;
kiritish a,b
chiqarish max
max = b;
tamom
ha
yo q
b > a?
2a- variant. Dastur #include int main() { int a, b, max; cout<<«Ikkita butun sonni kiriting\n"); cin>>a>>b ); max = a; if (b > a)
cout<<“Katta son”<< max; return 0; } max = b;
Shart operatorining to’liq ko’rinishi
2b- variant. Dastur #include int main() { int a, b, max; cout<<«Ikkita butun sonni kiriting\n"); cin>>a>>b ); max = b; if (a > b)
cout<<“Katta son”<< max; return 0; } max = a;
Shart operatorining to’liq ko’rinishi
Misollar if (a<0) b = 1;
else tarmog’i yo’q. Bunday konstrusiya “operatorni o’tkazishi” deb ataladi, chunki qiymat berish shartning bajarilmasligiga bog’liq bajariladi yoki o’tkazib yuboriladi.
if (ad | | a==0)) b++; else {b *= a; a = 0;}
ifoda rost bo’ladi, agar bir vaqtning aMisollar if (aelse {if (boperator uchta o’zgaruvchidan eng kichik qiymatni hisoblaydi. Katta qavslar bu holda shart emas, chunki kompilyator else qismini eng yaqin if ga o’tkazadi.
if (ad | | a==0)) b++; else {b *= a; a = 0;}
ifoda rost bo’ladi, agar bir vaqtning aswitch operatori
switch (o’tkazgich) operatori bir nechta yo’nalishlarda hisoblashlar jarayonini tarmoqlash uchun mo’ljallangan. Operatorning tuzilish sxemasi rasmda keltirilgan.
switch operatori
switch (ifoda) {
case qiymat1: operator 1; break;
case qiymat2: operator 1; break;
…
case qiymatN: operator N; break;
default:
operator N+1; break;;
}
Misol (dastur 4 amalli oddiy kalkulyatorni amalga oshiradi):
#include int main(){
int a, b, res;
char op;
cout << "\n Birinchi operandni kiriting : "; cin >> a;
cout << "\n Amal belgisini kiriting : "; cin >> op;
cout << "\n Ikkinchi operandni kiriting: "; cin >> b;
bool f = true;
switch (op) {
case '+' : res = a + b; break;
case '-' : res = a – b; break;
case '*' : res = a * b; break;
case '/' : res = a / b; break;
default : cout <<"\nNoma'lum amal"; f = false;
}
if (f) cout << "\nNatija : " << res;
return 0;
}