.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

การเขียนโปรแกรม C++/CLI ครั้งที่ 4 Options · View
paedotnet
Posted: Thursday, December 20, 2007 6:22:22 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 344
Location: bkk
การใช้งาน If

รูปแบบ if(เงื่อนไข ){
//ถ้าเงื่อนไขเป็นจริงก็ทำงานที่คำสั่งในนี้
}
ถ้าต้องการกำหนดคำสั่งต่างๆในกรณีที่เงื่อนไขไม่เป็นจริงก็ใช้ else ดังนี้

if(เงื่อนไข){

}
else{
}

ถ้าต้องการกำหนดเงื่อนไขมากกว่าหนึ่งเงื่อนไขในกรณีที่เงื่อนไขแรกไม่เป็นจริงก็ใช้ else if ดังนี้
if(เงื่อนไข1){
}else if(เงื่อนไข2){
}else{
}

ตัวอย่างการใช้ if

Code:
#include "stdafx.h"
using namespace System;
int main(){
    
    Console::WriteLine("Enter your score");
    String^ i = Console::ReadLine();
    if(i =="A" || i =="a"){
        Console::WriteLine("excellence");
    }else if(i =="B" || i =="b"){
        Console::WriteLine("very good");
    }else if(i =="C" || i =="c"){
        Console::WriteLine("good");
    }else{
        Console::WriteLine("ok");
    }
    Console::ReadLine();
return 0;
}


การใช้งาน for
for ใช้ในการวนลูปมีรูปแบบดังนี้
for(ค่าเริ่มต้น;เงื่อนไข;การเปลื่ยนแปลงค่า)
ตัวอย่าง

Code:
#include "stdafx.h"
using namespace System;
int main(){
    for(int i= 0;i<10;i++){
        Console::WriteLine(i);
    }
    Console::ReadLine();
return 0;
}

หรือถ้าต้องการเพิ่มค่าทีละสองก็เขียนได้ดังนี้

Code:
#include "stdafx.h"
using namespace System;
int main(){
    for(int i= 0;i<100;i+=2){
        Console::WriteLine(i);
    }
    Console::ReadLine();
return 0;
}



Code:
#include "stdafx.h"
using namespace System;
int main(){
    for(int i=-100;i<1;i+=2){
        Console::WriteLine(i);
    }
    Console::ReadLine();
return 0;
}

การใช้งาน for each
for each ใช้ในการวนลูปเหมือน for แต่มีรูปแบบไม่เหมือนกันดังนี้
for each(type_variable variabel_name in array){
}

ตัวอย่าง
Code:
#include "stdafx.h"
using namespace System;
int main(){
    int i[10] ={1,2,3,4,57,8,100,99,77,88};
    for each(int j in i){
        Console::WriteLine(j);
    }
    Console::ReadLine();
return 0;
}

การใช้งาน while
while ใช้ในการวนลูบมีรูปแบบดังนี้
while(เงื่อนไข){
//คำสั่ง
//การเพิ่มค่า
}

ตัวอย่าง


Code:
#include "stdafx.h"
using namespace System;
int main(){
    int i=0;
    while(i<20){
        Console::WriteLine(i);
        i++;
    }
    Console::ReadLine();
return 0;
}

การใช้ Do-While
do while จะคล้ายๆกับ while แต่จะทำงานตามคำสั่งก่อนแล้วค่อยตรวจสอบเงื่อนไข มีรุปแบบดังนี้

do{
//คำสั่ง
}while(เงื่อนไข);

ตัวอย่าง

Code:
#include "stdafx.h"
using namespace System;
int main(){
    int i=0;
    do{
        Console::WriteLine(i);
    i+=2;
    }while(i<20);
    Console::ReadLine();
return 0;
}

ตัวอย่าง

Code:
#include "stdafx.h"
using namespace System;
int main(){
    for(int i=0;i<10;i++){
        for(int j=0;j<i;j++){
            Console::Write("*");
        }
        Console::WriteLine();
    }
    Console::ReadLine();
return 0;
}
ผลลัพธ์แสดงดังรูปที่ 1


ตัวอย่างต่อไป

Code:
#include "stdafx.h"
using namespace System;
int main(){
    int k=5;
    int l=0;
    for(int i=0;i<k;i++){
        for(int j=0;j<i;j++){
            Console::Write(" ");
        }
        for(int m=5;m>i+1;m--){
            Console::Write("*");
        }
        for(int m=5;m>i+2;m--){
            Console::Write("*");
        }
        Console::WriteLine();
    }
    Console::ReadLine();
return 0;
}

ผลลัพธ์แสดงดังรูปที่ 2




ตัวอย่าง

Code:
#include "stdafx.h"
using namespace System;
int main(){
    int k=5;
    int l=0;
    for(int i=0;i<k;i++){
        for(int j=0;j<i;j++){
            Console::Write(" ");
        }
        for(int m=5;m>i+1;m--){
            Console::Write("*");
        }
     Console::WriteLine();
    }
    Console::ReadLine();
return 0;
}
ผลลัพธ์แสดงดังรูปที่ 3


paedotnet attached the following image(s):
pic1.jpg
pic2.jpg
pic3.jpg



[With great power comes great responsibility]
mint
Posted: Saturday, December 22, 2007 2:33:54 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 12/12/2007
Posts: 2
Location: BKK
สงสัยอย่างนึงคับ ในตัวอย่าง Code อันแรก
ทำไม variable String ตอนประกาศต้อง มี ^ ด้วยอ่ะ

อ้างอิง: String^ i = Console::ReadLine();
paedotnet
Posted: Saturday, December 22, 2007 2:50:06 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 344
Location: bkk

ครับ เครื่องหมาย ^ หมายถึง Handle นะครับ ที่ String จะต้องมี ^ เพราะว่า ชนิดของ String เป็น Reference type ครับ



[With great power comes great responsibility]
mint
Posted: Saturday, December 22, 2007 10:41:10 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 12/12/2007
Posts: 2
Location: BKK

thx krub 

sinjalaa
Posted: Saturday, June 21, 2008 9:02:57 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 6/21/2008
Posts: 1

#include<iostream.h>
#include<conio.h>
void main ()
{
  clrscr();
  int age;
  cin >> age;
  if(age<=3)
    cout <<"Baby\n";
  else if(age<=12)
    cout <<"Student\n";
  else if(age<=25)
    cout <<"Young\n";
  else if(age<=50)
    cout <<"Aduath\n";
  else
    cout <<"Old Man\n";
  getch();
}
 

คือว่าเวลาใส่ค่าไปแล้ว พอกด Enter มันก้อจาออกคับ

ผมจาทำให้มันวนกลับมาทำใหม่คับ ใส่ while ได้ไหม คับ ยังไงคับ

paedotnet
Posted: Monday, June 23, 2008 11:01:46 AM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 344
Location: bkk

ลองดูตัวอย่างนะครับ

#include<iostream>
#include<conio.h>
using namespace std;
void main ()
{
  int age;
  cin >> age;
  while(true){  //จะวนลูปไปเรื่อยๆคือให้ผู้ใช้สามารถใส่ค่าได้เรื่อยๆ
  if(age<=3)
    cout <<"Baby\n";
  else if(age<=12)
    cout <<"Student\n";
  else if(age<=25)
    cout <<"Young\n";
  else if(age<=50)
    cout <<"Aduath\n";
  else
    cout <<"Old Man\n";
  cin>>age;
  }
  getch();
}

แต่วิธีนี้ ถ้าใส่ while (true) อะครับมันจะวนลูปไปเรื่อยๆนะครับดังนั้นถ้าต้องการใช้ออกจากลูปก็ต้องมีเงื่อนไขมาตรวจสอบให้ออกจากลูปด้วยเช่น ถ้าผู้ใช้ใส่ค่าเข้ามาที่ไม่ใช้ตัวเลขก็ให้ออกจากลูปก็สามารถเขียนโค้ดได้ดังนี้

#include<iostream>
#include<conio.h>
using namespace std;
void main ()
{
 int age;
 while((cin>>age)){  //ให้รับค่าเข้ามาเป็นตัวเลขเท่านั้นถ้าเป็นตัวอักษรก็จะออกจากลูป
  if(age<=3)
    cout <<"Baby\n";
  else if(age<=12)
    cout <<"Student\n";
  else if(age<=25)
    cout <<"Young\n";
  else if(age<=50)
    cout <<"Aduath\n";
  else
    cout <<"Old Man\n";
}
 }

 

 



[With great power comes great responsibility]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by