Full Transcript

PRG 155 – Programming Fundamentals Using C 7. C Loop Control Statements Loop statements allow to execute a statement, or a group of statements, multiple times. Types of loops in C programming:  for loop  while loop  do...while loop Jump statements alter the normal execution sequence of a progr...

PRG 155 – Programming Fundamentals Using C 7. C Loop Control Statements Loop statements allow to execute a statement, or a group of statements, multiple times. Types of loops in C programming:  for loop  while loop  do...while loop Jump statements alter the normal execution sequence of a program:  continue – used to skip some statements inside the loop  break – used to terminate the execution of loop and switch-case statements  goto – used to jump from one statement to another within a function for Loop for loop is usually used when the number of iterations is known. Syntax for (initialization; test expressions; update expression) { statements; } for Statement Flowchart 1 PRG 155 – Programming Fundamentals Using C How it works? 1. The initialization is executed first and only once to initialize the loop control variable/counter 2. The test expression is evaluated  If test expression is found to be false (0), the loop statements will not be executed. Program control jumps to the next statement after the “for” loop.  If test expression is found to be true, the statements within the loop will be executed AND The update expression is executed. It will update the loop control variable/counter. 3. Step 2 is repeated until the test expression becomes false or loop is terminated using break statement. Example: #include int main(){ int n, counter, sum=0; printf("Enter integer number: "); scanf("%d", &n); for(counter=1; counter

Use Quizgecko on...
Browser
Browser