C: Selection
Selection, in programming, is a method of specifying conditions in which statements are executed.
- If
Used as:
if(boolean expression)
{
statement;
}
The statement will be run only if the boolean expression results in true.
- If... else...
Used as:
if(boolean expression)
{
statement;
}
else
{
statement2;
}
The first statement will be run if its boolean expression results in true. If it results in false, statement2 will be run instead.
- Nested if
Used as:
if(boolean expression)
{
if(boolean expression)
{
statement;
}
}
The statement will be run only if every boolean expression leading to it results in true.
- Switch case
Used as:
switch(expression)
{
case constant:
statement;
break;
case constant2:
statement2;
break;
default:
statement3;
}
A statement will be run only if the expression is equal to the constant leading to it. If no constant matches the expression, the default statement will be run.
2201757635
skyconnectiva.com
binus.ac.id
Juanda P. G.
- If
Used as:
if(boolean expression)
{
statement;
}
The statement will be run only if the boolean expression results in true.
- If... else...
Used as:
if(boolean expression)
{
statement;
}
else
{
statement2;
}
The first statement will be run if its boolean expression results in true. If it results in false, statement2 will be run instead.
- Nested if
Used as:
if(boolean expression)
{
if(boolean expression)
{
statement;
}
}
The statement will be run only if every boolean expression leading to it results in true.
- Switch case
Used as:
switch(expression)
{
case constant:
statement;
break;
case constant2:
statement2;
break;
default:
statement3;
}
A statement will be run only if the expression is equal to the constant leading to it. If no constant matches the expression, the default statement will be run.
2201757635
skyconnectiva.com
binus.ac.id
Juanda P. G.
Comments
Post a Comment