Beruflich Dokumente
Kultur Dokumente
Statement is a part of
your program that can be executed. In other words every
statement in your program alone or in combination specifies an
action to performed by your program. C provides variety of
statements to help you attain any function with maximum
flexibility and efficency. One of the reason for popularity of C is
because of the extreme power provided to programmer in C due
to it rich and diverse set of statements define in C. For becoming
a top notch programmer you must have clear understanding of
the C statments and the situations where staments in C are
applicable.
Contents -
1. Type of Statements
2. if - else Statement
3. switch Statement
4. for Statement
5. while Statement
6. do Statement
7. return Statment
8. goto Statement
9. break Statment
10. continue Statements
11. Expressions Statements
12. Block Statements
1. Types Of Statements
Block Statements
2. if - else Statement
if (condition) statement;
else statement;
#include <stdio.h>
#include <string.h>
{
if ( !strcmp (value,"Password")// Strcmp functions return zero
if string passed are same otherwise non - zero.
{
printf ("\nRight");
}
}
void ifandelse (char value []);
{
if ( !strcmp (value,"Password")
{
printf ("\nRight");
}
else
{
printf ("\nWrong");
}
}
int main ()
{
char value [1000];
printf ("Enter Password : ");
scanf ("%s", value);
return 0;
}
if (condition1) statement1;
if (condition2) statement2;
if (condition 3) statement3;
............. n levels;
else statement n+1
else statement n+2
else statement n+3
if (condition1) statement1
else
if (condition2) statement2
else
.
.
.
.
... n height
if (condition n+1) statement n+1
else
if (condition n+2) statement n+2
else
statement n+3 // Final Statement
3. switch Statement
switch (input) {
case constant1 :
statement1;
break;
case constant2 :
statement2;
break;
case constant3 :
statement3;
break;
.
.
.
.
... n cases
case constant n+1 :
statement n+1;
break;
case default :
statement n+2;
break;
It is defined by C that a compiler must support a minmum of
257 case statements in C89 and 1023 case statements in C99
though you may want to keep this to minimum in consideration
of efficiency.
#include <stdio.h>
switch (ch)
{
case 'A' : printf ("You Entered A");
break;
case 'B' : printf ("You Entered B");
break;
case 'C' : printf ("You Entered C");
break;
default : printf ("You Didnot Entered A, B or C");
}
return 0;
}
switch (ch)
{
case 'A' : printf ("You Entered A");
printf ("Enter Number 1 or 2);
scanf ("%d", &num);
switch (num)
{
case 1 : printf ("You Entered 1");
break;
case 2 : printf ("You Entered 1");
break;
default : printf ("You Did not Entered 1 or 2");
}
break;
case 'B' : printf ("You Entered B");
printf ("Enter Number 1 or 2);
scanf ("%d", &num);
switch (num)
{
case 3 : printf ("You Entered 1");
break;
case 4 : printf ("You Entered 1");
break;
default : printf ("You Did not Entered 1 or 2");
}
break;
default : printf ("You Did not Entered A or B");
}
return 0;
}
4. for Statement
#include <stdio.h>
#include <stdio.h>
#include <stdio.h>
#include <stdio.h>
#include <stdio.h>
5. while Statement
while (condition)
statment;
#include <stdio.h>
6. do - while Statment
do {
statement;
}
while (condition);
#include <stdio.h>
int main ()
{
int i = 0;
do
{
return 0;
}
7. return Statement
#include <stdio.h>
int b;
b = scanf ("%d", &b);
return b; // returns the value of b to the calling function.
}
int main ()
{
printf ("\n%d", function ());
return 0;
}
8. goto Statement
Goto is used for jumping from one point to another point in your
function. You can not jump from one function to another. Jump
points or way points for goto are marked by label statements.
Label statement can be anywhere in the function above or below
the goto statement. Special situation in which goto find use is
deeply nested loops or if - else ladders.
.
.
.
goto label1;
.
.
.
.
label1 :
.
.
.
label2 :
.
.
.
.
goto label2;
To further clarify the concpet of goto statement study the C
Source Code below -
#include <stdio.h>
9. break Statement
#include <stdio.h>
int main ()
{
for (int i = 0; i<100; i++)
{
printf ("\n%d", i);
if (i == 10); // This code prints value only upto 10.
break;
}
return 0;
}
#include <stdio.h>
int main ()
{
for (int i = 0; i<100; i++)
{
if (i == 10); // This code prints value only upto 9 even though
loop executes 100 times.
continue ;
printf ("\n%d", i);
}
return 0;
}
#include <stdio.h>
{
return 0;
}
}