Previous | Home | Next |
In looping statement how many times the action is to be perform.
While loop:
while ( expression ) { //Single statement orBlock of statements; }
Do While loop:
do { //Single statement or Block of statements; } while(expression);
For loop
for ( expression1; expression2; expression3) { //Single statement or Block of statements; }
// Nested loop, means one loop in the other loop. // For Example : for (i=0; i<10; ++i) { for (j=0; j<i; ++j) { printf ("i=%d, j=%d\n", i, j); } }
This means , we can initialize more than one variable within one loop.
for (int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) { //some block of statements }
Note: Loop is define as "the repetition of data according to the given conditions " so odd loop is that loop which only repeat the odd numbers & it leave the even numbers.
The break command allows you to terminate and exit a loop (that is, do, for, and while). You can place a break command only in the body of a looping command or in the body of a switch command. The break keyword must be lowercase and cannot be abbreviated.
In a looping statement, the break command ends the loop and moves control to the next command outside the loop. Within nested statements, the break command ends only the smallest enclosing do, for, switch, or while commands.
In a switch body, the break command ends the execution of the switch body and transfers control to the next command outside the switch body.
#include <stdio.h> #include <conio.h> void main() { clrscr(); int n; printf(" \nEnter the number :"); scanf("%d", &n); if (n < 0) { break; } }
The continue statement can only appear in a loop body. It skipped the rest of the statement body in the loop.
for (loop=0;loop<100;loop++) { if (loop==50) continue; printf ("%i\n", loop); }
goto is used for jumping from one statement to another. It is very negative approch to use goto in our program due its inherent properties of breaking the code.
goto label;
The goto statement is often combined with the if statement.
if condition then goto label;
Example
#include <stdio.h> #include <conio.h> int main() { int n = 0; loop: ; printf("\n%d", n); n++; if (n<10) { goto loop; } getch(); return 0; }
0
1
2
3
4
5
6
7
8
9
Disadvantages of goto Statement :Goto should not really be used in programs at all because they have a tendency to make code unstructured and they make programs difficult to manage when they become significantly large.
Previous | Home | Next |