C / C++ Courses ? 7 (Review and Continue with If Statements)
Let?s start with an example to remember previous course.
Write a program that enters a final grade of student and prints if the student passed or failed. (40 and above passed)
#include<stdio.h>
int main(){
int grade;
printf(?Enter the grade: ?);
scanf(?%d?,&grade);
if(grade>=40)
printf(?Passed.?);
if(grade<40)
printf(?Failed.?);
return 0;
}
If we write
if(1)
printf(?Hello!?);
It will accept this as true. Any number which is nonzero will be accepted true. Just
if(0)
printf(?Hello?); //won?t print anyhing on the screen.
*An Extreme Example
Compare both of the below.
1-What is the output of this?
int x=8; y=5;
if(x==y)
printf(?%d is equal to %d.?,&x,&y);
This will print nothing.
2-What is the output of this?
int x=8; y=5;
if(x=y)
printf(?%d is equal to %d.?,&x,&y);
This condition is ?x=y?. This is not comparing them. Just assigning y to x. It makes x?s value 5.
if(x=y) means if(5). And 5 is nonzero, so this is true. And it will print ?5 equals 5? on the screen.
What if we make the values like that?
int x=8, y=0;
if(x=y)
printf(?%d is equal to %d.?,&x,&y);
It will make x?s value 0. İf(0) means it is false. Therefore, it will print nothing.
Ex:Write a program that allows the user to enter an integer. This program will print whether the integer is even or odd.
#include<stdio.h>
int main(){
int x;
printf(?Enter an integer: ?);
scanf(?%d?,&x);
if(x%2==0)
printf(?It?s even.?)
if(x%2==1)
printf(?It?s odd.?);
return 0;
}
Let?s make a different example:
Find the output of them. Assume that i=1, j=2, k=3.
printf(?%d?, i==1); // 1
printf(?%d?, k<j); //0
printf(?%d?, k!=4); //1
printf(?%d?, i%2==1); // 1
The results of them must be 1 or 0. Because all of them is condition, they can be true or false, not anything else.
If/else statement
if ( condition )
statement1;
else
statement2;
If if is true, else is ignored. Let?s cover this part with an example.
Ex: Write a program that allows the user to enter the final grade of a student.
If the student?s grade is larger than or equal to 50, then;
1-assign a character variable the letter ?A?,
2-print the letter and the word ?Passed?.
If not, then;
1-assign the character variable the letter ?F?,
2-print the letter and the word ?Failed?.
#include<stdio.h>
int main(){
int grade;
char letter;
printf(?Enter the grade: ?);
scanf(?%d?,&grade);
if(grade>=50){
letter=?A?;
printf(?You passed with the letter %c.?, letter);
}
else{
letter=?F?;
printf(?You failed with the letter %c.?, letter);
}
return 0;
}
If grade is 90, it won?t execute else.
If grade is 10, it will execute if, then it will execute also else part.
Nested if/else statements
Ex:Write a program that allows the user to enter an integer. Find and print if the integer is positive or negative or equal.
#include<stdio.h>
int main(){
int num;
printf(?Enter a number: ?);
scanf(?%d?,&num);
if(num==0)
printf(?Number is equal to zero.?);
else{
if(num<0)
printf(?Number is negative?);
else
printf(?Number is positive?);
}
return 0;
}
For this example, no need to add brackets for first else. So we use this type of statements like that:
if(num==0)
printf(??…?);
else if(num<0)
printf(???.?);
else if(num<5)
printf(???.?);
else if(num<19)
printf(???.?);
else //this else part is for the other conditions except conditions above.
printf(????);
»Extra:
Answer is in the following lesson;)
?Answer of previous lesson’s extra:
We have talked about this. If condition is nonzero, then it is accepted as true. So it will print
“Yes, this is true”
on the screen.
See you soon ! 🙂
[…] Answer is in the following lesson […]
Bayağı güzel dersler, eline sağlık. Bir de ingilizcemiz de bu esnada gelişiyor. Çünkü, çok sade bir ve yeterli bir anlatım tarzı. Bayramınız kutlu olsun.
teşekkür ederim senin de bayramın kutlu olsun 🙂
dersleri begenmem hoşuma gitti, inşallah faydam olmuştur:)
ben üniversite okumadım ama bunlar çok kolay bana …
aferin o zaman …
Yoruma Açığız! :)
Kategoriler
Son Yorumlar
Arşivler
Bulutumdan Seç Beğen Al :)
bilgisayar mühendisliği oryantasyon bilm 100 hakkında c/c++ ders notları c/c++ training courses can yücel sözleri şiirleri can yücel şiirleri c courses c dersleri Ceng 100 / Bilm 100 Courses c hakkında örnekler program yazma c programlama dersleri c programming courses examples güzel sözler Güzel Yazılar Hz.Mevlana Mevlana sözleri yusuf ile züleyha sözleri Ziynet'in kaleminden yazılar Ziynet Nesibe Sözleri züleyhadan yusufa İskender Pala sözleri
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
En Çok Görüntülenenler
Son Yazılar
En Çok Yorumlananlar
En Çok Görüntülenenler