ป้ายกำกับ

วันพฤหัสบดีที่ 20 พฤศจิกายน พ.ศ. 2557

else if # 3

else if # 3

  คำสั่งที่ใช้ในการตรวจสอบเงื่อนไขในภาษาซี คำสั่งแรกที่เราจะเรียนรู้กันก็คือ if (แปลว่า “ถ้า”) นั่นคือเราจะใช้ if เพื่อตรวจสอบผลของการเปรียบเทียบว่าเป็น “จริง” หรือ “เท็จ” ถ้า เป็นจริง จะให้ทำอะไรต่อไป และถ้าเป็นเท็จ จะให้ทำอะไรต่อไป (ออกสอบ)

   

ขอบคุณที่มาจาก http://www.cprogramming.com/tutorial/lesson2.html

>     greater than              5 > 4 is TRUE
<     less than                 4 < 5 is TRUE
>=    greater than or equal     4 >= 4 is TRUE
<=    less than or equal        3 <= 4 is TRUE
==    equal to                  5 == 5 is TRUE
!=    not equal to              5 != 4 is TRUE

The structure of an if statement is as follows:
if ( TRUE )
  Execute the next statement

Here is a simple example that shows the syntax:
if ( 5 < 10 )
  cout<<"Five is now less than ten, that's a big surprise";


Else

if ( TRUE ) {
  // Execute these statements if TRUE
}
else {
  // Execute these statements if FALSE
}

Else If

if ( <condition> ) {
  // Execute these statements if <condition> is TRUE
}
else if ( <another condition> ) {
  // Execute these statements if <another condition> is TRUE and
  // <condition> is FALSE
}

ตัวอย่างโปรแกรม รูปแบบเงื่อนไข (ออกสอบ)

#include <iostream>

using namespace std;

int main()                                                        // Most important part of the program!
{
  int age;                                                        // Need a variable...
  
  cout<<"Please input your age: ";                  // Asks for age
  cin>> age;                                                  // The input is put in age
  cin.ignore ( );                                                 // Throw away enter
  if ( age <= 30 ) {                                         // If the age is less than 30
     cout<<"You are pretty young!\n";              // Just to show you it works...
  }
  else if ( age > 31 ) {                                      // I use else just to show an example 
     cout<<"You are old\n";                              // Just to show you it works...
  }
  else {
    cout<<"You are really old\n";                     // Executed if no other statement is
  }
  cin.get( );
}

thank you : http://www.cprogramming.com/tutorial/lesson2.html

ไม่มีความคิดเห็น:

แสดงความคิดเห็น