0% found this document useful (0 votes)
10 views

Unit 3control Statements

Control structures like loops and branches allow programmers to control the flow of execution in a program. There are several types of loops in C including while, for, and do-while loops which allow code to be repeated. C also provides branching statements like if-else and switch-case to allow programs to make decisions and choose between multiple execution paths. Common looping and branching control structures are used to iterate tasks and change the normal sequential flow of a program.

Uploaded by

richa.vijay
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Unit 3control Statements

Control structures like loops and branches allow programmers to control the flow of execution in a program. There are several types of loops in C including while, for, and do-while loops which allow code to be repeated. C also provides branching statements like if-else and switch-case to allow programs to make decisions and choose between multiple execution paths. Common looping and branching control structures are used to iterate tasks and change the normal sequential flow of a program.

Uploaded by

richa.vijay
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT III

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

C programming language provides the following types of loops to handle looping


requirements.

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

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.

C supports the following control statements.

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

Transfers control to the labeled statement.

C provides two styles of flow control:

 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

This is the most simple form of the branching statements.

It takes an expression in parenthesis and an statement or block of statements. if the expression is


true then the statement or block of statements gets executed otherwise these statements are
skipped.

NOTE: Expression will be assumed to be true if its evaulated values is non-zero.

if statements take the following form:

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.

? : takes the following form:

if condition is true ? then X return value : otherwise Y value;

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.

What is default condition:

If none of the listed conditions is met then default condition executed.

#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" );
}
}

This will produce following result:

Excellent
Good
OK
Mmmmm....
You must do better than this
What is your grade anyway?

Using break statement:

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

This will produce following result:

Good

What is default condition:

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

This will produce following result:

What is your grade anyway?

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.

Basic syntax of while loop is as follows:

while ( expression )
{
Single statement
or
Block of statements;
}
#include <stdio.h> #include <stdio.h>

main() main()
{ {
int i = 10; int i = 10;

while ( i > 0 ) while ( i > 0 )


{ {
printf("Hello %d\n", i ); printf("Hello %d\n", i );
i = i -1; i = i -1;
} if( i == 6 )
} {
break;
}
}
}

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:

Basic syntax of for loop is as follows:

for( expression1; expression2; expression3)


{
Single statement
or
Block of statements;
}
#include <stdio.h>

main()
{
int i;
int j = 10;

for( i = 0; i <= j; i ++ )
{
printf("Hello %d\n", i );
}
}

In the above syntax:

 expression1 - Initialisese variables.


 expression2 - Condtional expression, as long as this condition is true, loop will keep
executing.
 expression3 - expression3 is the modifier which may be simple increment of a variable.

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.

Basic syntax of do...while loop is as follows:

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

break and continue statements

C provides two commands to control how we loop:

 break -- exit form loop or switch.


 continue -- skip 1 iteration of loop.

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

This will produce following output:

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;

In this syntax, label is an identifier.


When, the control of program reaches to goto statement, the control of the program will jump to
the label: and executes the code below it.

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.

Reasons to avoid goto statement

 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.

C library function - exit()

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

Following is the declaration for exit() function.

void exit(int status)

Parameters

 status − This is the status value returned to the parent process.

Return Value

This function does not return any 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");

printf("Exiting the program....\n");


exit(0);

printf("End of the program....\n");

return(0);
}

Let us compile and run the above program that will produce the following result −

Start of the program....


Exiting the program....

Important/Practice Question

1. Explain the concept of scope of variables.


2. What is decision making structures? Explain with its suitable examples.
3. Write Short note on
 Switch & for loop
 break & Continue
4. Explain the difference between while & do-while loop
5. Explain various branching and looping condition available in c.

You might also like