Bài giảng Lập trình C - Session 5: Loop

Understand ‘for’ loop in ‘C’

Work with comma operator

Understand nested loops

Understand the ‘while’ loop and the ‘do-while’ loop

Work with break and continue statements

Understand the exit() function

 

ppt20 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1992 | Lượt tải: 1download
Tóm tắt nội dung Bài giảng Lập trình C - Session 5: Loop, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Loop Session 5 Objectives Understand ‘for’ loop in ‘C’ Work with comma operator Understand nested loops Understand the ‘while’ loop and the ‘do-while’ loop Work with break and continue statements Understand the exit() function Elementary Programming with C/Session 5 What is a Loop? Section of code in a program which is executed repeatedly, until a specific condition is satisfied Elementary Programming with C/Session 5 Elementary Programming with C/Session 5 3 types of Loop Structures Elementary Programming with C/Session 5 The for loop-1 Syntax The evaluation parameter defines how the loop control  variable changes, each time the loop is executed Elementary Programming with C/Session 5 The three sections of the for loop must be separated by a semicolon(;) The statement, which forms the body of the loop, can  either be a single statement or a compound statement The for loop continues to execute as long as the  conditional test evaluates to true. When the condition  becomes false, the program resumes on the statement  following the for loop The for loop-2 Elementary Programming with C/Session 5 /*This program demonstrates the for loop in a C program */ 	#include 	 	main() 	{ 	int count; 	printf(“\tThis is a \n”); 	 	for(count = 1; count 	main() 	{ 	int i, j , max; 	printf(“Please enter the maximum value \n”); 	printf(“for which a table can be printed: “); 	scanf(“%d”, &max); 	 	for(i = 0 , j = max ; i 	main() 	{ 	int i, j, k; 	i = 0; 	printf("Enter no. of rows :"); 	scanf("%d", &i); 	printf("\n"); 	for (j = 0; j 	main() 	 { 	int count = 1; 	while( count main () { 	int num1, num2; num2 = 0; 	do 	{ 	 printf( "\nEnter a number : "); 	 scanf(“%d”,&num1); 	 printf( " No. is %d",num1); 	 num2++; 	} while (num1 != 0); 	printf ("\nThe total numbers entered were %d",--num2); /*num2 is decremented before printing because count for last integer (0) is not to be considered */ 	} do…while Loop-2 Example Elementary Programming with C/Session 5 Jump Statements-1 expression The return statement is used to return from a function It causes execution to return to the point at which the call to the function was made The return statement can have a value with it, which it  returns to the program Elementary Programming with C/Session 5 label Jump Statements-2 Elementary Programming with C/Session 5 statement Jump Statements-3 Elementary Programming with C/Session 5 break statement #include 	main () 	{ 	int count1, count2; 	for(count1 = 1, count2 = 0;count1 	main () 	{ 	int num; 	for(num = 1; num <=100; num++) 	{ 	if(num % 9 == 0) 	continue; 	printf("%d\t",num); 	} 	} Example Elementary Programming with C/Session 5 function The exit() is used to break out of the program The use of this function causes immediate termination  of the program and control rests in the hands of the  operating system Jump Statements-5 

File đính kèm:

  • pptBài giảng Lập trình C - Session 5 Loop.ppt
Tài liệu liên quan