28

4 control statement

Embed Size (px)

Citation preview

Page 1: 4  control statement
Page 2: 4  control statement

Control StatementsControl Statements

คอประโยคทใชในการดำาเนนการของโปรแกรมไปในทศทางทตองการ ทนอกเหนอไปจากการดำาเนนการในลำาดบ (sequence ) ของประโยคหรอคำาสงปกต แบงออกเปน – if statement– switch statement– while statement– do statement– for statement– break statement– continue statement

Page 3: 4  control statement

if (<boolean expression>) <statement>;

[ else<statement>;

]

• ประโยค if ใชควบคมโปรแกรมเลอกดำาเนนการไปในเสนทางใดเสนทางหนงระหวางทางเลอกสองทาง โดยใชผลของ <boolean expression> เปนตดสนใจโครงสรางของประโยค โดยมรปแบบของประโยคดงน

if statement

Page 4: 4  control statement

• #include<iostram.h>• void main( ) {• int n; cout<<” Enter number :: >> ”; cin>>n;• if( n = = 0) • cout<<”Zero Number ” ;• if( n > 0)• cout<<” Positive Number \n” ;• else • cout<<” Negative Number \n” ;• }

Page 5: 4  control statement

if statement

Statement 1

expression

Statement 2

false

true

expression

Statement 3

true

Statement 2

false

Statement 1

แบบท แบบท 11 แบบท แบบท 22

Page 6: 4  control statement

#include<iostream>using namespace std;void main(){

int x = 0; if ( 1 ) cout<<"print 1"<<endl; if ( !x ) cout<<"!x\n"<<endl; else cout<<"x\n"<<endl;}

Page 7: 4  control statement

การเปรยบเทยบตวเลขการเปรยบเทยบตวเลขถารบคามา 5 คา โดย n0,n1,n2,n3,n4 แลวหาวาคา

ใดมากทสดคาใดนอยทสด (แนวคดทำาอยางไร)

ถามตวเลขไมซำำาคาตดบนลกบอล 5 ลก ใสไวในถงดำาแลวใหหยบมาท

ละลกเพอหาลกทมคามากทสดและนอยทสด

คำาถาม จะตองหยบกครำง และ มวธการอยางไรในการหาลกทนอยทสดและมากทสด

Page 8: 4  control statement

MAX = MIN =

11

ถามตวเลขไมซำำาคาตดบนลกบอล 5 ลก ใหหาเลขทนอยทสดและมาก

ทสดทตดบนฟตบอล จะทำาอยางไร !!!

Page 9: 4  control statement

ถามตวเลขไมซำำาคาตดบนลกบอล 5 ลก ใหหาเลขทนอยทสดและมาก

ทสดทตดบนฟตบอล จะทำาอยางไร !!!

MAX = MIN =

22

Page 10: 4  control statement

ถามตวเลขไมซำำาคาตดบนลกบอล 5 ลก ใหหาเลขทนอยทสดและมาก

ทสดทตดบนฟตบอล จะทำาอยางไร !!!

MAX = MIN =

33

Page 11: 4  control statement

ถามตวเลขไมซำำาคาตดบนลกบอล 5 ลก ใหหาเลขทนอยทสดและมาก

ทสดทตดบนฟตบอล จะทำาอยางไร !!!

MAX = MIN =

44

Page 12: 4  control statement

ถามตวเลขไมซำำาคาตดบนลกบอล 5 ลก ใหหาเลขทนอยทสดและมาก

ทสดทตดบนฟตบอล จะทำาอยางไร !!!

MAX = MIN =

55

Page 13: 4  control statement

การเปรยบเทยบตวเลขการเปรยบเทยบตวเลขถารบคามา 5 คา โดย n0,n1,n2,n3,n4 แลวหาวาคา

ใดมากทสดคาใดนอยทสด (แนวคดทำาอยางไร)1. รบคา cin>>no; cin>>n1;cin>>n2;cin>>n3;cin>>n4

2. กำาหนดให คาแรกเปน ตวทมากทสด และนอยทสด3. เปรยบเทยบคาตวทเหลอโดยเรมจากตวท 2 เพราะตวแรกใหมาก

และนอยทสดไปแลว4. แสดงผลลพธ

Page 14: 4  control statement

#include<iostream.h>void main(){

int n0,n1,n2,n3,n4;int max,min;cout<<"Enter 5 numbers >> ";cin>>n0;cin>>n1;cin>>n2;cin>>n3;cin>>n4;

max = n0;min =n0;//round 1if (n1>max) max=n1;if (n1<min) min=n1;//round 2if (n2>max) max=n2;if (n2<min) min=n2;//round 3if (n3>max) max=n3;if (n3<min) min=n3;//round 4if (n4>max) max=n4;if (n4<min) min=n4;// Print outputcout<<"minimum number = "<<min<<endl;cout<<"maximum number = "<<max<<endl;

}

Page 15: 4  control statement

#include<iostream.h>void main(){

int n[5];int max,min,i;cout<<"==== Enter 5 numbers ====="<<endl;for (i=0;i<5;i++){

cout<<"Emter number "<<i+1<<" >> ";cin>>n[i];//first is n[0] Not n[1];} max=n[0];min=n[0];for (i=1;i<5;i++){

if (n[i]>max) max=n[i];if (n[i]<min) min=n[i];

}

// Print outputcout<<"minimum number = "<<min<<endl;cout<<"maximum number = "<<max<<endl;

}

Page 16: 4  control statement

For statement

For (init-expression ; cond-expression ; loop-expression)#include<iostream.h>void main(){

int n[5]={1,2,3,4,5};int max,min,i;

for(i=1;i<=5;i++){ // i+=2cout<<"Print i value "<<i<<endl;

}}

Page 17: 4  control statement

int i;

for( i = 0; i < 5; cout<<i<<endl, i++);

int i, j;

for ( i = 5, j = 10 ; i + j < 20; i++, j++ )

cout<<"\n i + j = "<< (i+j) <<endl;

Page 18: 4  control statement

#include<iostream.h>void main(){

int n[5]={1,2,3,4,5};int i;for (i=0;i<=4;i++)

if (i >0){ cout<<n[i]<<endl;

i +=2;cout<<"Hahaha 55555 Joke "<<endl;

}else{

for(int j=0;j<2;j++)cout<<" I am a rock"<<j<<endl;

}cout<<"======================================="<<endl;

}//ผลลพธทไดคออะไร

Page 19: 4  control statement

เขยนคำาสง For statement แสดงผลดงน

Page 20: 4  control statement

#include<iostream.h>void main(){

int i,j,k;cout<<"Print value from 0 to 10,10 to 0 and -5 to 5 "<<endl;cout<<"i\t j\t k"<<endl;cout<<"---------------------"<<endl;for(i=0,j=10,k=-5;i<=10,j>=0,k<=5;i++,j--,k++){

// for(i=0,j=0,k=-5;i<=10;i++,j--,k++){cout<<i<<"\t"<<j<<"\t"<<k;

cout<<endl;}

cout<<"---------------------"<<endl;}

Page 21: 4  control statement

แสดงอณหภมเซลเซยส และฟาเรนไฮต โดยท

Fahreheit =9*Celseius /5+32

Page 22: 4  control statement

#include<iostream.h>void main(){

//Display Celseius and Fahrenheitfloat cel,fah;

cout<<"Celseius Fahreheit "<<endl;for(cel=0;cel<=100;cel++){

fah = 9*cel/5+32;cout<<" "<<cel<<"\t\t"<<fah<<endl;

}}

Page 23: 4  control statement

while..do and do…while#include <iostream.h>

int main()

{ int odd = 0, even = 0,time=0, result;

/* process 10 students; counter-controlled loop */

cout << "Enter Number 0 - 9 "<<endl;

while (time < 10){

cout << "Number "<< ++time<<" >>" ;

cin >> result;

if (result%2 == 0) even ++;

else odd++; }

cout<<"Odd = "<<odd<<endl;

cout<<"Even = "<<even<<endl;

}

while …doรบคา จำานวนเตม ตงแต 0 – 9 สบ

จำานวน แลวหาวามตวเลขค และ ค กจำานวน …while do

Page 24: 4  control statement

while..do and do…while#include <iostream.h>

int main()

{ int odd = 0, even = 0,time=0, result;

cout << "Enter Number 0 - 9 "<<endl;

do{cout << "Number "<< ++time<<" >>" ;

cin >> result;

if (result%2 == 0) even ++;

else odd++;

}while(time<10);

cout<<"Odd = "<<odd<<endl;

cout<<"Even = "<<even<<endl;

}

do…..while รบคา จำานวนเตม ตงแต 0 – 9 สบ

จำานวน แลวหาวามตวเลขค และ ค กจำานวน ...do…while

Page 25: 4  control statement

หาคา j k l กรณ ม และไมมคำาสง break #include <iostream.h>int main(){int j,k,l; j=10;k=1;l=0; do{

if(j+k) cout<<"J1+K1 = "<<j+k<<endl;

break; l++; j--;

}while(k++<6); cout<<"j = "<<j<<" k = "<<k<<" l = "<<l<<endl;}

Page 26: 4  control statement

#include <iostream.h>float power3(float);void main (void){ int i; for (i=1; i <=10; i++) cout<< i<<“ “<<power3(i) );}float power3(float x){ return x* x *x;}

1 1

2 8

3 27

4 64

5 125

6 216

7 343

8 512

9 729

10 1000

Function

Page 27: 4  control statement

1. พจารณาโปรแกรมตอไปน#include <iostream.h>

void main(){ int i, j, n; cout <<"Enter N = "; cin >> n; i = n; while (i >= 1) { j = 1; while (j < i) { cout << " "; j++; } while (j <= n) { cout << "*"; j++; } cout << "\n"; i--; }}

#include <iostream.h>void main(){ int i, j, n; cout <<"Enter N = "; cin >> n; for (i=n;i>=1;i--) { for (j=1;j<i;j++) cout << " "; for (j=i;j<=n;j++) cout <<"*"; cout << "\n"; }}

เปรยบเทยบการใช While และ For loop

Page 28: 4  control statement

#include<iostream>

using namespace std;

int main() {

char *buffer = "Any character stream";

int capa, lettera, nota;

char c;

capa = lettera = nota = 0;

cout<<"Any character stream";

while ( c = *buffer++ ) // Walks buffer until NULL

{

switch ( c )

{ case 'A':

capa++;

break;

case 'a':

lettera++;

break;

default:

nota++; }

}

cout<<"\nUppercase a: "<<capa<<"\nLowercase a: "<<lettera<<"\nNot character a: "<<nota;

cout<<"\nTotal:";

cout<<(capa + lettera + nota)<<endl;

}

ใหนกศกษาเขยนโคดโปรแกรมแลวทำาการรนวา

ไดคาอะไร แลวตอบคำาถามวา

โปรแกรมททำางานอยางไรเขยนขนมาเพออะไร