C Programming and Data Structures: Unit I: C Programming Fundamentals

Decision Making and Conditional Statements

statement, syntax, example C program

Various control structures are - 1. if statement 2. while statement 3. do-while statement 4. switch case statement 5. for loop

Decision Making and Conditional Statements

AU: Dec.-19, Marks 13

Various control structures are -

1. if statement

2. while statement

3. do-while statement

4. switch case statement

5. for loop

Let us discuss these control structures with the help of simple examples

 

1. If statement

There are two types of if statements - simple if statement and compound if statement. The simple if statement is a kind of if statement which is followed by single statement. The compound if statement is a kind of if statement for which a group of statements are followed. These group of statements are enclosed within the curly brackets.

If statement can also be accompanied by the else part.

Following table illustrates various forms of if statement


 

2. While statement

The while statement is executing repeatedly until the condition is false. The while statement can be simple while or compound while. Following table illustrates the forms of while statements –


 

3. do…while

The do...while statement is used for repeated execution. The difference between do while and while statement is that, in while statement the condition is checked before executing any statement whereas in do while statements the statement is executed first and then the condition is tested. There is at least one execution of statements in case of do...while. Following table shows the use of do while statement.



Note that the while condition is terminated by a semicolon.

 

4. Switch Case statement

From multiple cases if only one case is to be executed at a time then switch case statement is executed. Following table shows the use of switch case statements



5. for Loop

The for loop is a not statement it is a loop, using which the repeated execution of statements occurs. Following table illustrates the use of for loop -


 

Ex. 1.7.1 Write output of the following C code:

i) #include <stdio.h>

main ( )

{

int x=3;

float y=3.0;

if (x==y)

printf ("\nx and y are equal");

else

printf ("\nx and y are not equal");

}

ii) #include <stdio.h>

void main( )

{

int i=0;

for (; i ;)

printf ("\n Here is some mail for you");

iii) #include <stdio.h>

main( )

{

int x=4;

while (x==1)

{

x = x-1;

printf ("\n%d", x);

- - X;

}

}

Sol. :

i) x and y are equal

ii) The printf statement will not be executed. Hence there will not be any message on the console.

iii) The printf statement will not be executed.As x=4 and the condition in while loop gets false. The control will not enter in the loop at all. Hence there will not be any message on the console.

 

Ex. 1.7.2 Explain use of "break" and "continue" keywords in "C" with suitable example.

 Sol. : The break statement is used to terminate the execution of the loop. It can be used to break the looping of for, while, do-while and switch.

For example:

while(i<10)

{

printf("%d", i)

if(i==5) when i reaches to the value 5 the

while loop will be terminated.

break;

 i++;

}

The continue statement is used to continue the execution of the control loop.

For example:

for(i=0; i<3; i++)

{

for(j=0; j<3; j++)

{

if(a[i]= =b[j])→ continue statement skips this value

continue;

else

printf("%d %d", a[i],b[j]);

}

}

By above code if a[i] = b[j] simply go back and increment j. Hence if a[i] b[j] then both a[i] and b[j] will be displayed.

Review Questions

1. Explain Control Statement in C

2. Describe the decision making, branching and looping statements in C.

AU: Dec.-19, Marks 13


C Programming and Data Structures: Unit I: C Programming Fundamentals : Tag: : statement, syntax, example C program - Decision Making and Conditional Statements