You are on page 1of 16

İskenderun

Teknik
Üniversitesi

Bölüm: Havacılık ve Uzay Mühendisliği


Ders: Algoritmalar ve Programlama
Dönem: 2020 – 2021 (Bahar)
Öğretim üyesi: Dr. Öğr. Üyesi Ahmet ŞUMNU
• Prof. Dr. Bahattin Kanber „in ders notlarından
yararlanılmıştır.
• Prof. Dr. M. Serdar ÇELEBİ, İTÜ
(https://web.itu.edu.tr/~mscelebi/mscfiles/Lecture1.pdf)
• www.cplusplus.com

• www.cppreference.com

Ders Notu Hazırlanırken Yararlanılan


Kaynaklar
İlişkisel ve mantıksal operatörler
• Relational and logical operators
Boolean İfadeler
• Boolean Expressions
• The if and if ... else structures If and if ... else yapıları
• The ? Operator ? Operatörü
• Example solved problems Örnek çözülmüş problemler

İç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
b0 ? 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;

b0 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.

1 double x=1.3, y=2.7, c=0.;


2 if ( x > y) cout << "x is greater than y." << endl;
3 if ( y > 0. ) cout << x/y << endl;
4 if ( x+y != 0. ) c = 1/(x+y);
5 cout << "c = " << c << endl;

Output Note that there is no output from line 2


0.481481 because the relation ( x > y) is false.
c = 0.25
Doğru veya yanlış olarak değerlendirilen ifadeler Boolean İfadeleri olarak adlandırılır.
Boolean ifadelerini kontrol ifadeleri içinde (önceki sayfa) veya aşağıdaki gibi atamalar
şeklinde oluşturabiliriz:

int x=1, y=2, s; Note that variables u, z, t, and w are


bool u, z = true, t, w; declared as type bool and so can
s = 2 > 1; represent the states true and false.
u = x > 3;
z = x <= y && y > 0; Also literal constants true and false
t = y <= 0 || z; can be used in assignments and
w = !s; relational operations.

Results s = true since 2>1 (always).


u = false since 1>3 is false.
z = true since 1<=2 and 2>0 are both true.
t = true since z is true.
w = false since s is true, therefore its negation is false.
bool ok; variable ok can be true or false.
ok = y != 0.; ok is assigned true if y0 or false if y=0.
if ( ok ) c = x/y; c is assigned the result x/y if ok is true.

Integer represented of bools


integer 0 represents false
bool good = true; non-zero represents true
bool bad = false;
cout << true << endl; 1 Output
cout << false << endl; 0
cout << good << endl; 1
cout << bad << endl; 0
cout << (good || bad) << endl; 1
cout << (good && bad) << endl; 0
The if statement allows conditional execution; the general form is:
If condition is true then the block defined
if (condition) { by the braces {...} is executed.
statements
if ( x+y != 0. ) {
.
. c = 1/(x+y);
cout << "c = " << c << endl;
}
}

If statements is a single statement then the braces can be omitted:


if ( x+y != 0. )
if (condition) c = 1/(x+y);

statement ; cout << "c = " << c << endl;

c is assigned only if the condition is true. But, the


output statement will be executed in any case.

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:

İki gerçek kök var


Tek bir gerçek kök var
Kökler karmaşık.
İkinci dereceden denklemler ax 2  bx  c  0
b 
Kökler x1, 2 
2a
Diskriminant   b 2  4ac
∆> 0 İki gerçek kök var
∆= 0 Tek bir gerçek kök var
∆< 0 Kökler karmaşık

Ö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.

The general form is:

if (condition) { If condition is true then the first block is


executed, otherwise (false) the second block
statements1
is executed.
.
.
if ( x+y != 0. ) {
} else {
c = 1/(x+y);
statements2 cout << "c = " << c << endl;
. } else {
.
cout << "c is undefined! " << endl;
}
}

The if .. else structure


if ( x+y != 0. ) {
c = 1/(x+y);
cout << "c = " << c << endl;
} else {
cout << "c is undefined! " << endl;
}

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;

} else { else if (classCode==4)


cout << "Graduate" << endl;
statements3
else
. cout << "Illegal class code." << endl;
}

The if .. else if .. else structure


The ? Operator (conditional expression operator) provides a concise form
of the if else structure.

The general form is:


( condition ) ? expression1 : expression2;
The value produced by this operation is either expression1 or expression2
depending on condition being true or false respectively.

Example:
max = ( x > y ) ? x : y;
is equivalent to
if ( x > y )
max = x;
else
max = y;

The ? Operator

You might also like