ระบุเงื่อนไขของ control Structure 
ระบุเงื่อนไขของ control Structure
JAVA :: Structure :: คำสั่งควบคุมการทำงาน IF
subject:คำสั่งควบคุมการทำงาน IF
syntax : if ( condition )
{
structure;
}
content: คำสั่ง if คือ ใช้ตรวจสอบเงื่อนไข ถ้าหากว่าเงื่อนไขเป็นจริง ก็จะทำตามคำสั่งที่กำหนด
example: int num = 100;
if ( num > 100 )
{
System.out.print ( "many" );
}
JAVA :: Structure :: คำสั่งควบคุมการทำงาน IF ELSE
subject: คำสั่งควบคุมการทำงาน IF ELSE
syntax: if ( condition )
{
structure;
}
else
{
structure;
}
content: คำสั่ง if คือ ใช้ตรวจสอบเงื่อนไข ถ้าหากว่าเงื่อนไขเป็นจริง ก็จะทำตามคำสั่งที่กำหนด
ส่วน else จะหมายถึง ถ้าไม่ตรงตามเงื่อนไขใดๆ ให้ทำตามคำสั่งที่กำหนดใน else
example: int num = 100;
if ( num > 100 )
{
System.out.print ( "many" );
}
else
{
System.out.print ( "little" );
}
JAVA :: Structure :: คำสั่งควบคุมการทำงาน IF ELSE IF
subject: คำสั่งควบคุมการทำงาน IF ELSE IF
syntax: if ( condition )
{
structure;
}
else if ( condition )
{
structure;
}
else
{
structure;
}
content: คำสั่ง if คือ ใช้ตรวจสอบเงื่อนไข ถ้าหากว่าเงื่อนไขเป็นจริง ก็จะทำตามคำสั่งที่กำหนด
ส่วน else จะหมายถึง ถ้าไม่ตรงตามเงื่อนไขใดๆ ให้ทำตามคำสั่งที่กำหนดใน else
example: int num = 100;
if ( num > 100 )
{
System.out.print ( "many" );
}
else if ( num > 50 )
{
System.out.print ( "middle" );
}
else
{
System.out.print ( "little" );
}
JAVA :: Structure :: คำสั่งควบคุมการทำงาน NESTED IF
subject: คำสั่งควบคุมการทำงาน NESTED IF
syntax: if ( condition )
{
if ( condition )
{
structure;
}
}
content: คำสั่ง if คือ ใช้ตรวจสอบเงื่อนไข ถ้าหากว่าเงื่อนไขเป็นจริง ก็จะทำตามคำสั่งที่กำหนด
ส่วน else จะหมายถึง ถ้าไม่ตรงตามเงื่อนไขใดๆ ให้ทำตามคำสั่งที่กำหนดใน else
example: int num = 100;
if ( num > 100 )
{
System.out.print ( "many" );
if ( num > 50 )
{
System.out.print ( "middle" );
}
}
JAVA :: Structure :: คำสั่งควบคุมการทำงาน SWITCH CASE
subject: คำสั่งควบคุมการทำงาน SWITCH CASE
syntax: switch ( variable )
{
case value_1
: structure;
break;
case value_2
: structure;
break;
default
: structure;
break;
}
content: คำสั่ง switch case คือ จะตรวจสอบค่าของตัวแปร ถ้าตรงตามเงื่อนไขใดก็จะทำตามคำสั่งนั้นๆ
จนกว่าจะเจอ คำสั่ง break
example: int num = 100;
switch ( num )
{
case 100
: System.out.print ( "many" );
break;
case 50
: System.out.print ( "middle" );
break;
default
: System.out.print ( "little" );
break;
}
JAVA :: Structure :: คำสั่งควบคุมการทำงาน EXPRESSION
subject: คำสั่งควบคุมการทำงาน EXPRESSION
syntax: variable = condition ? value_when_true : value_when_false ;
content: คำสั่ง expression คือ ใช้กำหนดคำสั่งแบบมีเงื่อนไข
example: int num = 100;
String str = ( num == 100 ) ? "many" : "little" ;
JAVA :: Structure :: คำสั่งควบคุมการทำงาน FOR
subject: คำสั่งควบคุมการทำงาน FOR
syntax: for ( ค่าเริ่มต้น; เงื่อนไข; การเพิ่มค่า )
{
structure;
}
content: คำสั่ง for คือ จะทำงานเมื่อเงื่อนไขเป็นจริง และจะจบการทำงานเมื่อเงื่อนไขเป็นเท็จ
โดยแต่ละรอบของการวนลูปจะมีการเพิ่มค่าตามที่กำหนด
example: for ( int i=0; i<100; i++ )
{
System.out.print ( i );
}
JAVA :: Structure :: คำสั่งควบคุมการทำงาน WHILE
subject: คำสั่งควบคุมการทำงาน WHILE
syntax: while ( เงื่อนไข )
{
structure;
}
content: คำสั่ง while คือ จะทำงานเมื่อเงื่อนไขเป็นจริง และจะจบการทำงานเมื่อเงื่อนไขเป็นเท็จ
example: int num = 10;
while ( num > 0 )
{
System.out.print ( num );
num--;
}
JAVA :: Structure :: คำสั่งควบคุมการทำงาน DO WHILE
subject: คำสั่งควบคุมการทำงาน DO WHILE
syntax:do
{
structure;
}
while ( เงื่อนไข );
content: คำสั่ง do while คือ จะทำงานก่อน 1 ครั้ง แล้วหลังจากนั้นจะทำงานก็ต่อเมื่อเงื่อนไขยังเป็นจริงอยู่
และจะจบการทำงานเมื่อเงื่อนไขเป็นเท็จ
example: int num = 10;
do
{
System.out.print ( num )
num--;
}
while ( num > 10 )
ไม่มีความคิดเห็น:
แสดงความคิดเห็น