Dalam bab ni kitorang belajar camne nak tukar if...else statement kepada switch...case statement.
kat bawah ni ialah contoh camne nak buat statement ni...(kalau tak faham,buat2 faham jer...hehehe).
#include<iostream.h>
main()
{
//declare variable
int age;
//if else statement
if(age>=1 ll age<=6)
{cout<<"you are kids!;}
else if(age>=7 ll age<=12)
{cout<<"you are child babe!";}
else if(age>=13 ll age <=21)
{cout<<"you are teenager!";}
else if(age>=21 ll age<=30)
{cout<<"you are adult now!";}
else if(age>=30)
{cout<<"you are old,boo!";}
else
{cout<<"invalid selection";}
return 0;
}
*yg kat atas ni ialah if...else statement dan yang kat bawah ni pulak ialah switch...case statement.
#include<iostream.h>
main()
{
//declare variable
int age;
//input
cout<<"enter your age=";
cin>>age;
//switch case statement
switch(age)
{case('age'>=1 ll 'age'<=6):cout<<"you are kids!";break;
case('age'>=7 ll 'age'<=12):cout<<"you are child beb!";break;
case('age'>=13 ll 'age'<=21):cout<<"you are teenage!";break;
case('age'>=22 ll 'age'<=30):cout<<"you are adult now!";break;
case('age'>=30):cout<<"you are old!";break;
default:cout<<"invalid selection";
}
return 0;
}
Saturday, March 26, 2011
If...else statement to switch...case statement
Posted by mohammad at 8:39 AM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment