Hi, My name is alfred and i'm going to show u my notes about repetition using c language
1. There are 3 types of looping :For, While, Do-While
2. Syntax for repetition : FOR
ex: for(con1;con2;con3) statement;
- con1 = initialisation
- con2 = condition for stopping
- con3 = increment(+1) or decrement (-1)
*single statement looping*
or
ex: for(con1;con2;con3) {
statement1;
statement2;
}
*the example above is how to use for to repeat 2 or more statements*
3. In for there are 2 types of loop, which is :
- Infinite loop : Loop with no condition, it will loop forever and will stop by using "break;".
- Nested loop : Loop in Loop, the repetition will begin from the loop inside first.
4. While vs Do-While : The difference between while and do-while is that while will check the condition first, if it fits then it'll start to loop, meanwhile do-while will start the loop first and check the statement at the end.
5. Syntax in While : while(con1) statement;
-con1 is usually the counter for the loop
-statement is the thing that gets repeated
*single statement looping*
or
ex: while(con1) {
statement1;
statement2;
}
*the example above is how to use for to repeat 2 or more statements*
6. Syntax in Do-While : do{
statement;
}while(con1);
*"do" will keep looping until con1 is wrong*
*works for multi statements as well*
7. If the statement is wrong, "while" will not repeat it at all, but "do-while" will repeat it at least once
8. Break vs Continue
- Break will stop the loop or case in "switch-case"
- Continue will skip the statements inside a loop or skip to the next loop
Alfred.S
Binus.ac.id
2201731913
No comments:
Post a Comment