Control flow
Alternative: it allows you select a sentence or a block of sentences through the evaluation of expression:
-Sentence if, else if, else: this sentence evalues from the values given returning true or false to go in example
The result of this will be:
-Switch is a sentence used to choose one between the several options. This one is specially when the selected values is simple.
The case default is like else in if; is the rest of expression
The result:
Frequents errors in alternative programming:
-The most common error is make a mistake with the assignment operator = and the equal operator == in if sentences
-The use of equal operator == in operations with decimal numbers, maybe it returns a fake logical expression because the decimal numbers don’t store exactly so, better use <= or >= in the case of decimal number.
-In the switch sentences the value to evaluate have to be integrate number or compatible, so the decimal number can’t be used here.
Repetitive sentences, loops.
This sentences can be executed a determinate or indeterminate number of times, this sentence has three parts, body, itinerate and control variable
-While the body will repeat all time until the control variable becomes true or write break;
The result
Do while: This loop usually is used to check information.
This code checks if the user introduces a digit and this loop at least execute once instead of while
-For: This repetitive operator is quite different from the rest you have to considerate:
– Initialization:
-It can be any value
-You can avoid that
-You can initialize more than one
– Checking:
-Simple condition
– Complex Condition
-You can avoid that, not recommended
– Itineration:
-It can be integral or decimal values.
-It can be mutables or fixed values.
Example of for:
-Cautions to considerate writing a for loop
-The condition must become false sometime, if not it will be an infinite loop.
-Don’t change the value of the control variable in the body.
-You can exist from the loop using break but is not recommendable
– Don’t write ; forward of for(…);
-For each This is used to roam a collection, like an array:
The structure is easy for (TypeOfCollection itinerator:NameOfCollection){
}
Repetitive sentences, loops.
This sentences can be executed a determinate or indeterminate number of times, this sentence has three parts, body, itinerate and control variable
-While the body will repeat all time until the control variable becomes true or write break;
The result
Do while: This loop usually is used to check information.
This code checks if the user introduces a digit and this loop at least execute once instead of while
-For: This repetitive operator is quite different from the rest you have to considerate:
– Initialization:
-It can be any value
-You can avoid that
-You can initialize more than one
– Checking:
-Simple condition
– Complex Condition
-You can avoid that, not recommended
– Itineration:
-It can be integral or decimal values.
-It can be mutables or fixed values.
Example of for: