Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Semester: - II Subject: - Computer Programming A.Y.

:- 2022-2023

Chapter No: 2

Why Do We Use Control Statements in C?


In C language, the control of the program flows from a given instruction to another. This type
of control flow that occurs from any given command to another is known as the sequential
control flow. Now, in any C program, a programmer might want to repeat some sets of
instructions or even skip the instructions when they are writing logic. Declarations in C, also
known as control declarations or decision-making, help them in making such decisions.
Conditional statements are used in the C programming language for making certain decisions
on the basis of the available conditions. These conditional statements get executed sequentially
in case no condition is present around the statements.
Whenever we put a condition for the block statements, the flow of execution may get altered
on the basis of the result that is evaluated by the condition in the program. The process
mentioned here is known as decision in making in the C language.
Control Statements Types Used in C Language
The C language provides support for the following set of statements in its program:
1. If Statements
2. Switch Statement
3. Conditional Operator Statement
4. Goto Statement
5. Loop Statements
The If Statements This type of statement would enable a programmer to choose various instruction sets
on the basis of the available condition. The instruction sets will only get executed when the evaluation
of the condition turns out to be true.
In case the evaluation of the condition is false, there will be an execution of a different instruction set.
These are also known as decision control statements.
These are of the following types:
• Simple else or Null else
• Else if ladder
• Nested if
• If… else

Prof. Vandana V.
If Statement
The if statement is used to check some given condition and perform some operations depending
upon the correctness of that condition. It is mostly used in the scenario where we need to
perform the different operations for the different conditions. The syntax of the if statement is
given below.

1. if(expression){
2. //code to be executed
3. }

FLOWCHART

Prof. Vandana V.
EXAMPLE (Simple if)

#include<stdio.h>
int main()
{
int number=0;
printf("Enter a number:");
scanf("%d", &number);

if( number%2 == 0)
{
printf("%d is even number", number);
}
printf(“End of Program”);
return 0;
}

If-else Statement
The if-else statement is used to perform two operations for a single condition. The if-else
statement is an extension to the if statement using which, we can perform two different
operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness
of the condition. Here, we must notice that if and else block cannot be executed
simiulteneously. Using if-else statement is always preferable since it always invokes an
otherwise case with every if condition. The syntax of the if-else statement is given below.

1. if(expression){
2. //code to be executed if condition is true
3. }else{
4. //code to be executed if condition is false
5. }

Prof. Vandana V.
Flowchart of the if-else statement in C

EXAMPLE
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d", &age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
return 0;
}

Prof. Vandana V.
The Nested If Statement
In this case, the condition available in the next if statement (the second statement) will only get
evaluated if the evaluation of the condition available in the first statement turns out to be true. This
occurs throughout the program that has a nested statement

Decision making structures require that the programmer specifies one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false.

FLOWCHART

EXAMPLE
int main()
{
int age, sal;

printf("\n Enter Your Age : ");


scanf("%d", &age);
printf("\n Enter Your Salary : ");
scanf("%d", &sal);

Prof. Vandana V.
if(age > 50)
{
if( sal > 45000)
{
sal = sal +10000;
printf("\n Incremented Salary =%d ",sal);
}
else
{
sal = sal + 5000;
printf("\n Incremented Salary = %d",sal);
}
}
else
{
sal = sal +3000;
printf("\n Incremented Salary = %d",sal);
}
printf("\n \nEND OF PROGRAM\n");
return 0;
}

Prof. Vandana V.
The Else If Ladder
In this statement, the execution of an array of instructions occurs only when the available
condition is correct. The verification of the next condition occurs when this first condition is
incorrect. In case all of the specifications fail even after the verification, then there will be an
execution of the default block statements. The remainder of the program’s ladder is shown
below,

Syntax:

if(boolean_expression 1)
{ /* Executes }
else if( boolean_expression 2)
{ /* Executes }
else if( boolean_expression 2)
{ /* Executes }
else if( boolean_expression 2)
{ /* Executes }
else
{ /* executes }

Prof. Vandana V.
EXAMPLE

#include <stdio.h>
int main () {
int a = 100,b=80,c=90;
if( a >> b && a>>c )
{
printf(“a is greater" );
}
else if(b>>a && b>>c )
{
printf(“b is greater\n " );
}
else if( c>>a && c>>b )
{

Prof. Vandana V.
printf(“ C is greater\n" );
}
else
{
printf(“ All are Equal\n" );
}
Printf(“End of Program”);
return 0;
}

2. The Switch Statements


The C language offers its users with a selection statement in various ways in case a program becomes
difficult to read with an increased number of conditions. A switch statement is a multi way type of
selection statement that would resolve this issue. The switch declaration comes into play when more
than three alternatives (conditions) exist in a program. This command then switches between all the
available blocks on the basis of the expression value. Then, each block has a corresponding value with
it. The syntax for this statement is as follows:
Switch (expression_A)
{
Label case_A: Statement A (sA);
Break;
Label case_B: Statement B (sB);
Break;
Label case_C;
Statement C (sC);
Break;
Label case_Z:
Statement Z (sZ);
Break;
Default:
Statement_1 (s1);
Break;
}

Prof. Vandana V.
Every block is shown here with the use of the case keyword. As a matter of fact, the case keyword is
also followed by the block label. Note that the break statement and default block statement are very
optional in the case of the switch statement.

FLOWCHART

Prof. Vandana V.
EXAMPLE
#include <stdio.h>
int main()
{
int x = 10, y = 5;
switch(x>y && x+y>0)
{
case 1:
printf("hi");
break;
case 0:
printf("bye");
break;
default:
printf(" Hello bye ");
}
return 0;
}

Prof. Vandana V.
DIFFERENCE IN IF ELSE AND SWITCH

If-else switch

Definition Depending on the condition in the 'if' The user will decide which statement is to be
statement, 'if' and 'else' blocks are executed.
executed.

Expression It contains either logical or equality It contains a single expression which can be
expression. either a character or integer variable.

Evaluation It evaluates all types of data, such as It evaluates either an integer, or character.
integer, floating-point, character or
Boolean.

Sequence of First, the condition is checked. If the It executes one case after another till the break
execution condition is true then 'if' block is keyword is not found, or the default statement
executed otherwise 'else' block is executed.

Default execution If the condition is not true, then by If the value does not match with any case, then
default, else block will be executed. by default, default statement is executed.

Editing Editing is not easy in the 'if-else' Cases in a switch statement are easy to maintain
statement. and modify. Therefore, we can say that the
removal or editing of any case will not interrupt
the execution of other cases.

Speed If there are multiple choices If we have multiple choices then the switch
implemented through 'if-else', then the statement is the best option as the speed of the
speed of the execution will be slow. execution.

Prof. Vandana V.
3. The Goto Statement
The Goto statement is especially known in the case of jumping control statements. We
mainly use the goto statement when we want to transfer a program’s control from any one
block to another. Also, we use the goto keyword for the declaration of the goto statement.
The syntax of the goto statement is as follows:
goto name_of_label;
name_of_label;
In the syntax given above, we have used the goto as a keyword for transferring the control
of the program to the name_of_label.
Here, the name_of_label refers to a variable’s name. Thus, in simpler words, the goto here
will ultimately transfer the program’s control to the name_of_label. Thus, there will occur
an execution of all those statements that are followed by the name_of_label.

4. The Loop Statements

The looping can be defined as repeating the same process multiple times until a specific
condition satisfies. There are three types of loops used in the C language. In this part of
the tutorial, we are going to learn all the aspects of C loops.

Why use loops in C language?


The looping simplifies the complex problems into the easy ones. It enables us to alter the
flow of the program so that instead of writing the same code again and again, we can repeat
the same code for a finite number of times. For example, if we need to print the first 10
natural numbers then, instead of using the printf statement 10 times, we can print inside a
loop which runs up to 10 iterations.

Advantage of loops in C
1) It provides code reusability.

2) Using loops, we do not need to write the same code again and again.

A programmer in C might want to repeat any set of instructions or certain statements in


the program to meet the necessary requirements. In such instances, it becomes difficult
to rewrite and repeat everything. And that is exactly where we would like to create
loops using the looping declarations. Loop control statements help in such types of
situations in C.
We have the following types of loops in C:
• Do While Loop
• While Loop
• For Loop

Prof. Vandana V.
For Loop

Definition: A for loop is a repetition control structure which allows us to write a loop
that is executed a specific number of times. The loop enables us to perform n number
of steps together in one line.

Syntax:

for (initialization expr; test expr; update expr)


{
// body of the loop
// statements we want to execute
}

FLOWCHART

// C program to illustrate for loop

#include <stdio.h>
int main()
{
int i=0;

for (i = 1; i <= 10; i++)


{
printf( "Hello World\n");

Prof. Vandana V.
}
return 0;
}

While Loop

The while loop in c is to be used in the scenario where we don't know the number of iterations
in advance. The block of statements is executed in the while loop until the condition specified
in the while loop is satisfied. It is also called a pre-tested loop.

The syntax of while loop in c language is given below:

1. while(condition){
2. //code to be executed
3. }

Example
// C program to illustrate while loop

#include <stdio.h>

int main()

int i=1;

while (i<= 10)

Prof. Vandana V.
printf( "Hello World\n");

i++;

return 0;

do while loop in C
The do while loop is a post tested loop. Using the do-while loop, we can repeat the execution
of several parts of the statements. The do-while loop is mainly used in the case where we need
to execute the loop at least once. The do-while loop is mostly used in menu-driven programs
where the termination condition depends upon the end user.

do while loop syntax

The syntax of the C language do-while loop is given below:

do{
//code to be executed
}while(condition);

//Simple program of c language do while loop where we are printing the table of 1.

#include<stdio.h>
int main(){
int i=1;
do{
printf("%d \n",i);
i++;
}while(i<=10);
return 0;
}

Prof. Vandana V.
Branching Statements

● Branching statements are the statements used to jump the flow of execution from one
part of a program to another.
● The branching statements are mostly used inside the control statements. Java has
mainly three branching statements, i.e., continue, break, and return.
● The branching statements allow us to exit from a control statement when a certain
condition meet.

break statement
The break is a keyword in C which is used to bring the program control out of the
loop. The break statement is used inside loops or switch statement. The break
statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the
inner loop first and then proceeds to outer loops. The break statement in C can be
used in the following two scenarios:

With switch case


With loop
Example:

#include<stdio.h>
#include<stdlib.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);

}
C continue statement
The continue statement in C language is used to bring the program control to the beginning
of the loop. The continue statement skips some lines of code inside the loop and continues with
the next iteration. It is mainly used for a condition so that we can skip some code for a particular
condition.

Prof. Vandana V.
Syntax:
//loop statements
continue;
//some lines of the code which is to be skipped

Example

#include<stdio.h>
void main ()
{
int i = 0;
while(i!=10)
{
printf("%d", i);
continue;
i++;
}
}

C goto statement
The goto statement is known as jump statement in C. As the name suggests, goto is used to
transfer the program control to a predefined label. The goto statment can be used to repeat some
part of the code for a particular condition. It can also be used to break the multiple loops which
can't be done by using a single break statement. However, using goto is avoided these days
since it makes the program less readable and complecated.

Syntax:

label:
//some part of the code;
goto label;

#include <stdio.h>
int main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");

Prof. Vandana V.
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table;
}

FLOWCHART

Prof. Vandana V.

You might also like