SlideShare a Scribd company logo
1 of 20
www.emust.edu.mn
Сэдэв: Нөхцөл шалгах
www.emust.edu.mn
Нөхцөл шалгах if
Программ зохиоход ч гэсэн цаг үргэлж сонголттой учирдаг.
Ямарваа нэгэн нөхцлийг шалгаад түүнийхээ үр дүнгээс
хамааран программын өөр өөр хэсгүүд рүү очих болно. C++-д
нөхцөл шалгах хэд хэдэн арга бий. IF оператор бол хамгийн
энгийн нөхцөл шалгах оператор юм.
Бичигдэх хэлбэр
if (нөхцөл)
{
// нөхцөл үнэн үед биелэгдэх үйлдлүүдийг бичнэ
}
www.emust.edu.mn
Нөхцөл шалгах if
Хэрэв нөхцөл үнэн бол if-ийн үндсэн хэсэг дэх үйлдлийг, худал
бол if хэсгийн их бие доторх үйлдлийг гүйцэтгэлгүй алгасна.
www.emust.edu.mn
Example 1: if statement
true
false
www.emust.edu.mn
Example 1: if statement
// Program to display a number if it is negative
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) { printf("You entered %d.n", number); }
printf("The if statement is easy.");
return 0;
}
Output 1
Enter an integer: -2
You entered -2.
The if statement is easy.
Output 2
Enter an integer: 5 The if statement is
easy.
www.emust.edu.mn
Нөхцөл шалгах if...else
Бичигдэх хэлбэр
if (нөхцөл)
{
//нөхцөл үнэн үед биелэгдэх үйлдлүүд
}
else
{
//нөхцөл хэдэл үед биелэгдэх үйлдлүүд
}
Хэрэв нөхцөл үнэн бол if-ийн үндсэн хэсэг дэх үйлдлийг, худал
бол if худал хэсгийн их бие доторх үйлдлийг гүйцэтгэнэ.
www.emust.edu.mn
Нөхцөл шалгах if...else
www.emust.edu.mn
Example 2: if...else statement
true
false
www.emust.edu.mn
Example 2: if...else statement
// Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0)
{ printf("%d is an even integer.",number); }
else { printf("%d is an odd integer.",number); }
return 0;
}
Output 1
Enter an integer: 7
7 is an odd integer.
Output 2
Enter an integer: 8
8 even integer."
www.emust.edu.mn
Нөхцөл шалгах if...else Ladder
If ... else хэллэг нь тестийн илэрхийлэл үнэн эсвэл худал
эсэхээс хамаарч хоёр өөр кодыг ажиллуулдаг. Заримдаа 2-
оос дээш боломжит хувилбараас сонголт хийх шаардлагатай
болдог. If ... else шат нь олон туршилтын илэрхийлэлүүдийн
хооронд шалгаж, янз бүрийн мэдэгдлийг гүйцэтгэх
боломжийг олгодог.
Бичигдэх хэлбэр
if (нөхцөл 1) {// үйлдэл }
else if(нөхцөл 2) {// үйлдэл }
else if (нөхцөл 3) { // үйлдэл }
. . else {// үйлдэл }
www.emust.edu.mn
Example 3: C if...else Ladder
// Program to relate two integers using =, > or < symbol
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2)
{ printf("Result: %d = %d",number1,number2); }
//checks if number1 is greater than number2.
www.emust.edu.mn
else if (number1 > number2)
{ printf("Result: %d > %d", number1, number2); }
//checks if both test expressions are false
else { printf("Result: %d < %d",number1, number2); }
return 0; }
Output
Enter two integers:
12 23
Result: 12 < 23
www.emust.edu.mn
Нөхцөл шалгах Nested if...else
if ... else нөхцөл дотор давхар if ... else ашиглах боломжтой.
Example 4: Nested if...else
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2)
{
if (number1 == number2)
{ printf("Result: %d = %d",number1,number2); }
www.emust.edu.mn
Нөхцөл шалгах Nested if...else
else
{ printf("Result: %d > %d", number1, number2); }
}
else { printf("Result: %d < %d",number1, number2); }
return 0;
}
www.emust.edu.mn
Нөхцөл шалгах Nested if...else
if ... else мэдэгдлийн үндсэн хэсэгт зөвхөн нэг үйлдэл байгаа
бол та хаалт {} ашиглах шаардлагагүй.
Жишээлбэл:
if (a > b)
{ print("Hello"); }
print("Hi");
if (a > b) print("Hello"); print("Hi");
www.emust.edu.mn
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
www.emust.edu.mn
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
www.emust.edu.mn
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
www.emust.edu.mn
#include <stdio.h>
int main()
{
int x, y;
printf("enter the value of x:");
scanf("%d", &x);
printf("enter the value of y:"); scanf("%d", &y);
if (x>y)
{
printf("x is greater than yn");
}
www.emust.edu.mn
if (x<y)
{
printf("x is less than yn");
}
if (x==y)
{
printf("x is equal to yn");
}
printf("End of Program");
return 0;
}

More Related Content

What's hot (20)

си хэл 10
си хэл 10си хэл 10
си хэл 10
 
Лекц №10
Лекц №10Лекц №10
Лекц №10
 
Лекц №12
Лекц №12Лекц №12
Лекц №12
 
Лекц №9
Лекц №9Лекц №9
Лекц №9
 
Лекц 7 (Давталтуудын Си хэлэнд)
Лекц 7 (Давталтуудын Си хэлэнд)Лекц 7 (Давталтуудын Си хэлэнд)
Лекц 7 (Давталтуудын Си хэлэнд)
 
массив
массивмассив
массив
 
Лекц №13
Лекц №13Лекц №13
Лекц №13
 
C++ vndsen oilgolt хичээл 1
C++ vndsen oilgolt хичээл 1C++ vndsen oilgolt хичээл 1
C++ vndsen oilgolt хичээл 1
 
Hylbar shugaman programmuud хичээл 4
Hylbar shugaman programmuud хичээл 4Hylbar shugaman programmuud хичээл 4
Hylbar shugaman programmuud хичээл 4
 
Unshih hewleh uildel хичээл 3
Unshih hewleh uildel хичээл 3Unshih hewleh uildel хичээл 3
Unshih hewleh uildel хичээл 3
 
For presentation
For presentationFor presentation
For presentation
 
Лекц №14
Лекц №14Лекц №14
Лекц №14
 
C lects (4)
C lects (4)C lects (4)
C lects (4)
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
SW206 labo
SW206 laboSW206 labo
SW206 labo
 
Massiv presentation
Massiv presentationMassiv presentation
Massiv presentation
 
While prst
While prstWhile prst
While prst
 
U.cs101 лаборатори 1
U.cs101 лаборатори 1U.cs101 лаборатори 1
U.cs101 лаборатори 1
 
Ci prog tolgoi file хичээл 2
Ci prog tolgoi file хичээл 2Ci prog tolgoi file хичээл 2
Ci prog tolgoi file хичээл 2
 
Лекц №16
Лекц №16Лекц №16
Лекц №16
 

Similar to Лекц №7 (19)

If presentation
If presentationIf presentation
If presentation
 
C lects (2)
C lects (2)C lects (2)
C lects (2)
 
Lecture2
Lecture2Lecture2
Lecture2
 
онол
онолонол
онол
 
Java laboratoriin udirdamj 2
 Java laboratoriin udirdamj 2 Java laboratoriin udirdamj 2
Java laboratoriin udirdamj 2
 
Ci hel
Ci helCi hel
Ci hel
 
баяраа сургалт
баяраа сургалтбаяраа сургалт
баяраа сургалт
 
Лабораторийн ажил № 2
Лабораторийн ажил № 2Лабораторийн ажил № 2
Лабораторийн ажил № 2
 
Лабораторийн ажил № 1
Лабораторийн ажил № 1Лабораторийн ажил № 1
Лабораторийн ажил № 1
 
Лаборатор-3
Лаборатор-3Лаборатор-3
Лаборатор-3
 
Video lab1-web
Video lab1-webVideo lab1-web
Video lab1-web
 
Lecture
LectureLecture
Lecture
 
C lects (1)
C lects (1)C lects (1)
C lects (1)
 
U.cs101 лаборатори 4
U.cs101 лаборатори 4U.cs101 лаборатори 4
U.cs101 лаборатори 4
 
Лекц 3
Лекц 3Лекц 3
Лекц 3
 
лекц-3
лекц-3лекц-3
лекц-3
 
Lecture3
Lecture3Lecture3
Lecture3
 
Lecture7
Lecture7Lecture7
Lecture7
 
Програмчлалын хэл
Програмчлалын хэлПрограмчлалын хэл
Програмчлалын хэл
 

More from Amarsaikhan Tuvshinbayar (11)

U.it101 lec2
U.it101 lec2U.it101 lec2
U.it101 lec2
 
U.it101 lec1
U.it101 lec1U.it101 lec1
U.it101 lec1
 
Лекц №15
Лекц №15Лекц №15
Лекц №15
 
Лекц №6
Лекц №6Лекц №6
Лекц №6
 
Лекц №5
Лекц №5Лекц №5
Лекц №5
 
Лекц №4
Лекц №4Лекц №4
Лекц №4
 
Лекц №3
Лекц №3Лекц №3
Лекц №3
 
Лекц №2
Лекц №2Лекц №2
Лекц №2
 
Лекц №1
Лекц №1Лекц №1
Лекц №1
 
U.IT101 homework 1
U.IT101 homework 1U.IT101 homework 1
U.IT101 homework 1
 
U.CS101
U.CS101U.CS101
U.CS101
 

Лекц №7

  • 2. www.emust.edu.mn Нөхцөл шалгах if Программ зохиоход ч гэсэн цаг үргэлж сонголттой учирдаг. Ямарваа нэгэн нөхцлийг шалгаад түүнийхээ үр дүнгээс хамааран программын өөр өөр хэсгүүд рүү очих болно. C++-д нөхцөл шалгах хэд хэдэн арга бий. IF оператор бол хамгийн энгийн нөхцөл шалгах оператор юм. Бичигдэх хэлбэр if (нөхцөл) { // нөхцөл үнэн үед биелэгдэх үйлдлүүдийг бичнэ }
  • 3. www.emust.edu.mn Нөхцөл шалгах if Хэрэв нөхцөл үнэн бол if-ийн үндсэн хэсэг дэх үйлдлийг, худал бол if хэсгийн их бие доторх үйлдлийг гүйцэтгэлгүй алгасна.
  • 4. www.emust.edu.mn Example 1: if statement true false
  • 5. www.emust.edu.mn Example 1: if statement // Program to display a number if it is negative #include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); // true if number is less than 0 if (number < 0) { printf("You entered %d.n", number); } printf("The if statement is easy."); return 0; } Output 1 Enter an integer: -2 You entered -2. The if statement is easy. Output 2 Enter an integer: 5 The if statement is easy.
  • 6. www.emust.edu.mn Нөхцөл шалгах if...else Бичигдэх хэлбэр if (нөхцөл) { //нөхцөл үнэн үед биелэгдэх үйлдлүүд } else { //нөхцөл хэдэл үед биелэгдэх үйлдлүүд } Хэрэв нөхцөл үнэн бол if-ийн үндсэн хэсэг дэх үйлдлийг, худал бол if худал хэсгийн их бие доторх үйлдлийг гүйцэтгэнэ.
  • 9. www.emust.edu.mn Example 2: if...else statement // Check whether an integer is odd or even #include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); // True if the remainder is 0 if (number%2 == 0) { printf("%d is an even integer.",number); } else { printf("%d is an odd integer.",number); } return 0; } Output 1 Enter an integer: 7 7 is an odd integer. Output 2 Enter an integer: 8 8 even integer."
  • 10. www.emust.edu.mn Нөхцөл шалгах if...else Ladder If ... else хэллэг нь тестийн илэрхийлэл үнэн эсвэл худал эсэхээс хамаарч хоёр өөр кодыг ажиллуулдаг. Заримдаа 2- оос дээш боломжит хувилбараас сонголт хийх шаардлагатай болдог. If ... else шат нь олон туршилтын илэрхийлэлүүдийн хооронд шалгаж, янз бүрийн мэдэгдлийг гүйцэтгэх боломжийг олгодог. Бичигдэх хэлбэр if (нөхцөл 1) {// үйлдэл } else if(нөхцөл 2) {// үйлдэл } else if (нөхцөл 3) { // үйлдэл } . . else {// үйлдэл }
  • 11. www.emust.edu.mn Example 3: C if...else Ladder // Program to relate two integers using =, > or < symbol #include <stdio.h> int main() { int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); //checks if the two integers are equal. if(number1 == number2) { printf("Result: %d = %d",number1,number2); } //checks if number1 is greater than number2.
  • 12. www.emust.edu.mn else if (number1 > number2) { printf("Result: %d > %d", number1, number2); } //checks if both test expressions are false else { printf("Result: %d < %d",number1, number2); } return 0; } Output Enter two integers: 12 23 Result: 12 < 23
  • 13. www.emust.edu.mn Нөхцөл шалгах Nested if...else if ... else нөхцөл дотор давхар if ... else ашиглах боломжтой. Example 4: Nested if...else #include <stdio.h> int main() { int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); if (number1 >= number2) { if (number1 == number2) { printf("Result: %d = %d",number1,number2); }
  • 14. www.emust.edu.mn Нөхцөл шалгах Nested if...else else { printf("Result: %d > %d", number1, number2); } } else { printf("Result: %d < %d",number1, number2); } return 0; }
  • 15. www.emust.edu.mn Нөхцөл шалгах Nested if...else if ... else мэдэгдлийн үндсэн хэсэгт зөвхөн нэг үйлдэл байгаа бол та хаалт {} ашиглах шаардлагагүй. Жишээлбэл: if (a > b) { print("Hello"); } print("Hi"); if (a > b) print("Hello"); print("Hi");
  • 16. www.emust.edu.mn #include <stdio.h> int main() { int x = 20; int y = 22; if (x<y) { printf("Variable x is less than y"); } return 0; }
  • 17. www.emust.edu.mn #include <stdio.h> int main() { int x = 20; int y = 22; if (x<y) { printf("Variable x is less than y"); } return 0; }
  • 18. www.emust.edu.mn #include <stdio.h> int main() { int x = 20; int y = 22; if (x<y) { printf("Variable x is less than y"); } return 0; }
  • 19. www.emust.edu.mn #include <stdio.h> int main() { int x, y; printf("enter the value of x:"); scanf("%d", &x); printf("enter the value of y:"); scanf("%d", &y); if (x>y) { printf("x is greater than yn"); }
  • 20. www.emust.edu.mn if (x<y) { printf("x is less than yn"); } if (x==y) { printf("x is equal to yn"); } printf("End of Program"); return 0; }