Unit 3control Statements
Unit 3control Statements
Control Structures
Control structures form the basic entities of a “structured programming language“. We all
know languages like C/C++ or Java are all structured programming languages. Control
structures are used to alter the flow of execution of the program.
C - Loops
Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times. Given
below is the general form of a loop statement in most of the programming languages −
Loop Architecture
1 while loop
Repeats a statement or group of statements while a given condition is true. It tests the condition
before executing the loop body.
2 for loop
Executes a sequence of statements multiple times and abbreviates the code that manages the loop
variable.
3 do...while loop
It is more like a while statement, except that it tests the condition at the end of the loop body.
4 nested loops
You can use one or more loops inside any other while, for, or do..while loop.
Loop control statements change execution from its normal sequence. When execution leaves a
scope, all automatic objects that were created in that scope are destroyed.
1 break statement
Terminates the loop or switch statement and transfers execution to the statement immediately
following the loop or switch.
2 continue statement
Causes the loop to skip the remainder of its body and immediately retest its condition prior to
reiterating.
3 goto statement
Branching
Looping
Branching is deciding what actions to take and looping is deciding how many times to take a
certain action.
Branching:
Branching is so called because the program chooses to follow one branch or another.
if statement
if (expression) EXAMPLE
statement; #include <stdio.h>
or main()
{
if (expression) int cows = 6;
{
Block of statements; if (cows > 1)
} printf("We have cows\n");
or if (cows > 10)
printf("loads of them!\n");
if (expression) else
{
Block of statements; printf("Executing else part...!\n");
}
else if (cows == 5 )
{ {
Block of statements; printf("We have 5 cows\n");
} }
else if( (cows == 6 )
or {
printf("We have 6 cows\n");
if (expression) }
{ }
Block of statements;
This will produce following result:
}
else if(expression)
{ We have cows
Block of statements; Executing else part...!
} We have 6 cows
else
{
Block of statements;
}
? : Operator
The ? : operator is just like an if ... else statement except that because it is an operator you can
use it within expressions.
? : is a ternary operator in that it takes three values, this is the only ternary operator C has.
switch statement:
The switch statement is much like a nested if .. else statement. Its mostly a matter of preference
which you use, switch statement can be slightly more efficient and easier to read.
switch( expression )
{
case constant-expression1: statements1;
[case constant-expression2: statements2;]
[case constant-expression3: statements3;]
[default : statements4;]
}
Using break keyword:
If a condition is met in switch case then execution continues on into the next case clause also if it
is not explicitly specified that the execution should exit the switch statement. This is achieved by
using break keyword.
#include <stdio.h>
main()
{
int Grade = 'A';
switch( Grade )
{
case 'A' : printf( "Excellent\n" );
case 'B' : printf( "Good\n" );
case 'C' : printf( "OK\n" );
case 'D' : printf( "Mmmmm....\n" );
case 'F' : printf( "You must do better than this\n" );
default : printf( "What is your grade anyway?\n" );
}
}
Excellent
Good
OK
Mmmmm....
You must do better than this
What is your grade anyway?
You can come out of the switch block if your condition is met. This can be achieved using break
statement. Try out following example:
#include <stdio.h>
main()
{
int Grade = 'B';
switch( Grade )
{
case 'A' : printf( "Excellent\n" );
break;
case 'B' : printf( "Good\n" );
break;
case 'C' : printf( "OK\n" );
break;
case 'D' : printf( "Mmmmm....\n" );
break;
case 'F' : printf( "You must do better than this\n" );
break;
default : printf( "What is your grade anyway?\n" );
break;
}
}
Good
If none of the conditions is met then default condition is executed. Try out following example to
understand default condition.
#include <stdio.h>
main()
{
int Grade = 'L';
switch( Grade )
{
case 'A' : printf( "Excellent\n" );
break;
case 'B' : printf( "Good\n" );
break;
case 'C' : printf( "OK\n" );
break;
case 'D' : printf( "Mmmmm....\n" );
break;
case 'F' : printf( "You must do better than this\n" );
break;
default : printf( "What is your grade anyway?\n" );
break;
}
}
Looping
Loops provide a way to repeat commands and control how many times they are repeated. C
provides a number of looping way.
while loop
The most basic loop in C is the while loop.A while statement is like a repeating if statement.
Like an If statement, if the test condition is true: the statments get executed. The difference is
that after the statements have been executed, the test condition is checked again. If it is still true
the statements get executed again.This cycle repeats until the test condition evaluates to false.
while ( expression )
{
Single statement
or
Block of statements;
}
#include <stdio.h> #include <stdio.h>
main() main()
{ {
int i = 10; int i = 10;
for loop
for loop is similar to while, it's just written differently. for statements are often used to proccess
lists such a range of numbers:
main()
{
int i;
int j = 10;
for( i = 0; i <= j; i ++ )
{
printf("Hello %d\n", i );
}
}
do...while loop
do ... while is just like a while loop except that the test condition is checked at the end of the
loop rather than the start. This has the effect that the content of the loop are always executed at
least once.
do #include <stdio.h>
{
Single statement main()
or {
Block of statements; int i = 10;
}while(expression); do{
printf("Hello %d\n", i );
i = i -1;
}while ( i > 0 );
}
This will produce following output:
Hello 10
Hello 9
Hello 8
Hello 7
Hello 6
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1
You already have seen example of using break statement. Here is an example showing usage of
continue statement.
#include
main()
{
int i;
int j = 10;
for( i = 0; i <= j; i ++ )
{
if( i == 5 )
{
continue;
}
printf("Hello %d\n", i );
}
}
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
goto statements
In C programming, goto statement is used for altering the normal sequence of program execution
by transferring control to some other part of the program.
Syntax:
goto label;
.............
.............
.............
label:
statement;
Example:
//Write a C Program Which Print 1 To 10 Number Using goto statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
count: //This is Label
printf("%d\n",i);
i++;
if(i<10) {
goto count; //This jumps to label "count:"
}
getch();
}
Note:
Though goto statement is included in ANSI standard of C, use of goto statement should be
reduced as much as possible in a program.
Though, using goto statement give power to jump to any part of program, using goto
statement makes the logic of the program complex and tangled.
In modern programming, goto statement is considered a harmful construct and a bad
programming practice.
The goto statement can be replaced in most of C program with the use of break and
continue statements.
In fact, any program in C programming can be perfectly written without the use of goto
statement.
All programmer should try to avoid goto statement as possible as they can.
Description
The C library function void exit(int status) terminates the calling process immediately. Any
open file descriptors belonging to the process are closed and any children of the process are
inherited by process 1, init, and the process parent is sent a SIGCHLD signal.
Declaration
Parameters
Return Value
Example
The following example shows the usage of exit() function.
#include <stdio.h>
#include <stdlib.h>
int main () {
printf("Start of the program....\n");
return(0);
}
Let us compile and run the above program that will produce the following result −
Important/Practice Question