It is common to need to make choices between different alternatives when solving
ID: 3662081 • Letter: I
Question
It is common to need to make choices between different alternatives when solving problems. Alternatives can take different forms. For instance, only do action X if condition 1 is true presents an alternative of either doing or not doing action X. A different form of alternative occurs in the case where we need to do action X if condition 2 is true, otherwise do action Y. Here we have a choice between 2 different actions, and we will always do X or Y depending on condition 2. This form can be extended to choosing between 3, 4, or any number of actions where one action is always selected. Lets discuss some real examples where making these types of choices are needed. Identify the alternative actions and the condition that controls the choice.
Explanation / Answer
These making decisions are nothing but the
1)simple if
2)if else
3)switch case
simple if we will use based on the particular condition.if the particulr condition is true we will go with the process
if(gender=="female")
{
system.out.println("female member");
}
in the if else condition if the condition is true we will choose one option and if the condition false also we will go with another option
eg:
if(gender=="female")
{
system.out.println("female member");
}
else
{
system.out.println("male member");
}
the switch case statement is used make choice between the multiple options.
for example
switch(c)
case 'red' :system.out.println("it is red");
break;
case 'green' :system.out.println("it is green");
break;
case 'black' :system.out.println("it is black");
break;
case 'white' :system.out.println("it is white");
break;
}
break;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.