0% found this document useful (0 votes)
35 views52 pages

Chapter 3

Uploaded by

patel.ved98795
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views52 pages

Chapter 3

Uploaded by

patel.ved98795
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CHARUTAR VIDYA MANDAL UNIVERSITY

A D PATEL I NSTITUTE O F T ECHNOLOGY

Control Structure in C

P REPARED BY:
P ROF. A XIT K ACHHIA
CP D EPARTMENT
Decision Making with IF Statement
➢The if Statement is a powerful decision-making statement and is used to
control the flow of execution of statements.
➢It is basically a two way decision statement and is used in conjunction with
an expression.
➢It takes the following form:
if(test expression)
➢It allows the computer to evaluate the expression first and then, depending
on whether the value of the expression is true or false, it transfer the control
to a particular statement.

2
Decision Making with IF Statement
➢The point of program has two paths to follow, on for the true condition and
another is for the false condition.
➢Some example of decision making are:
➢1. if(bank balance is zero)
borrow money
➢2. if(room is dark)
turn on light
➢3. if(code is 1)
person is male

3
Simple 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.
➢i.e. if a certain condition is true then a block of statement is executed
otherwise not.
➢The syntax of the if statement is given below.
if(expression){
//code to be executed
}

4
Simple IF Statement
➢Let's see a simple example of if statement
#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); }
return 0;
}
5
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.
➢The syntax of the if-else statement is given below.
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}
6
Flow chart of the If-Else Statement

7
If-Else Statement
#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); }
else{
printf("%d is odd number",number); }
return 0; }
8
IF else-if Ladder
➢The if-else-if ladder statement is an extension to the if-else statement.
➢It is used in the scenario where there are multiple cases to be performed
for different conditions.
➢In if-else-if ladder statement, if a condition is true then the statements
defined in the if block will be executed, otherwise if some other condition is
true then the statements defined in the else-if block will be executed, at the
last if none of the condition is true then the statements defined in the else
block will be executed.

9
IF else-if Ladder
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true } ...
else{
//code to be executed if all the conditions are false
}
10
IF else-if Ladder

11
Example
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number==10){
printf("number is equals to 10"); }
else if(number==50){
printf("number is equal to 50");
}
12
Example
else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
return 0;
}

13
Switch Statement
➢We have seen that when one of the many alternatives is to be selected, we
can use an IF statement to control the selection.
➢However, the complexity of such a program increases dramatically when the
number of alternatives increases. The Program becomes difficult to read and
follow.
➢Fortunately, C has a built in multiway decision statement known as a
SWITCH.
➢Here, We can define various statements in the multiple cases for the
different values of a single variable.

14
Switch Statement
The syntax of switch statement in c language is given below:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched; }
15
Rules for Switch Statement
1) The switch expression must be of an integer or character type.
2) The case value must be an integer or character constant.
3) The case value can be used only inside the switch statement.
4) You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
5) When a break statement is reached, the switch terminates, and the flow of
control jumps to the next line following the switch statement.
6) A switch statement can have an optional default case, which must appear
at the end of the switch. The default case can be used for performing a task
when none of the cases is true. No break is needed in the default case.

16
Flowchart of Switch Statement

17
The ? : Operator
➢The C language has an unusual operator, useful for making two-way
decision.
➢The operator is a combination of ? And : , and takes three operands.
➢This operator is popularly known as the conditional operator.
➢The general form of use of the conditional operator is as follow:
conditional expression ? expression 1 : expression 2
➢The conditional operator is evaluated first. If the result is non-zero,
expression 1 is evaluated otherwise expression 2 is evaluated.

18
The ? : Operator
➢For example, ➢Consider the following function:
if (x<0) y = 1.5x + 3 for x <= 2
flag = 0; y = 2x + 5 for x > 2
else This can be evaluated using the conditional
flag = 1; operator:
Can be written as:
y = (x > 2) ? (2 * x + 5) : (1.5 * x + 3);
flag = (x < 0) ? 0 : 1;

19
The GOTO Statement
➢A goto statement in C programming provides an unconditional jump from
the 'goto' to a labeled statement in the same function.
➢The goto statement is a jump statement which is sometimes also referred to
as unconditional jump statement.
➢The goto statement can be used to jump from anywhere to anywhere within
a function.
➢The syntax for a goto statement in C is as follows −
Syntax1 | Syntax2
goto label; | label:
. | .
. | .
label: | goto label; 20
The GOTO Statement
➢In the above syntax, the first line tells the compiler to go to or jump to the
statement marked as a label.
➢The statement immediately followed after ‘label:’ is the destination
statement. The ‘label:’ can also appear before the ‘goto label;’ statement in
the above syntax.

21
Decision Making and Looping
➢Execution of a statement or set of statement repeatedly is called as looping.
➢The loop may be executed a specified number of times and this depends on
the satisfaction of a test condition.
➢A program loop is made up of two parts one part is known as body of the
loop and the other is known as control condition.
➢Depending on the control condition statement the statements within the
loop may be executed repeatedly.

22
Decision Making and Looping
➢Example: ……..
sum = 0;
n = 1;
loop:
sum = sum + n*n;
if (n == 10)
goto print;
else { n = n+1;
goto loop; }
print: …………………..
23
Decision Making and Looping
➢Depending on the position of the Control statement in the loop, a control
structure may be classified either as the entry-controlled loop or as the exit-
controlled loop.
➢In, entry controlled loop, the control conditions are tested before the start
of the loop execution. If the conditions are not satisfied, then the body of the
loop will not be executed.

➢In, exit-controlled loop, the test is performed at the end of the body of the
loop and therefore the body is executed unconditionally for the first time.

24
Decision Making and Looping
➢Entry Controlled Loop Exit Controlled Loop

25
The WHILE Statement
➢A while loop in C programming repeatedly executes a target statement as
long as a given condition is true.
➢The syntax of a while loop in C programming language is −
while ( test condition )
{
body of the loop;
}

➢The while is an entry controlled loop statement.


➢If the condition is true, then the body of the loop is executed.
26
The WHILE Statement
➢When the condition becomes false, the program control passes to the line
immediately following the loop.
➢FLOW DIAGRAM:

27
The WHILE Statement Example
#include <stdio.h>
int main ()
{ /* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 ) {
printf("value of a: %d\n", a);
a++;
}
return 0; }
28
The DO Statement
➢The WHILE loop makes a test of condition before the loop is executed.
Therefore, the body of the loop may not be executed at all if the condition is
not satisfied at the very first attempt.
➢On some occasions it might be necessary to execute the body of the loop
before the test is performed.
➢In such situation can be handled with the help of the do statement.

➢Unlike for and while loops, which test the loop condition at the top of the
loop, the do...while loop in C programming checks its condition at the bottom
of the loop.

29
The DO Statement
➢A do...while loop is similar to a while loop, except the fact that it is
guaranteed to execute at least one time.
➢Syntax:
do
{
statement(s);
}
while ( condition );

30
The DO Statement
➢On reaching the do statement, the program proceeds to evaluate the body
of the loop first. At the end of the loop, the condition in the while statement
is evaluated.
➢If the condition is true, the program continues to evaluate the body of the
loop once again.
➢When the condition becomes false, the loop will be terminated and the
control goes to the statement that appears immediately after the while
statement.

31
The DO Statement
➢Flow Diagram:

32
Example:
#include <stdio.h>
int main () { /* local variable definition */
int a = 10;
/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}
33
The FOR Statement
➢A for loop is a repetition control structure that allows you to efficiently write
a loop that needs to execute a specific number of times.
➢The body of a for statement is executed zero or more times until an optional
condition becomes false.
➢The syntax of a for loop in C programming language is −
for ( init; condition; increment )
{
statement(s);
}

34
The FOR Statement
➢The execution of the FOR statement is as follows:
➢The init step is executed first, and only once. This step allows you to declare
and initialize any loop control variables.
➢Next, the condition is evaluated. If it is true, the body of the loop is
executed. If it is false, the body of the loop does not execute and the flow of
control jumps to the next statement just after the 'for' loop.
➢After the body of the 'for' loop executes, the flow of control jumps back up
to the increment statement. This statement allows you to update any loop
control variables. This statement can be left blank, as long as a semicolon
appears after the condition.

35
Flow Diagram of FOR Statement

36
Example of FOR Statement
#include <stdio.h>
int main ()
{
int a;
/* for loop execution */
for( a = 10; a < 20; a = a + 1 )
{
printf("value of a: %d\n", a);
}
return 0;
} 37
Example of FOR Statement
#include<stdio.h> ◦ #include <stdio.h>
int main() ◦ int main()
{ ◦{

int i; int a,b,c;


for(i=1;i<=10;i++) for(a=0,b=12,c=23;a<2;a++)
{ {
printf("%d \n",i); printf(“%d ",a+b+c);
} }
◦}
return 0;
}
38
Practice
➢Print the no. 10, 9, 8…. 1 using while loop
➢Write a C program to print the table number using C FOR loop
➢Print a table of number 5 using do-while loop in C

39
Practice
#include <stdio.h>
int main() {
int i;
i = 10;
◦ while(i >= 1)
◦{
◦ printf("%d ", i);
◦ i = i - 1;
◦}
◦ return 0;
}
40
Practice
#include<stdio.h>
int main() {
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++)
{
printf("%d \n",(number*i));
}
return 0; }
41
Practice
#include<stdio.h>
int main(){
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
do{
printf("%d \n",(number*i));
i++;
}while(i<=10);
return 0;
} 42
Jumps in LOOPs
➢Loops perform a set of a operations repeatedly until the control variable
fails to satisfy the test-condition.
➢Sometimes, executing a loop it becomes desirable to skip a part of the loop
or to leave the loop as soon as a certain condition occurs.
➢For example, consider the case of searching for a particular name in a list
containing, say, 100names.
➢A program loop written for reading and testing the names 100 times must
be terminated as soon as the desired name is found.
➢C permits a jump from one statement to another within a loop as well as
jump out of the loop.

43
Break
➢It was used to "jump out" of a switch statement.
➢The break statement can also be used to jump out of a loop.
➢The break statement is used to end the loop immediately after the
encounter. It is used in C/C++ in the following scenario:-
➢It is used to terminate the loop, and program control resumes at the next
statement following the loop.
➢It is used to terminate a case in the switch statement, which will be
discussed later in the blog.

44
Break
#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
break; }
printf("%d\n", i);
}
return 0;
} 45
Break
➢Control Flow of break statement in for loop
Control Flow of break statement in while loop

46
Continue
➢The continue statement is also one of the loop control statements in C/C++.
➢When the continue statement is encountered, the code below the continue
statement is skipped, and the next iteration begins.
➢The continue statement in C programming works somewhat like
the break statement. Instead of forcing termination, it forces the next
iteration of the loop to take place, skipping any code in between.

➢Syntax;
continue;

47
Flow diagram of Continue

48
Nesting of a Control Structures
➢C provides us the feature of nesting one structure within another structure
by using which, complex data types are created.
➢For example, we may need to store the address of an entity employee in a
structure. The attribute address may also have the subparts as street number,
city, state, and pin code.
➢Hence, to store the address of the employee, we need to store the address
of the employee into a separate structure and nest the structure address into
the structure employee.

49
Nesting of a Control Structures
The structure can be nested in the following ways.
[Link] separate structure
[Link] Embedded structure

50
Nesting of a Control Structures
➢1. By separate Structure: Here, we create two structures, but the
dependent structure should be used inside the main structure as a member.
struct Date {
int dd;
int mm; As you can see, doj (date of joining) is the variable of type
int yyyy; }; Date.
struct Employee { Here doj is used as a member in Employee structure.
int id;
In this way, we can use Date structure in many structures.
char name[20];
struct Date doj;
}emp1; 51
Nesting of a Control Structures
2. By Embedded structure: The embedded structure enables us to declare
the structure inside the structure. Hence, it requires less line of codes but it
can not be used in multiple data structures.
struct Employee
{ int id;
char name[20];
struct Date
{ int dd;
int mm;
int yyyy;
}doj;
}emp1;
52

You might also like