Академический Документы
Профессиональный Документы
Культура Документы
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
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
# 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(); }
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++ ;
} 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
correct
Tips 6- boundry
Tips 6- boundry
Tips 7
Answer
A The condition in the while loop is must
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
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.
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:
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