2. ! 34
âĒ! āļāļąāļ§āļāļĒïāļēāļāđāļāļĢāđāļāļĢāļĄ test_if1.cpp āđāļŠāļāļāļāļēāļĢāļĢāļąāļāļïāļāļĄāļđāļĨāļāđāļēāļāļ§āļāđāļāđāļĄ 2 āļāļēāļāļ§āļ āđāļĨāļ°āđāļāļĢāļĩāļĒāļāđāļāļĩāļĒāļ
āđ
āļāļąāļāđāļāļĒāđāļï if āđāļāļāļāļĢāļ°āđāļĒāļ(statement) āđāļāļĒāļ§
āļĩ
/*Program : test_if1.cpp
Process : copare 2 integer
*/
#include <iostream.h>
#include <conio.h>
void main()
{ int x,y;
int result = 100;
//start statement
clrscr();
cout<< "Please enter integer number : n";
cout<< "Finst number : ";
cin>>x;
cout<< "nSecond number : ";
cin>>y;
//condition of if
if (x>y) //condition
cout<<"nFirst number is greater than second numberan";
if (x+y>result) //condition
cout<<"nFirst number + Seconde number is greater than an"<<result;
getch();
}
âĒ! āđāļāļĢāđāļāļĢāļĄ test_if2.cpp āđāļŠāļāļāļāļēāļĢāđāļāļĢāļĒāļāđāļāļĒāļāļāđāļēāļāļ§āļāļāļąāļ§āđāļĨāļ 2 āļāļēāļāļ§āļ āđāļĨāļ°āđāļï if āđāļāļ
āļĩ āļĩ āđ
āļŦāļĨāļēāļĒ āļāļĢāļ°āđāļĒāļ(Compound statement)
/*Program : test_if2.cpp
Process : copare 2 integer*/
#include <iostream.h>
#include <conio.h>
void main()
{ int x,y;
int result = 100;
//start statement
clrscr();
cout<< "Please enter integer number : n";
cout<< "Finst number : ";
cin>>x;
cout<< "nSecond number : ";
cin>>y;
//condition of if
āļĻāļīāļĢāļīāļāļąāļĒ āļāļēāļĄāļāļļāļĢāļĩ āļāļēāļĢāđāļĨāļ·āļāļāļāđāļē
3. ! 35
if (x>y)
{ clrscr();
cout<<"nFirst number is : "<<x;
cout<<"nSecond number is : "<<y;
cout<<"nFirst number is greater than second numberan";
}
if (x+y<=result)
{ clrscr();
cout<<"nFirst number is : "<<x;
cout<<"nSecond number is : "<<y;
cout<<"nFirst number + Seconde number <= an"<<result;
}
getch();
}
âĶ! āļēāļĢāđāļĨāļāļāļāđāļēāđāļāļ ifâĶelse
āļ āļ·
āļāļēāļĢāđāļĨāļ·āļāļāļāđāļēāđāļāļ if âĶelse āļĄāļĩāļŦāļĨāļąāļāļāļēāļĢāļāļēāļāļēāļ āļāļāđāļĢāļĄāļāļ§āļĒāļāļēāļĢāļāļāļŠāļāļāđāļāļāļāđāļāļāļāđāļēāļŦāļāļāđāļ§ï āļïāļēāļāļĨ
āđ āļ· āđāļī ï āđāļ· āđāļĩ
āļāļēāļĢāļāļĢāļ§āļāļŠāļāļāđāļāļāļāđāļ āđāļāļāļāļāļāđāļĩ
āđāļ· ï āļą
- āđāļïāļāļāļĢāļīāļ statement āļāļĩāđāļāļĒāļđïāļïāļāļāļēāļ if āļāļ°āļāļđāļāļāđāļēāļāļēāļ
- āđāļïāļāđāļāđāļ statement āļāļĩāđāļāļĒāļđïāļïāļāļāļēāļ else āļāļ°āļāļđāļāļāđāļēāļāļēāļ
āļĢāļāđāļāļāļāđāļĩ 1 ifâĶelse āđāļāļ statement āđāļāļĩāļĒāļ§ āļĄāļĩāļĢāļđāļāđāļāļ āļāļąāļāļāļĩāđ
āļđ
if(āđāļāļ·āļāļāđāļāļāļēāļĢāđāļāļĢāļĩāļĒāļāđāļāļĩāļĒāļ)
āđ
statement;
else
statement;
āļĢāļāđāļāļāļāđāļĩ 2 ifâĶelse āđāļāļāļŦāļĨāļēāļĒ statement āļĄāļĩāļĢāļđāļāđāļāļ āļāļąāļāļāļĩāđ
āļđ
if(āđāļāļ·āļāļāđāļāļāļēāļĢāđāļāļĢāļĩāļĒāļāđāļāļĩāļĒāļ)
āđ
{ statement;
statement;
statement;
}
else
{ statement;
statement;
! }
āļĻāļīāļĢāļīāļāļąāļĒ āļāļēāļĄāļāļļāļĢāļĩ āļāļēāļĢāđāļĨāļ·āļāļāļāđāļē
4. ! 36
âĒ! āļāļąāļ§āļāļĒïāļēāļāđāļāļĢāđāļāļĢāļĄ test_if3.cpp āđāļŠāļāļāļāļēāļĢāđāļï ifâĶelse āđāļāļ statement āđāļāļĒāļ§
āļĩ
/*Program : test_if3.cpp
Process : copare 2 integer and use if...else one statement
*/
#include <iostream.h>
#include <conio.h>
void main()
{ int x,y;
//start statement
clrscr();
cout<< "Please enter integer number : n";
cout<< "Finst number : ";
cin>>x;
cout<< "nSecond number : ";
cin>>y;
if (x>y) //condition of if
cout<<"nFirst number is greater than second numberan";
else
cout<<"nSecond number is greater than or equal first numberan";
getch();
}
âĒ! āļāļąāļ§āļāļĒïāļēāļāđāļāļĢāđāļāļĢāļĄ test_if4.cpp āđāļŠāļāļāļāļēāļĢāđāļï ifâĶelse āđāļāļāļŦāļĨāļēāļĒ statement
/*Program : test_if3.cpp
Process : copare 2 integer and use if...else
*/
#include <iostream.h>
#include <conio.h>
void main()
{ int x,y;
//start statement
clrscr();
cout<< "Please enter integer number : n";
cout<< "Finst number : ";
cin>>x;
cout<< "nSecond number : ";
cin>>y;
//condition of if
if (x>y)
āļĻāļīāļĢāļīāļāļąāļĒ āļāļēāļĄāļāļļāļĢāļĩ āļāļēāļĢāđāļĨāļ·āļāļāļāđāļē
5. ! 37
{ clrscr();
cout<<"You enter 2 number :";
cout<<"nFirst number is : "<<x;
cout<<"nSecond number is : "<<y;
cout<<"nFirst number is greater than second numberan";
getch();
}
else
{ clrscr();
cout<<"You enter 2 number :";
cout<<"nFirst number is : "<<x;
cout<<"nSecond number is : "<<y;
cout<<"nSecond number is greater than or equal first numberan";
}
getch();
}
âĶ! āļēāļĢāđāļĨāļāļāļāđāļēāđāļāļ if āļāļāļāļāļ (nested if)
āļ āļ· ï āļą
āđāļĢāļēāļŠāļēāļĄāļēāļĢāļāđāļï ifâĶelse if āļïāļāļāļāļąāļ āđāļāļāļāļĢāļ§āļāļŠāļāļāđāļāļāļāđāļāđāļāđāļāļĢāđāļāļĢāļĄāđāļŦāļĄāļāļēāļāđāļĨāļāļāļŦāļĨāļēāļĒ āđ
āđāļ· āđāļ· ï āļĩ āļ·
āļāļēāļāđāļï āļĄāļĢāļāđāļāļāļāļāļāđāļĩ
āļĩāļđ āļą
if (āđāļāļ·āļāļāđāļāļāļĩāđ 1)
āđ
! { statement;
statement;
}
else if (āđāļāļ·āļāļāđāļāļāļĩāđ 2)
āđ
{ statement;
statement;
! }
! else if (āđāļāļ·āļāļāđāļāļāļĩāđ 3)
āđ
! { statement;
! statement;
! }
! else
! { statement;
âĒ! āļāļąāļ§āļāļĒïāļēāļāđāļāļĢāđāļāļĢāļĄ grade1.cpp āđāļŠāļāļāļāļēāļĢāļāđāļēāļāļ§āļāđāļāļĢāļ āđāļāļĒāļāļēāļĢāļāļĢāļāļāļāļ°āđāļāļāļĢāļ§āļĄāļāļēāļ
! statement;
āļāļĩāļĒïāļāļāļĢïāļ āđāļāļĒāļĄāļĩāđāļāļ·āđāļāļāđāļāļāļēāļĢāļāļąāļāđāļāļĢāļāļāļēāļāļāļ°āđāļāļāļĢāļ§āļĄ āļāļąāļāļāļĩāđ
! }
āļĻāļīāļĢāļīāļāļąāļĒ āļāļēāļĄāļāļļāļĢāļĩ āļāļēāļĢāđāļĨāļ·āļāļāļāđāļē
6. ! 38
āļāļ°āđāļāļāļĢāļ§āļĄ 0-49 āđāļïāđāļāļĢāļ F
āļāļ°āđāļāļāļĢāļ§āļĄ 50-59 āđāļïāđāļāļĢāļ D
āļāļ°āđāļāļāļĢāļ§āļĄ 60-69 āđāļïāđāļāļĢāļ C
āļāļ°āđāļāļāļĢāļ§āļĄ 70-79 āđāļïāđāļāļĢāļ B
āļāļ°āđāļāļāļĢāļ§āļĄ 80-100 āđāļïāđāļāļĢāļ A
āļāļ°āđāļāļāļāļ āđ (āđāļĄïāļāļđāļāļïāļāļ)
āđāļ· āđāļïāđāļāļĢāļ *
āđāļāļĒāđāļŠāļāļāļāļēāļĢāđāļï ifâĶelseâĶif āđāļāļ statement āđāļāļĒāļ§ āļāļąāļāļāļĩāđ
āļĩ
/*Program: grade1.cpp
Process: calculate grade from total score*/
#include <iostream.h>
#include <conio.h>
void main()
{ int score;
char grade;
//begin statement
clrscr();
cout<< "Program calculate grade";
cout<< "nnPlease enter your score : ";
cin>>score; //input score
if (score<0) //calculate grade use if...else if..
grade='*';
else if (score<=49)
grade='F';
else if(score<=59)
grade='D';
else if(score<=69)
grade='C';
else if(score<=79)
grade='B';
else if(score<=100)
grade='A';
else
grade='*';
cout<< "You get grade : a"<<grade<<'n'; //show get grade
if (grade=='*')
cout<< "Your score = "<<score<< " is error range !!!n"; getch();
}
āļĻāļīāļĢāļīāļāļąāļĒ āļāļēāļĄāļāļļāļĢāļĩ āļāļēāļĢāđāļĨāļ·āļāļāļāđāļē
7. ! 39
âĒ! āļāļąāļ§āļāļĒïāļēāļāđāļāļĢāđāļāļĢāļĄ grade2.cpp āđāļŠāļāļāļāļēāļĢāļāđāļēāļāļ§āļāļāļēāļĢāļāļąāļāđāļāļĢāļāđāļāļĒāđāļï Logical Operator āđāļï
āđāļï || (OR) && (AND) āļĄāļēāđāļïāđāļāļāļēāļĢāļāđāļēāļŦāļāļāđāļāļāļāđāļ āļāļāļĄāļāļĨāļĨāļāļāļāļēāļĢāļāđāļēāļāļēāļāđāļŦāļĄāļ·āļāļ
āđāļ· āđāļķ āļĩ āļą ï
āđāļāļĢāđāļāļĢāļĄ grade1.cpp
/*Program: grade2.cpp
Process: calculate grade from total score
*/
#include <iostream.h>
#include <conio.h>
void main()
{ int score;
char grade;
//begin statement
clrscr();
cout<< "Program calculate grade";
cout<< "nnPlease enter your score : ";
cin>>score; //input score
//calculate grade use if...else if..
if (score<0 || score>100) //check enter error score
grade='*';
else if (score>=0 && score<=49)
grade='F';
else if(score>=50 && score<=59)
grade='D';
else if(score>=60 && score<=69)
grade='C';
else if(score>=70 && score<=79)
grade='B';
else
grade='A'; // end of if command
cout<< "You get grade : a"<<grade<<'n'; //show get grade
if (grade=='*')
cout<< "Your score = "<<score<< " is error range !!!n";
getch();
}
āļĻāļīāļĢāļīāļāļąāļĒ āļāļēāļĄāļāļļāļĢāļĩ āļāļēāļĢāđāļĨāļ·āļāļāļāđāļē