Professional Documents
Culture Documents
Teknik
Üniversitesi
• www.cppreference.com
İçerik
Kontrol ifadeleri, iki nesneyi karşılaştırmak için ilişki operatörlerini kullanır. C ++
'da aşağıdaki gibi altı ilişkisel operatör vardır:
Example:
if ( b != 0 ) c = a/b;
Y
b0 ? c = a/b
control structure using
N
a relational operator
İlişkisel Operatörler
Bileşik ilişki ifadeleri mantıksal operatörler kullanılarak oluşturulabilir:
Example:
if ( b != 0 && a > 0 ) c = a/b;
b0 Y
and a>0 c = a/b
control structure using a
compound relational operator N
Mantıksal operatörler
b! = 0 gibi ilişkisel bir işlemin sonucu ya doğru ya da yanlıştır; seçim yapısında
c'nin atanması eğer (b! = 0) c = a / b; yalnızca b! = 0 doğruysa oluşur.
The if structure
İkinci dereceden bir denklemin köklerini bulmak için bir program yazın. A, b ve c
katsayıları kullanıcı tarafından girilmelidir. Program aşağıdaki üç koşulu kontrol
etmeli ve ilgili çıktıları vermelidir:
Örnek
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c, disc, x1, x2;
cout << "input coefficients \n";
cin >> a >> b >> c;
disc = b*b-4. * a * c;
if ( disc > 0.0 ) {
cout<< "there are two real roots!\n";
x1=(-b+sqrt(disc))/(2.*a);
x2=(-b-sqrt(disc))/(2.*a);
cout<<"first root is "<< x1<<endl;
cout<<"second root is "<< x2<<endl;
}
if ( disc == 0.0 ) {
cout<< "there is one real root!\n";
x1=x2=-b/(2.*a);
cout<<"first root is "<< x1<<endl;
cout<<"second root is "<< x2<<endl;
}
if ( disc < 0.0 ) cout<< "No real roots!\n";
system("pause"); }
Çözüm
The if..else structure allows both outcomes of a selection to be defined.
F T
x+y 0 c = 1 / (x+y)
“c is undefined!” “c = ” c
A complete example program using the if else structure.
#include <iostream>
#include <cmath> start
using namespace std;
int main() { Input a
double a, b, c;
cin >> a; b=a+2
b = a + 2.0;
F T
if ( a > 2.0 ) { c = b5 a >2 c = ln(a+b)
c = log(a+b);
} else {
c = pow(b,5.0); Output a, c
}
end
cout << a << " " << c << endl;
}
More levels of selection can be added with the else if statement.
The general form is:
int classCode;
if (condition 1) { cout << "Enter the class code: ";
cin >> classCode;
statements1
if (classCode==1)
. cout << "Freshman" << endl;
} else if else if (classCode==2)
(condition 2) { cout << "Sophmore" << endl;
statements2 else if (classCode==3)
. cout << "Junior" << endl;
Example:
max = ( x > y ) ? x : y;
is equivalent to
if ( x > y )
max = x;
else
max = y;
The ? Operator