Statements
The statements of a C program control the flow of program execution.In C language several kinds of statements are available. They are- if statement
- switch statement
- goto statement
- for statement
- while statement
- do-while statement
- break statement
- continue statement
- expression statement
- compound statement
- return statement
- null statement
if statements
If statement is a conditional branching statement. In conditional branching statement a condition is evaluated, if it is evaluate true a group of statement is executed. The simple format of an if statement is as follows:
if (expression)
statement;
If the expression is evaluated and found to be true, the single statement following the "if" is executed. If false, the following statement is skipped. Here a compound statement composed of several statements bounded by braces can replace the single statement.
Here's an example program using simple if statement:
if else statement:
This feature permits the programmer to write a single comparison, and then execute one of the two statements depending upon whether the test expression is true or false. The general form of the if-else statement is
if(expression)
statement1
else
statement2
Here also expression in parentheses must evaluate to (a boolean) true or false. Typically you're testing something to see if it's true, and then running a code block(one or more statements) if it is true, and another block of code if it isn't. The statement1 or statement2 can be either simple or compound statement.
The following program demonstrates a legal if else statement:
This brings up the other if-else construct, the if, else if, else. This construct is useful where two or more alternatives are available for selection.
The syntax is
if(condition)
statement 1;
else if (condition)
statement 2;
.....................
.....................
else if(condition)
statement n-1;
else
statemens n ;
The various conditions are evaluated one by one starting from top to bottom, on reaching a condition evaluating to true the statement group associated with it are executed and skip other statements. If none of expression is evaluate to true, then the statement or group of statement associated with the final else is executed.
The following program demonstrates a legal if-elseif-else statement:
c switch statement
Switch statements simulate the use of multiple if statement.The switch statement is probably the single most syntactically awkward and error-prone feature of the C language. Syntax of c switch statement is
switch(expression)
{
case constant1:
statements 1;
break;
case constant2:
statements 2;
break;
…………………..
default:
statements n;
break;
}
When the switch statement is executed, the expression in the switch statement is evaluated and the control is transferred directly to the group of statements whose case label value matches the value of the expression. Each group of statement ends with a break statement. The execution of break statement causes immediate exit from the switch statement. If break statement is not present, then the execution will falls trough all the statement in the selected case. If none of the case-label value matches the value of expression, then none of the group within the switch statement will be selected and the control is transferred directly to the statement that follows the switch statement c.
The expression that forms the argument of switch is evaluated to either char or integer. Similarly the constant expression follows the keyword case should be a value int or char. That is, names of variable can also be used. Switch can test for only equality.
Take a look at the following if-else code, and notice how confusing it can be to have nested if tests, even just a few levels deep:
if(number == 1)
{
printf("Given number is 1\n");
}
else if((number == 2)
{
printf("Given number is 2\n");
}
else if((number ==3)
{
printf("Given number is 3\n");
}
else if((number ==4)
{
printf("Given number is 4\n");
}
else if((number ==5)
{
printf("Given number is 5\n");
}
else
{
if(number<0)
printf("Given number is negative\n");
else
printf("Given number is greater than 5\n");
}
Now let's see the same functionality represented in a switch construct:
![]() |
int temp = 100;
switch(temp)
{
case 10 : printf("10");
break;
case 10 : printf("10"); // won't compile!
break;
case 100 :printf("1000");
break;
default : printf("default");
break;
}
Break and Fall-Through in switch Blocks:
When case constants are evaluated from the top to down, and the first case constant that matches the switch's expression is the execution entry point. In other words, once a case constant is matched, C will execute the associated code block, and all subsequent code blocks .
Example:
![]() |