100% found this document useful (1 vote)
95 views30 pages

C - Case Control Statements

The document discusses various case control statements in C language. It describes switch case statement syntax and provides an example program. It also explains for, continue, goto and break statements with examples. Key case control statements covered are switch, break, continue and goto. Various loop examples like printing patterns, arrays, strings are also provided.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
95 views30 pages

C - Case Control Statements

The document discusses various case control statements in C language. It describes switch case statement syntax and provides an example program. It also explains for, continue, goto and break statements with examples. Key case control statements covered are switch, break, continue and goto. Various loop examples like printing patterns, arrays, strings are also provided.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

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; }

Example program for switchcase statement in C:


#include <stdioh>
int main () { int value = 3; switch(value) { case 1: printf("Value is 1 \n" ); break; case 2: printf("Value is 2 \n" ); break; case 3: printf("Value is 3 \n" ); break; case 4: printf("Value is 4 \n" ); break; default : printf("Value is other than 1,2,3,4 \n" ); } return 0; }

For Loop
Basic syntax to use for loop is:

for (variable initialization; condition to control loop; iteration of variable) { statement 1; statement 2; }
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

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;j++) printf("j);

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

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

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

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. NOTE: Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, 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.
goto label; .. . label: statement;

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.

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

#include<stdio.h> #includ<conio.h> void main() { int num,r,sum,temp; int min,max;

printf("Enter the minimum range: "); scanf("%d",&min);

printf("Enter the maximum range: "); scanf("%d",&max);

printf("Armstrong numbers in given range are: "); for(num=min;num<=max;num++){ temp=num; sum = 0;

while(temp!=0){ r=temp%10; temp=temp/10; sum=sum+(r*r*r); } if(sum==num) printf("%d ",num); }

getch(); }

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

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

#include

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

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

* ** *** **** *****

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

1 12 123 1234 12345

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

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

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

Arrays
Array

Name of array (Note that all elements of this array have the same name, c)

Group of consecutive memory locations c[1] Same name and type c[2] c[3] To refer to an element, 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[ 1 ]...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

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

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> #include<conio.h> void main() { Int a[5]; Int i; Clrscr(); for(i=0;i<5;i++) { printf(\n Enter a[%d] elements:", I); scanf(%d, &a*i+); } Printf(Array elements are\n); for(i=0;i<5;i++) printf(a*%d+=%d\n, a*i+); getch(); }

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

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

printf("%d",binaryNumber[j]);
getch(); }

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

Strings
Character arrays
String first is really a static array of characters Character arrays can be initialized using string literals
char string1[] = "first";

Null character '\0' terminates strings string1 actually has 6 elements


It is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '\0' };

Can access individual characters


string1[ 3 ] is character s

Array name is address of array, so & not needed for scanf


scanf( "%s", string2 );

Reads characters until whitespace encountered Can write beyond end of array, be careful

DEMO OF C STRING
OUTPUT: HELLO TULIKA

#include<stdio.h> #include<conio.h> void main() { Char str*+= Tulika; Clrscr(); printf(Hello %s\n,str); getch(); }

STRING Vs CHARACTER ARRAY


Hello Tulika

#include<stdio.h> #include<conio.h> void main() { char str*+=,T,u,l,I,k,a-; clrscr(); printf(Hello %s\n,str); getch(); }

C String functions: String.h header file supports all the string functions in C language. All the string functions are given below.

String functions Description 1. strcat ( ) Concatenates str2 at the end of str1. 2. strcpy ( ) Copies str2 into str1 3. strncpy ( ) copies given number of characters of one string to another 4. strlen ( ) gives the length of str1. 5. strcmp ( ) Returns 0 if str1 is same as str2. Returns <0 if strl < str2. Returns >0 if str1 > str2. 6. strrev ( ) reverses the given string

You might also like