1 / 8

Computer Programming CSET101

King Fahd University of Petroleum and Minerals Hafr Al-Batin Community College Computer Science & Engineering Technology Unit (CSET). Computer Programming CSET101. Loop statements in C language Lecture-7. Loop statements in C .

ion
Télécharger la présentation

Computer Programming CSET101

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. King Fahd University of Petroleum and MineralsHafr Al-Batin Community College Computer Science & Engineering Technology Unit (CSET) Computer ProgrammingCSET101 Loop statements in C language Lecture-7

  2. Loop statements in C • Loops are used to repeat one statement or set statements more than one time. Most real programs contain some construct that loops within the program, performing repetitive actions on a stream of data or a region of memory. There are several ways to loop in C.

  3. For Loop • For loop is a counter loop. The for loop allows automatic initialization of instrumentation of a counter variable. The general form isfor (initialization; condition; increment/decrement){statements block}If the statement block is only one statement, the braces are not necessary. Although the for allows a number of variations, generally the initialization is used to set a counter variable to its starting value. The condition is generally a relational statement that checks the counter variable against a termination value, and the increment increments (or decrements) the counter value. The loop repeats until the condition becomes false.

  4. Example #include <stdio.h>void main(){inti;for(i = 0; i < count; i++){ printf(“%d\n”,i);}}

  5. While Loop • The while loop repeats a statement until the test at the top proves false.The while loop has the general form:while(condition){statement block}The while tests its condition at the top of the loops. Therefore, if the condition is false to begin with, the loop will not execute at all. The condition may be any expression. An example of a while follows. It reads characters until end-of-file is encountered.

  6. Example # include <stdio.h> void main(){int t = 0;while(t<=10) { printf(“%d\n”,t); t=t+1; } } 

  7. do-while loop • This is very similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The test then verifies the data, and loops back to read again if it was unacceptable.

  8. Example #include <stdio.h> void main(void){intval;do{ printf("Enter 1 to continue and 0 to exit :");scanf("%d\n", &val); } while (val!= 1 && val!= 0)}

More Related