A brief summary of loop processing in C language

C has a total of 3 loop statements

--for statement: Specify the number of times --while statement: Condition specification (first judgment) --do ~ while statement: Condition specification (post-judgment)

Number of times specification loop (for statement)

for statement: Specify the number of times

int i;
for (i = 1;i <=Number of iterations;i++) {
Repeated sentence;
}
Kill the loop

This can be used for all loop processing.

break;

Condition specification loop (while statement and do ~ while statement)

while statement: Condition specification (first judgment)

--Judge the conditional expression before execution

while (Conditional expression) {
Repeated sentence;
}

If you want to use it like for, write as follows

Initialization;
while (Conditional expression) {
Repeated sentence;
update;
}

do ~ while statement: Condition specification (post-judgment)

--Judge the condition after execution --Use this if you want to execute it once

――Effective when checking input. When you re-enter if you make a mistake

do {
Repeated sentence;
} while (Conditional expression);

Recommended Posts