The Repeat-Until-Sentinel Idiom A better approach for the addition program that works for any number of values is to use the Repeat-Until-Sentinel idiom, which executes a set of statements until the user enters a specific value called a sentinel to signal the end of the list: while (true){ prompt user and read in a value if (value ==sentinel)break; rest of loop body You should choose a sentinel value that is not likely to occur in the input data.It also makes sense to define the sentinel as a named constant to make the sentinel value easy to change
The Repeat The Repeat-Until-Sentinel Idiom Sentinel Idiom A better approach for the addition program that works for any number of values is to use the Repeat-Until-Sentinel idiom, which executes a set of statements until the user enters a specific value called a sentinel to signal the end of the list: while (true) { prompt user and read in a value if (value == sentinel) break; rest of loop body } You should choose a sentinel value that is not likely to occur in the input data. It also makes sense to define the sentinel as a named constant to make the sentinel value easy to change
The if Statement The simplest of the control statements is the if statement,which occurs in two forms.You use the first form whenever you need to perform an operation only if a particular condition is true: if(condition)【 statements to be executed if the condition is true You use the second form whenever you want to choose between two alternative paths,one for cases in which a condition is true and a second for cases in which that condition is false: if (condition){ statements to be executed if the condition is true else statements to be executed if the condition is false
The if Statement Statement The simplest of the control statements is the if statement, which occurs in two forms. You use the first form whenever you need to perform an operation only if a particular condition is true: if (condition) { statements to be executed if the condition is true } You use the second form whenever you want to choose between two alternative paths, one for cases in which a condition is true and a second for cases in which that condition is false: if (condition) { statements to be executed if the condition is true } else { statements to be executed if the condition is false }