C – Case control statements

The statements which are used to execute only specific block of statements in a series of blocks are called case control statements There are 4 types of case control statements in C language They are, 1. switch 2. break 3. continue 4. goto

1 switch case statement in C:
– Switch case statements are used to execute only specific case statements based on the switch expression – Below is the syntax for switch case statement

• switch (expression) { case label1: statements; break; case label2: statements; break; default: statements; break; }

break. } . break. break. break.3. case 3: printf("Value is 3 \n" ). } return 0. switch(value) { case 1: printf("Value is 1 \n" ). case 2: printf("Value is 2 \n" ). default : printf("Value is other than 1. case 4: printf("Value is 4 \n" ).4 \n" ).2.Example program for switchcase statement in C: • • • • • • • • • • • • • • • • • • • • • • • • • • #include <stdioh> int main () { int value = 3.

statement 2.For Loop Basic syntax to use ‘for’ loop is: for (variable initialization. condition to control loop. iteration of variable) { statement 1. } Variable initialization is the initialization of counter of loop Condition is any logical condition that controls the number of times the loop statements are executed Iteration is the increment/decrement of counter .

.j++) printf("j”).Loop can run infinitely if condition is set to TRUE always or no condition is specified For example: for (.) If loop contain only one statement then braces are optional. generally it is preferred to use braces from readability point of view for (j=0.j<5..

scanf("%d". scanf("%f".&n). for(i=1.n.i).h> #include<conio.h> void main() { float num.# include <stdio. //for loop breaks if num<0.&num). printf("Maximum no of inputs\n"). printf("Average=%2f". int i.0 sum= sum+ num. } .++i) { printf("Enter n%d: ".i<=n.average).sum. getch().average. if(num<0. } average=sum/(i-1).0) break.

.continue Statement It is sometimes desirable to skip some statements inside the loop. Just like break. Syntax of continue Statement continue. continue is also used with conditional if statement. In such cases. continue statements are used.

getch().num.i).product=1. / *In this program.h> void main() { int i.i<=4.product). it skips the statement product*=num and continue the loop.product. for(i=1.# include <stdio.h> #include<conio. */ product*=num. when num equals to zero. } . } printf("product=%d".&num).++i) { printf("Enter num%d:". scanf("%d". if(num==0) continue.

.. making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten so that it doesn't need the goto. Here label can be any plain text except C keyword and it can be set anywhere in the C program above or below to goto statement. goto label. NOTE: Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program. . label: statement. .goto statement in C A goto statement in C programming language provides an unconditional jump from the goto to a labeled statement in the same function.

a). } .h> void main () { /* local variable definition */ int a = 10. goto LOOP. } printf("value of a: %d\n". } while( a < 20 ). a++. /* do loop execution */ LOOP:do { if( a == 15) { /* skip the iteration */ a = a + 1. getch().h> #include<conio.#include <stdio.

printf("Armstrong numbers in given range are: ").num++){ temp=num. } if(sum==num) printf("%d ".r.h> #includ<conio. scanf("%d".h> void main() { int num. while(temp!=0){ r=temp%10.num<=max. sum=sum+(r*r*r). printf("Enter the minimum range: "). scanf("%d". } .num). for(num=min. temp=temp/10.&max).&min).sum. printf("Enter the maximum range: "). sum = 0.temp. } getch().#include<stdio.max. int min.

h> void main() { int n. second = 1. printf("First %d terms of fibonacci series are :-\n". } getch().h> #include<conio. } . c < n . for ( c = 0 .#include<stdio. second = next. scanf("%d". c.next).&n). else { next = first + second.n). first = second. first = 0. printf("Enter the number of terms\n"). return 0. c++ ) { if ( c <= 1 ) next = c. next. printf("Fibonacci Series in C using for loop\n\n"). } printf("%d\n".

printf("FIBONACCI SERIES: "). long int i=0l. } .k++) { f=i+j. printf(" %ld".h> void main() { int k. printf("Enter the number range:").f. scanf("%d".j) for(k=2.i. i=j. } getch().k<r. printf("%ld %ld".r.j).h> #include<conio. j=f.&r).j=1.#include<stdio.

&n).h> #include<conio. printf("%d in binary number system is:\n". } printf("\n"). scanf("%d". c--) { k = n >> c. for (c = 31. getch(). else printf("0").#include <stdio.h> void main() { int n. if (k & 1) printf("1"). n). } . c. c >= 0. printf("Enter an integer in decimal number system\n"). k.

rows.h> #include<conio.#include<stdio.h> void main() { int i. printf("Enter the number of rows: ").i<=rows. } getch(). for(i=1.j<=i.++j) { printf("* "). } * ** *** **** ***** . scanf("%d".j.&rows). } printf("\n").++i) { for(j=1.

} getch().h> #include<conio. printf("Enter the number of rows: ").j<=i. } 1 12 123 1234 12345 .j).#include<stdio.rows.++j) { printf("%d ".i<=rows. scanf("%d". } printf("\n").++i) { for(j=1.j.h> int main() { int i.&rows). for(i=1.

++temp. char input. } .A #include<stdio.++j) printf("%c". } getch(). temp='A'.h> BB #include<conio. printf("\n").h> CCC int main() DDDD { EEEEE int i.++i) { for(j=1. for(i=1. scanf("%c".&input).i<=(input-'A'+1).temp).j<=i.j. printf("Enter uppercase character you want in triangle at last row: ").

h> 01 void main() 010 { 0101 int i. } getch().#include<conio. clrscr().j++) { if(j%2==0) printf("0").i<n.h> 0 #include<stdio. printf("\nEneter the no of lines to be printed: ").j<=i.j. } . for(i=0. scanf("%d".i++) { for(j=0.n. } printf("\n"). else printf("1").&n).

Initialization of one-dimensional array: Arrays can be initialized at declaration int age[5]={2.4.3. .4.3.ARRAY An array is a sequence of data item of homogeneous value(same type). For example: int age[5].34.4}.  It is not necessary to define the size of arrays during initialization.34. int age[]={2.4}. Arrays are of two types: One-dimensional arrays Multidimensional arrays Declaration of one dimension Array data_type array_name[array_size].

.. specify c[4] – Array name c[5] – Position number c[6] c[7] Format: c[8] arrayname[ position number ] c[9] – First element at position 0 c[10] c[11] – n element array named c: • c[ 0 ].c[ n – 1 ] c[0] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 Position number of the element within array c . c) Group of consecutive memory locations c[1] – Same name and type c[2] c[3] To refer to an element. c[ 1 ].Arrays • Array Name of array (Note that all elements of this array have the same name.

c[ 0 ] ). Perform operations in subscript. printf( "%d". If x equals 3 c[ 5 .2 ] == c[ 3 ] == c[ x ] .Array elements are like normal variables c[ 0 ] = 3.

getch().i<5. } Printf(“Array elements are\n”). } .i<5. a*i+).i++) { printf(“\n Enter a[%d] elements:".INPUT 5 ELEMENTS & DISPLAY IT Output: Enter a[0] elements:23 Enter a[1] elements:13 Enter a[2] elements:33 Enter a[3] elements:53 Enter a[4] elements:5 Array elements are a[0]=23 a[0]=13 a[0]=33 a[0]=53 a[0]=5 #include<stdio.h> void main() { Int a[5].h> #include<conio.i++) printf(“a*%d+=%d\n”. scanf(“%d”. I). &a*i+). for(i=0. Int i. Clrscr(). for(i=0.

n.#include<stdio.sum=0.&n).h> #include<conio. printf("Enter number of students: "). scanf(“%d”. getch().i++) { printf(“Enter the marks of student %d”.h> int main() { int marks[10]. for(i=0.&marks[i]).i+1).i<n.sum).i. scanf("%d". } . sum+=marks[i]. } printf(“sum=%d”.

scanf("%ld". getch().h> int main() { long int decimalNumber.quotient.remainder. while(quotient!=0) { binaryNumber[i++]= quotient % 2.i=1.j.decimalNumber). quotient = quotient / 2. int binaryNumber[100].j> 0.j--) printf("%d".&decimalNumber).h> #include<conio.#include<stdio. } printf("Equivalent binary value of decimal number %d: ".binaryNumber[j]). for(j = i -1 . printf("Enter any decimal number: "). quotient = decimalNumber. } .

1 }. 6. i. 7. 5. 3. 15.1 /* Fig. "Element". 13.  2000 Prentice H 23 } . 18 19 printf( "\n" ). 12 13 for ( i = 0. 17.8: fig06_08. '*' ). 20 } 21 22 return 0. "Value". i <= SIZE . j. 9 int i. "Histogram" ). 9. j <= n[ i ].h> 4 #define SIZE 10 5 6 int main() 7 { 8 int n[ SIZE ] = { 19. 11. i++ ) { 14 printf( "%7d%13d ". n[ i ]) .c Outline 2 Histogram printing program */ 3 #include <stdio. 15 16 for ( j = 1. j++ ) /* print one bar */ 17 printf( "%c". 10 11 printf( "%s%13s%17s\n".1.

'\0' }. – Can access individual characters string1[ 3 ] is character „s‟ – Array name is address of array. • Null character '\0' terminates strings • string1 actually has 6 elements – It is equivalent to char string1[] = { 'f'. 's'. be careful . 't'. 'i'. • Reads characters until whitespace encountered • Can write beyond end of array. string2 ). so & not needed for scanf scanf( "%s".Strings • Character arrays – String “first” is really a static array of characters – Character arrays can be initialized using string literals char string1[] = "first". 'r'.

h> #include<conio. getch().h> void main() { Char str*+=“ Tulika”. Clrscr().DEMO OF C STRING OUTPUT: HELLO TULIKA #include<stdio.str). printf(“Hello %s\n”. } .

clrscr(). getch().STRING Vs CHARACTER ARRAY Hello Tulika #include<stdio.’k’.h> #include<conio.’I’.’u’.‘T’.’a’-.str).h> void main() { char str*+=.’l’. printf(“Hello %s\n”. } .

strrev ( ) reverses the given string . strcmp ( ) Returns 0 if str1 is same as str2. 5. 6. strlen ( ) gives the length of str1.C String functions: String. String functions Description 1. strcat ( ) Concatenates str2 at the end of str1. strcpy ( ) Copies str2 into str1 3. 2. All the string functions are given below. strncpy ( ) copies given number of characters of one string to another 4.h header file supports all the string functions in C language. Returns <0 if strl < str2. Returns >0 if str1 > str2.