The document discusses various loop control statements in C programming such as for, while, and do-while loops. It provides the syntax for each loop type and explains their usage through examples. Some key points covered include:
- The syntax and flow of for loops, including initialization, condition checking, and increment/decrement.
- Different ways to write for loops, such as with single/multiple statements, missing initialization/increment, etc.
- The syntax and usage of while and do-while loops, and how they differ from each other and for loops in terms of condition checking.
- How to create infinite loops and how to terminate them using break statements.
- The continue statement and how it can be
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
36 views
2.2C Loop Control
The document discusses various loop control statements in C programming such as for, while, and do-while loops. It provides the syntax for each loop type and explains their usage through examples. Some key points covered include:
- The syntax and flow of for loops, including initialization, condition checking, and increment/decrement.
- Different ways to write for loops, such as with single/multiple statements, missing initialization/increment, etc.
- The syntax and usage of while and do-while loops, and how they differ from each other and for loops in terms of condition checking.
- How to create infinite loops and how to terminate them using break statements.
- The continue statement and how it can be
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9
C Loop Control
Chapter covers Loop Control Statements in C Programming Language -
No Topic 1 For Loop : Introduction | Flowchart 2 Different Ways of Writing For Loop 3 Nesting of For Loop 4 While Loop Introduction 5 Infinite While Loop 6 Do-While Loop 7 Differenciation : For Loop Vs While Loop 8 Differenciation : For Loop Vs Do-While Loop 9 Differenciation : While Loop Vs Do-While Loop 10 Unconditional Jump : continue | break | exit
Understanding Looping Statement : Whenever we need to execute certain action multiple times, we need to wrap programming statement in the loop body. Take an example we are computing the sum of 2 numbers 5 times then we need to write code in the loop body. for(i=0; i<5; i++) { printf("\nEnter two Numbers : "); scanf("%d %d",&num1,&num2);
ans = num1 + num2;
printf("\nAddition of 2 Numbers is %d"); } Syntax of the For Loop : for(initialization; condition; increment/decrement) { body of loop; } Flowchart of For Loop :
Explanation of For Loop : 1. Firstly the for loop executes the initialize statement in which the subscript variable will be initialized with the initial value. 2. After the initialize statement the condition part of the loop will be executed if the condition becomes true then body of the loop will be executed otherwise the loop will be terminated 3. If the loop condition becomes true then body of the loop will be executed. After the execution of the for loop body the control goes to the third part of the loop statement i.e Expression Updation 4. After updating subscript variable control again goes to execute condition statement. Consider the following image then -
Note : 1. For Single Line of Code Opening and Closing braces are not needed. 2. There can Exist For Loop without body. 3. Initialization , Incrementation and Condition steps are on same Line. 4. Like While loop , For Loop is Entry Controlled Loop.[i.e conditions are checked if found true then and then only code is executed ] Different Ways of Using For Loop in C Programming In order to do certain actions multiple times , we use loop control statements. For loop can be implemented in different verities of using for loop - 1. Single Statement inside For Loop 2. Multiple Statements inside For Loop 3. No Statement inside For Loop 4. Semicolon at the end of For Loop 5. Multiple Initialization Statement inside For 6. Missing Initialization in For Loop 7. Missing Increment/Decrement Statement 8. Infinite For Loop 9. Condition with no Conditional Operator.
Different Ways of Writing For Loop in C Programming Language Way 1 : Single Statement inside For Loop for(i=0;i<5;i++) printf("Hello"); 1. Above code snippet will print Hello word 5 times. 2. We have single statement inside for loop body. 3. No need to wrap printf inside opening and closing curly block. 4. Curly Block is Optional. Way 2 : Multiple Statements inside For Loop for(i=0;i<5;i++) { printf("Statement 1"); printf("Statement 2"); printf("Statement 3");
if(condition) { -------- -------- } } If we have block of code that is to be executed multiple times then we can use curly braces to wrap multiple statement in for loop. Way 3 : No Statement inside For Loop for(i=0;i<5;i++) {
} this is bodyless for loop. It is used to increment value of i. This verity of for loop is not used generally. At the end of above for loop value of i will be 5. Way 4 : Semicolon at the end of For Loop for(i=0;i<5;i++); 1. Generally beginners thought that , we will get compile error if we write semicolon at the end of for loop. 2. This is perfectly legal statement in C Programming. 3. This statement is similar to bodyless for loop. (Way 3) Way 5 : Multiple Initialization Statement inside For for(i=0,j=0;i<5;i++) { statement1; statement2; statement3; } Multiple initialization statements must be seperated by Comma in for loop. Way 6 : Missing Increment/Decrement Statement for(i=0;i<5;) { statement1; statement2; statement3; i++; } however we have to explicitly alter the value i in the loop body. Way 7 : Missing Initialization in For Loop i = 0; for(;i<5;i++) { statement1; statement2; statement3; } we have to set value of i before entering in the loop otherwise it will take garbage value of i. Way 8 : Infinite For Loop i = 0; for(;;) { statement1; statement2; statement3;
if(breaking condition) break;
i++; } Infinite for loop must have breaking condition in order to break for loop. otherwise it will cause overflow of stack. While Loop Syntax : initialization; while(condition) { ---------- ---------- ---------- ---------- ---------- ------ incrementation; } Note : For Single Line of Code Opening and Closing braces are not needed. while(1) is used for Infinite Loop Initialization , Incrementation and Condition steps are on different Line. While Loop is also Entry Controlled Loop.[i.e conditions are checked if found true then and then only code is executed ] Different Ways of Infinite While Loop in C We can use While loop for looping purpose. We can use infinite while loop in C to iterate loop for infinite times. We can Even use Infinite while loop for operation in which we cannot decide how many iteration does it take at compile time.
Way 1 : Semicolon at the end of While #include<stdio.h> void main() { int num=300; while(num>255); //Note it Carefully printf("Hello"); } Output : It won't Print anything Explanation of Above Infinite Loop : 1. In the above program , Condition is specified in the While Loop 2. Semicolon at the end of while indicated while without body. 3. In the program variable num doesnt get incremented , condition remains true forever. 4. As Above program does not have Loop body , It wont print anything
Way 2 : Non-Zero Number as a Parameter #include<stdio.h> void main() { while(1) printf("Hello"); } Output : Infinite Time "Hello" word How ? 1. Note : We can specify any Non-Zero Positive Number inside while 2. Non Zero is specified in the While Loop means Loop will have always TRUE condition specified inside. 3. As condition inside loop doesnt get changed ,condition inside while remains true forever.
Way 3 : Subscript Variable Remains the same #include<stdio.h> void main() { int num=20; while(num>10) { printf("Hello"); printf(" C "); } } Output : Infinite Time "Hello C" word How ? 1. Condition is specified in while Loop ,but Terminating Condition is not specified 2. Also Subscript Variable [Variable used to Repeat action] is also not either incremented or decremented 3. so while remains true forever.
Way 4 : Character as a Parameter in While Loop #include<stdio.h> void main() { while('A') printf("Hello"); } Output : Infinite Time "Hello" word How ? 1. Character is Represented in integer in the form of ASCII internally. 2. Any Character is Converted into Non-zero Integer ASCII value 3. Any Non-zero ASCII value is TRUE condition , that is why Loop executes forever How to Break Infinite While Loop : Whenever you decide to use infinite while loop while writing code , you must include terminating or ending condition in a while loop to ensure that your code will be terminated at some moment of time. X=10; while(1) { printf("%d",x); x--;
Note : It is Exit Controlled Loop. Initialization , Incrementation and Condition steps are on different Line. It is also called Bottom Tested [i.e Condition is tested at bottom and Body has to execute at least once ]
Continue Statement : loop { continue; //code }
Note : It is used for Skipping part of Loop. Continue causes the remaining code inside a loop block to be skipped and causes execution to jump to the top of the loop block
Loop Use of Continue !! for
while
do- while
Break Statement Simply Terminate Loop and takes control out of the loop. Break in For Loop : for(initialization ; condition ; incrementation) { Statement1; Statement2; break; }
Break in While Loop : initialization ; while(condition) { Statement1; Statement2; incrementation break; }
Break Statement in Do-While : initialization ; do { Statement1; Statement2; incrementation break; }while(condition);