In: Computer Science
Respond to the following in a minimum of 175 words:
The flow of a program is controlled by different structures. The three basic control structures in computer programs are sequence structures, decision structures (also called selection structures), and repetition structures (also called iteration structures).
Discuss the differences between decision structures and repetition structures used in algorithms. Provide examples of when you might use each.
The three basic control structures for any program are :
The difference between decision structure and Repetition structure used in algorithms:
1. Decision Structure: These statements make evaluations based on the comparator operators used in the statements lile ' = ' , '>' , '<' ,'>=' , '<=' , '==' etc. It can also contain boolean operators like OR operator, AND operator and the overall output of the expression determines the output. The precedence rule of operators must always be taken into consideration for the evaluation of these expressions.
if (Condition set according to the user )
{
//block of code for execution
}
else if (Another condition set according to the user)
{
//block of code for execution
}
else
{
// the block of code that executes when the above conditions are not satisfied
}
Switch (the value that will act as switching parameter) :
{
case 1 : // block of code for execution
break;
case 2 : // block of code for execution
break;
break://Default block of code that executes if none of the conditions match
}
2. Repetition Structure
for(initialization, condition for looping, increment)
{
//Block of code for execution.
}
Example: for(int i=0;i<5;i++)
{
System.out.println("Hello World ");
}
initialization
while(condition that needs to be satisfied for execution)
{
//block of code for execution
( increment or decrement operator if required)
}
do
{
// block of code for execution
} while( condition )