Вы находитесь на странице: 1из 17

Loops Tricks and Tips of Loops & Switch

B.Sathis Kumar
The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. This repetitive operation is done through a loop control instruction

They are: (a) Using a for statement (b) Using a while statement (c) Using a do-while statement

General for loop


main( ) { int i ; for ( i = 1 ; i <= 10 ; i = i + 1 ) printf ( "%d\n", i ) ; }

For Loop Tips 1


main( ) { int i ; for ( i = 1 ; i <= 10 ; ) { printf ( "%d\n", i ) ; i=i+1; } }

For Loop Tips 1


main( ) { int i ; for ( i = 1 ; i <= 10 ; ) { printf ( "%d\n", i ) ; i=i+1; } }
Here, the incrementation is done within the body of the for loop and not in the for statement. Note that inspite of this the semicolon after the condition is necessary.

For Loop Tips 2


main( ) { int i =1; for ( ; i <= 10 ; i=i+1) { printf ( "%d\n", i ) ; } }

For Loop Tips 2


main( ) { int i =1; for ( ; i <= 10 ; i=i+1) { printf ( "%d\n", i ) ; i=i+1; } }
Here the initialisation is done in the declaration statement itself, but still the semicolon before the condition is necessary.

For loop tips 3


main( ) { int i = 1 ; for ( ; i <= 10 ; ) { printf ( "%d\n", i ) ; i=i+1; } }

For loop tips 3


main( ) { int i = 1 ; for ( ; i <= 10 ; ) { printf ( "%d\n", i ) ; i=i+1; } }
Here, neither the initialisation, nor the incrementation is done in the for statement, but still the two semicolons are necessary.

For loop tips 4


int main() { int i=1; for(;;) { printf("%d",i++); if(i>10) break; } getch(); }

For loop tips 4


int main() { int i=1; for(;;) { printf("%d",i++); if(i>10) break; } getch(); }
Here, the initialisation is outside for loop and incrementation and condition done inside the for loop , but still the two semicolons are necessary.

For loop tips 5


main( ) { int i ; for ( i = 0 ; i++ < 10 ; ) printf ( "%d\n", i ) ; }

For loop tips 5


main( ) { int i ; for ( i = 0 ; i++ < 10 ; ) printf ( "%d\n", i ) ; }
Here, the comparison as well as the incrementation is done through the same statement, i++ < 10. Since the ++ operator comes after i firstly comparison is done, followed by incrementation. Note that it is necessary to initialize i to 0.

For loops tips 6


main( ) { int i ; for ( i = 0 ; ++i <= 10 ; ) printf ( "%d\n", i ) ; }

For loops tips 6


main( ) { int i ; for ( i = 0 ; ++i <= 10 ; ) printf ( "%d\n", i ) ; }
Here, both, the comparison and the incrementation is done through the same statement, ++i <= 10. Since ++ precedes i firstly incrementation is done, followed by comparison. Note that it is necessary to initialize i to 0.

Nesting for loops


/* Demonstration of nested loops */ main( ) { int r, c, sum ; for ( r = 1 ; r <= 3 ; r++ ) /* outer loop */ { for ( c = 1 ; c <= 2 ; c++ ) /* inner loop */ { sum = r + c ; printf ( "r = %d c = %d sum = %d\n", r, c, sum ) ; } }

Multiple Initialisations in the for Loop


The initialisation expression of the for loop can contain more than one statement separated by a comma. For example,

Multiple Increment in the for Loop


The increment expression of the for loop can contain more than one statement separated by a comma. For example,

for ( i = 1, j = 2 ; j <= 10 ; j++ ) However, only one expression is allowed in the test expression.

for ( i = 1, j = 2 ; j <= 10 ; j++,i++ ) However, only one expression is allowed in the test expression. This expression may contain several conditions linked together using logical operators

expression may contain several conditions linked together using logical operators

Odd loop(Execution of a loop an unknown number of times )


The loops that we have used so far executed the statements within them a finite number of times. However, in real life programming one comes across a situation when it is not known beforehand how many times the statements in the loop are to be executed.

# include<stdio.h> # include<conio.h> int main() {int i,j; for (i = 0, j = 0; i < 3 && j < 3; i++, j=j+2) printf("i=%d j=%d",i,j); getch(); } Result i=0 j=0 i=1 j=2

Example
# include<stdio.h> # include<conio.h> int main() { int a=1; for(;a==1;) { printf("inside for loop"); scanf("%d",&a); } getch(); }

The break Statement


We often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test. The keyword break allows us to do this. When break is encountered inside any loop, control automatically passes to the first statement after the loop.

example
main( ) { int num, i ; printf ( "Enter a number " ) ; scanf ( "%d", &num ) ; i=2; while ( i <= num - 1 ) { if ( num % i == 0 ) { printf ( "Not a prime number" ) ; break ; } i++ ;

The continue Statement


In some programming situations we want to take the control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed. The keyword continue allows us to do this. When continue is encountered inside any loop, control automatically passes to the beginning of the loop.

} if ( i == num )
printf ( "Prime number" ) ;

example
main( ) { int i, j ; for ( i = 1 ; i <= 2 ; i++ ) { for ( j = 1 ; j <= 2 ; j++ ) { if ( i == j ) continue ; printf ( "\n%d %d\n", i, j ) ; } }

output
The output of the above program would be... 12

21

While loop
The general form of while is as shown below:

Tips1
The condition being tested may use relational or logical operators as shown in the following examples:

Tips 2

Tips 3 Indefinite loop

correct

Tips 4 Decrement loop counter

Tips 5 Increment or decrement can be float

Tips 6- boundry

Tips 6- boundry

Tips 7

Answer
A The condition in the while loop is must

The do-while Loop


The do while loop looks like this

10

Do while vs While
There is a minor difference between the working of while and do-while loops. This difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. As against this, the do-while tests the condition after having executed the statements within the loop.

Do while vs While
This means that do-while would execute its statements at least once, even if the condition fails for the first time. The while, on the other hand will not execute its statements if the condition fails for the first tim

example

Switch case Tips -1


You can put the cases in any order
main( ) { int i = 22 ; switch ( i ) { case 121 : printf ( "I am in case 121 \n" ) ; break ; case 7 : printf ( "I am in case 7 \n" ) ; break ; case 22 : printf ( "I am in case 22 \n" ) ; break ; default : printf ( "I am in default \n" ) ; } }

11

Tips 2
You are also allowed to use char values in case and switch as shown in the following program:
main( ) { char c = 'x' ; switch ( c ) { case 'v' : printf ( "I am in case v \n" ) ; break ; case 'a' : printf ( "I am in case a \n" ) ; break ; case 'x' : printf ( "I am in case x \n" ) ; break ; default : printf ( "I am in default \n" ) ; } }

Tips -3At times we may want to execute a common set of statements for multiple cases. How this can be done is shown in the following example.
main( ) { char ch ; printf ( "Enter any of the alphabet a, b, or c " ) ; scanf ( "%c", &ch ) ; switch ( ch ) { case 'a' : case 'A' : printf ( "a as in ashar" ) ; break ; case 'b' : case 'B' : printf ( "b as in brain" ) ; break ; default : printf ( "wish you knew what are alphabets" ) ; } }

Tips 4
Even if there are multiple statements to be executed in each case there is no need to enclose them within a pair of braces (unlike if, and else).

Every statement in a switch must belong to some case or the other. If a statement doesnt belong to any case the compiler wont report an error. However, the statement would never get executed. For example, in the following program the printf( ) never goes to work.

12

Tips 5
main( ) { int i, j ; printf ( "Enter value of i" ) ; scanf ( "%d, &i ) ; switch ( i ) { printf ( "Hello" ) ; case 1 : j = 10 ; break ; case 2 : j = 20 ; break ; } }

If we have no default case, then the program simply falls through the entire switch and continues with the next instruction (if any,) that follows the closing brace of switch

Tips 6
We can check the value of any expression in a switch. Thus the following switch statements are legal.
switch ( i + j * k ) switch ( 23 + 45 % 4 * k )

Tips 7
Expressions can also be used in cases provided they are constant expressions. Thus case 3 + 7 is correct, however, case a + b is incorrect.

switch ( a < 4 && b > 7 )

13

Tips 8
The break statement when used in a switch takes the control outside the switch. However, use of continue will not take the control to the beginning of switch as one is likely to believe

Tips 9
A float expression cannot tested using a switch switch (1.2) not allowed

Tips 10
Cases can never have variable expressions (for example it is wrong to say case a +3 : )

Tips 11
Multiple cases cannot use same expressions. Thus the following switch is illegal:

switch ( a ) { case 3 : ... case 1 + 2 : ... }

14

Quiz

Answer

Quiz

Answer

15

Quiz

Answer

Quiz
Point out error, if any # include<stdio.h> # include<conio.h> int main() { int a=5; switch(a) { } printf("hai"); getch(); }

answer
No error No case it come out of switch loop and it print hai

16

answer
Point out error, if any # include<stdio.h> # include<conio.h> int main() { int a=5; switch(a) { printf("hai"); case 5: printf("hai ram"); getch(); } hai ram

Goto keyword
Avoid goto keyword! They make a C programmers life miserable. There is seldom a legitimate reason for using goto, and its use is one of the reasons that programs become unreliable, unreadable, and hard to debug. And yet many programmers find goto seductive

example
Int main() { int a; scanf(%d,&a) if (a==10) goto 10 printf(%d,a); 10: printf(hai); }

17

Вам также может понравиться