Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

Objective-C and if , else if , switch Flow Condition Statement (iOS,iPhone,iPad)

Objective-C and if , else if , switch Flow Condition Statement (iOS,iPhone,iPad) ในการเขียนโปรแกรมทุกภาษา สิ่งที่ขาดไม่ได้เลยนั่นก็คือการใช้งาน if , else if และ switch โดยชุดคำสั่งเหล่านี้จะนำมาใช้ในกรณีที่ต้องการให้โปรแกรมเลือการทำงาน หรือตัดสินใจทำอย่างใดอย่างหนึ่ง และในภาษา Objective-C คำสังเหลานี้ก็ไม่ได้ต่างอะไรกับภาษาที่เราค้น ๆ กันอยู่ เช่น php , c# , java javascript หรือภาษาอืาน ๆ และเกือบจะเรียกได้ว่าไม่ต่างกันแม้กระทั่งรูปแบบการเขียนเลย ในบทความนี้จะไม่ได้อธิบายรูปแบบการใช้งานที่ละเอียดนัก แต่ก็พอจะเป็นตัวอย่างที่ดูแล้วสามารถเข้าใจได้ในทันที เพื่อเป็นพื้นฐานในการเขียนโปรแกรมบน iOS , iPhone และ iPad นั้นสะดวกยิ่งขึ้น

- if Statement เงื่อนไขเป็นจริงเท่านั้นถึงจะทำงาน

if (boolean expression) {
// Statement
}

Ex
    int x = 10;
    if (x == 10)
    {
        NSLog (@ "True x = %i" , x);
    }

True x = 10

แสดงค่าเมื่อมีค่าเป็นจริงตามเงื่อนไข



- if else Statement ทางเลือก 2 ทาง ทำงานในเงื่อนที่เป็นจริง และ ทำงานในเงื่อนที่เป็นเท็จ

if (boolean expression) {
// Statement True
else
{
// Statement False
}

Ex
    int x = 8;
    
    if ( x > 10 )
    {
        NSLog (@"%i is more than 10",x);
    }
    else
    {
        NSLog (@"%i is less than 10",x);
    }

8 is less than 10

กำหนดเงื่อนไข 2 ทางเลือก ว่าจะทำในกรณีที่เป็นจริง หรือ ทำในกรณีที่เป็นเท็จ



- if...else if...else Statement สร้างเงื่อนที่มากกว่า 2 เงื่อนไข

if (boolean expression) {
// Statement condition is true
}
else if (boolean expression) {
{
// Statement condition is true
}
else if (boolean expression) {
{
// Statement condition is true
}
else
{
// Statement False
}

Ex
    int x = 3;
    
    if ( x == 1 )
    {
        NSLog (@"%i is equal 1",x);
    }
    else if( x == 2 )
    {
        NSLog (@"%i is equal 2",x);
    }
    else if( x == 3 )
    {
        NSLog (@"%i is equal 3",x);
    }
    else if( x == 4 )
    {
        NSLog (@"%i is equal 4",x);
    }
    else if( x == 5 )
    {
        NSLog (@"%i is equal 5",x);
    }
        else
    {
        NSLog (@"%i not have in condition",x);
    }

3 is equal 3

ในตัวอย่างจะมีหลายเงื่อนไข แต่จะเลือกทำในเงื่อนที่เป็นจริงกับ condition นั้น ๆ








นอกจากนี้เรายังสามารถใช้ and (&&) เพื่อกำหนดเงื่อนไนในส่วนของ if ได้อีกด้วย เช่น

Ex
    int x = 75;
    
    if ( x >= 90 && x <= 100 )
    {
        NSLog (@"Grade A");
    }
    else if( x >= 80 && x <= 89 )
    {
        NSLog (@"Grade B");
    }
    else if( x >= 70 && x <= 79 )
    {
        NSLog (@"Grade C");
    }
    else if( x >= 60 && x <= 69 )
    {
        NSLog (@"Grade D");
    }
    else
    {
        NSLog (@"Grade F");
    }

Grade C



- switch Statement สามารถสร้างเงื่อนได้ตั้งแต่ 1 เงื่อนไข จนถึงหลายเงื่อนไข แต่เขียนง่ายและสะดวกกว่า if..else..if

    switch (expression)
    {
        case expression 1 :
            statement
            break;
        case expression 2 :
            statement
            break;
        case expression n :
            statement
            break;
        default:
      }

สังเกตุว่าในแต่ล่ะ statement จะต้องมี break; ด้วย

Ex
    int x = 3;
    
    switch (x)
    {
        case 1 :
            NSLog (@"%i is equal 1",x);
            break;
        case 2 :
            NSLog (@"%i is equal 2",x);
            break;
        case 3 :
            NSLog (@"%i is equal 3",x);
            break;
        case 4 :
            NSLog (@"%i is equal 4",x);
            break;
        case 5 :
            NSLog (@"%i is equal 5",x);
            break;
        default:
            NSLog (@"%i not sent condition",x);
    }

3 is equal 3


จากตัวอย่าง 4-5 ตัวจะเห็นว่าภาษา Objective-C มีรูปแบบเงื่อนการใช้งานคำสั่ง if .. else .. switch เหมือน ๆ กับภาษาที่เราคุ้น ๆ พวก php , javascript , c# หรือ java ฉะนั้นบทความนี้จะไม่ได้อธิบายการใช้งานที่ละเอียดนัก







.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-10-21 06:32:43 / 2017-03-25 22:51:33
  Download : No files
 Sponsored Links / Related

 
การเขียน Coding ด้วยภาษา Objective-C กับการแสดงผลง่าย ๆ บน iOS (iPhone,iPad)
Rating :

 
iOS/iPhone Dynamic Create Object Dynamically (Objective-C , iPhone)
Rating :

 
Objective-C and Cocoa กับการเขียนโปรแกรมบน iOS สำหรับ iPhone และ iPad
Rating :

 
Objective-C and Variable รู้จักกับตัวแปรในภาษา Objective-C (iOS,iPhone,iPad)
Rating :

 
Objective-C and DataType ชนิดของตัวแปรในภาษา Objective-C (iOS,iPhone,iPad)
Rating :

 
Objective-C and Loop การสร้างลูปและแสดง Loop แบบง่าย ๆ (iOS,iPhone,iPad)
Rating :

 
Objective-C and Operator การใช้ Operator บนภาษา Objective-C (iOS,iPhone,iPad)
Rating :

 
Objective-C Property(strong,nonatomic) , Weak , Strong type (iOS, iPhone, iPad)
Rating :

 
Objective-C and Class Object (OOP) การเรียกใช้งาน Class (iOS,iPhone,iPad)
Rating :

 
iOS/iPhone Substring / Split / Replace String (Objective-C)
Rating :

 
iOS/iPhone and Timer (NSTimer, Objective-C)
Rating :

 
iOS/iPhone and Thread (NSThread, Objective-C)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่