1 / 9

โครงสร้างควบคุมการทำงานแบบทางเลือก ( ต่อ )

โครงสร้างควบคุมการทำงานแบบทางเลือก ( ต่อ ). การใช้ Conditional Operator ?:. ใน C++ มีการกำ หนดเงื่อนไขที่ทำ งานได้เหมือน if…else เรียกว่า conditional operator ใช้สัญลักษณ์ ?: แทน มีรูปแบบ คือ หลักการ ทำงาน เมื่อเงื่อนไขให้ค่าเป็น จริง ตัวแปรผลลัพธ์จะมีค่าเป็น ค่าที่ 1

owen
Download Presentation

โครงสร้างควบคุมการทำงานแบบทางเลือก ( ต่อ )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. โครงสร้างควบคุมการทำงานแบบทางเลือก(ต่อ)โครงสร้างควบคุมการทำงานแบบทางเลือก(ต่อ)

  2. การใช้ Conditional Operator ?: • ใน C++ มีการกำ หนดเงื่อนไขที่ทำ งานได้เหมือน if…else เรียกว่า conditional operator • ใช้สัญลักษณ์ ?: แทน มีรูปแบบ คือ • หลักการทำงานเมื่อเงื่อนไขให้ค่าเป็น จริงตัวแปรผลลัพธ์จะมีค่าเป็น ค่าที่ 1 • แต่ถ้าเงื่อนไขให้ค่าเป็น เท็จตัวแปรผลลัพธ์จะมีค่าเป็น ค่าที่ 2 ตัวแปรเก็บผลลัพธ์ = (เงื่อนไขเปรียบเทียบ) ? ค่าที่ 1 : ค่าที่ 2 ;

  3. if (a>b) c= a; else c=b; c=(a>b)? a : b;

  4. การเปรียบเทียบค่า i และ j โดยใช้ conditional operator #include <iostream> using namespace std; int main() { int i = 1, j = 2; cout << ( i > j ? i : j ) << " is greater." << endl; }

  5. การเลือกทำ แบบ switch … case • ในกรณีที่การเลือกทำ มีหลายเงื่อนไข แต่ละเงื่อนไขขึ้นอยู่กับ ตัวแปร (variable) ตัวเดียวกันที่เป็นประเภท int หรือ char • สามารถใช้การเลือกทำแบบ switch…case แทนการเลือก ทำแบบ if ซ้อนกัน (nested if ) ได้

  6. การเลือกทำ แบบ switch … case

  7. การเลือกทำ แบบ switch … case … • การพิจารณาเกรด grade 4 cout<<“Excellent”; break; 3 cout<<“Very good”; break; 2 cout<<“good”; break; default cout<<“Poor”; break; …

  8. การเลือกทำ แบบ switch … case • การพิจารณาเกรด #include<iostream> main(){ int grade; cout<<“Please enter your grade”<<endl; cin>>grade; switch(grade){ case 4 : cout<<“Excellent”; break; case 3 : cout<<“Very good”; break; case 2 : cout<<“good”; break; default : cout<<“Poor”; }

  9. #include <iostream.h> void main() { intfirst,second; char choice; //begin statement cout<<"Program Calcurate Area\n"; cout<<"1. Circle\n"; cout<<"2. Square\n"; cout<<"3. Triangle\n"; cout<<"Please select your choice <1-3>: "; cin>>choice; //begin switch statement switch(choice) { case '1': cout<<"\nYou select choice "<<choice<< " calculate Circle Area\n"; cout<<"Press any key to end program\n"; break; case '2': cout<<"\nYou select choice "<<choice<< " calculate Square Area\n"; cout<<"Press any key to end program\n"; break; case '3': cout<<"\nYou select choice "<<choice<< " calculate Triangle Area\n"; cout<<"Press any key to end program\n"; break; default: cout<<"\nYou select Another choice \a\a\n"; cout<<"Press any key to end program\n"; } }

More Related