0% found this document useful (0 votes)
5 views45 pages

C Programming Control Structures Guide

The document outlines the fundamentals of control structures in C programming, focusing on conditional execution, selection statements, and looping mechanisms. It covers various control statements such as if-else, switch, while, for, and do-while loops, along with their syntax and usage. Additionally, it discusses jump statements like break, continue, and goto, providing examples and practices for better understanding.

Uploaded by

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

C Programming Control Structures Guide

The document outlines the fundamentals of control structures in C programming, focusing on conditional execution, selection statements, and looping mechanisms. It covers various control statements such as if-else, switch, while, for, and do-while loops, along with their syntax and usage. Additionally, it discusses jump statements like break, continue, and goto, providing examples and practices for better understanding.

Uploaded by

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

Module 1

Control Structures

FE – C Programming

Prof. Gilu K Abraham


Assistant Professor
Dept. of AIML,
SIES Graduate School of Technology
Learning Objectives:

1. To understand basics of control structure

2. To make students thorough about If-else, Nested If- else control


structures

3. To make students thorough about Switch statements

4. To make students thorough about Looping, do while, while loop

5. To make students thorough about Break and continue statements

Gilu
Introduction

• A C application begins executing with the first line of the main() function
and proceeds statement by statement until it gets to the end of the main()
function.

• In C, any sequence of statements can be grouped together to function as a


syntactically equivalent single statement by enclosing the sequence in
braces.

• This grouping is known as statement block or compound statement.

• Every function has a function body consisting of a set of one or more


statements, i.e., a statement block

Gilu
Introduction

• The order in which statements are executed in a running program is


called the flow of control.

• Control statements embody the decision logic that tells the executing
program what action to carry out next depending on the values of certain
variables or expression statements.

• The control statements include selection, iteration, and jump statements


that work together to direct program flow.

Gilu
Introduction

Gilu
Introduction
• A selection statement is a control statement that allows choosing between
two or more execution paths in a program

• Each decision is based on a boolean expression (also called a condition or


test expression), which is an expression that evaluates to either true or false.
The result of the expression determines which statement is executed next.

• The programming mechanism that executes a series of statements repeatedly


a given number of times, or until a particular condition is fulfilled, is called a
loop. The construct used for loop is known as iteration statement.

• Jump statements transfer the control to another point of the program. Jump
statements include goto, break, continue and return.

Gilu
CONDITIONAL EXECUTION AND SELECTION

• When dealing with selection statements, there are generally three


versions: one-way, two-way, and multiway.

• One-way decision statements do a particular thing or they do not.

• Two-way decision statements do one thing or do another.

• Multi-way decision statements can do one of many different things


depending on the value of an expression

Gilu
CONDITIONAL EXECUTION AND SELECTION

• One-way decisions using if statement


if(TestExpr)
stmtT;
TestExpr is the test expression. stmtT can be a simple
statement or a block of statements enclosed by curly
braces {}.

Gilu
CONDITIONAL EXECUTION AND SELECTION

• Two-way decisions using if –else statement


if(TestExpr)
stmtT;
else
stmtF;
If the test expression TestExpr is true, stmtT will be executed; if the
expression is false, stmtF will be executed. stmtT and stmtF can be single
or a block of statements.

Gilu
CONDITIONAL EXECUTION AND SELECTION

• Multi way decision

Gilu
CONDITIONAL EXECUTION AND SELECTION

• Multi way decision

Gilu
CONDITIONAL EXECUTION AND SELECTION

Gilu
If-else ladder
• WAP to print the garde according to the score secured by student.
For score>=90----A garde
For score>=75----B garde
For score>=50----C garde
Else Grade=Fail

Gilu
Nested -if
• When any if statement is written under another if statement, this cluster is called
a nested if.

• The if statement that tests for divisibility by 5 is located inside of the if statement
that tests for divisibility by 3 therefore it is considered to be a nested if statement.

if (number % 3 == 0)
{
printf(“number is divisible by 3. \n”);
if (number % 5 == 0)
{
printf(“number is divisible by 3 and 5. \n”);
}
}

Gilu
Dangling else problem
• This classic problem occurs when there is no matching else for each if.
To avoid this problem, the simple C rule is that always pair an else to the
most recent unpaired if in the current block.

• If TestExprA is evaluated to true, then execution moves


to the nested if and evaluates TestExprB.
• If TestExprB is evaluated to true then stmtBT will be
executed.
• If TestExprA is evaluated to false, then stmtAF is
executed. But in the code above, the else is
automatically paired with the closest if. But, it is
needed to associate an else with the outer if also.
• The solution is either of the following:
- Use of a null else
- Use of braces to enclose the true action of the second
if.
Gilu
Dangling else problem

• Now, in both the


solutions, if the
expression TestExprA
evaluates to false then
the statement stmtAF
will be executed.
• If it evaluates to true,
then it checks for
TestExprB.
• If TestExprB evaluates
to true then statement
stmtBT will be executed.

Gilu
Switch Statements
• It belongs to the category of selection/ branching control structures
• The switch statement in C is an alternate to if-else-if ladder
statement
• It allows us to execute multiple operations for the different possible
values of a single variable called switch variable.
• Here, We can define various statements in the multiple cases for the
different values of a single variable.

Gilu
Switch Statements
The syntax of switch statement in c language:

switch(expression){
case value1:
//code to be executed;
break;
case value2:
//code to be executed;
break;
......

default:
code to be executed if all cases are not matched;
}

Gilu
Rules for switch statement in C language

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) The break statement in switch case is not must. It is optional. If


there is no break statement found in the case, all the cases will be
executed present after the matched case. It is known as fall
through the state of C switch statement.

Gilu
Flowchart of switch statement in C

Gilu
Functioning of switch case statement
• First, the integer expression specified in the switch statement is
evaluated.
• This value is then matched one by one with the constant values given
in the different cases.
• If a match is found, then all the statements specified in that case are
executed along with the all the cases present after that case including
the default statement.
• No two cases can have similar values.
• If the matched case contains a break statement, then all the cases
present after that will be skipped, and the control comes out of the
switch.
• Otherwise, all the cases following the matched case will be executed.

Gilu
Practices:

• Grade:

Gilu
Practices:

Gilu
Practices

• Sample Calculator

Gilu
Practices:

Gilu
Iterations

• A loop allows one to execute a statement or block of statements


repeatedly.
• There are mainly two types of iterations or loops

Unbounded loop/iterations
Bounded loop/iterations

Gilu
• Bounded Iteration
Repetition is implemented by constructs that allow a determinate
number of iterations
Example :for loop

• Unbounded Iteration
In many occasions when one does not know ,ahead of time ,how
many iterations may be required .

Example: while loop and


do-while loop

Gilu
A loop can be either be a pre-test loop
or post-test loop .

Pre-test Loop Post-test loop


• The condition is checked before the • The code is always executed
beginning of each iteration .
once ,At the completion of the
• If the test expression evaluates to true
loop code ,the test expression
, the statements associated with the
pre- test loop construct are executed
is tested .
and the process is repeated till the • If the test expression is true
test expression becomes false. then loop repeats and if false
or then terminates.
• If the test evaluates false ,the
statement associated with the
constructs are skipped and the
statement next to the loop is executed

Gilu
Loop Variations :pre-test and Post-
test loops

Gilu
In addition to condition/test expression the other
two processes are associated with almost all loops .

• These are: • Initialization :It is the statement


that assigns the initial value to
Initialization the loop control variable.

• Updating :The action that


Updating
changes the test expression
from true to false .

Gilu
While construct

• While loop is pre-test loop


• It uses a test expression to
control the loop.

While construct

Gilu
Expanded syntax of while and its
flowchart representation
#include<stdio.h>
int main()
{
int c;
c=5;
while(c>0)
{
printf(“\n%d”,c);
c=c-1;
}
return 0;
}
Gilu
for Construct
• The loop formed by using the • for(initialization;Testexpr;Updating)
for statement is generally called {
a determinate or definite loop . Stmt
• (Because the programmer knows }
exactly how many times it will
repeat.)
• Initialization: First one to
execute
• Testexpr: represents a test
expression that must be true for
the loop to continue execution
• Updating :The statement
contained here are executed
every time through loop
• Stmt: Single block or block of
statement.

Gilu
The Sequence of events that
generate the iteration using for loop
• Evaluate the Initialization expression
• If the value of the Testexpr is false ,terminate and go to next line of stmt
Otherwise continue with the same operation.
• Execute the statement or block of statement
• Evaluate Update expression.

Gilu
In general how many times does the
body of for() loop is executed
• for(i=m;i<=n;i++)
Ans:(n-m)+1 times

• for(i=m;i<n;i++)
Ans:(n-m) times

• for(i=m;i<n;i+=x)
Ans:(n-m)/x

Gilu
Example of for() loop

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

Gilu
Points to be remembered for ‘for
’loop
• EVEN THOUGH 3 EXPRESSION IS NOT PRESENT IN THE
for STATEMENT ‘;’ MUST BE PRESENT.

• ANY INITIALIZATION STATEMENT CAN BE USED IN THE


FIRST PART OF THE FOR LOOP .

• MULTIPLE INITIALIZATION SHOULD BE SEPERATED


WITH A COMMA OPERATOR.

Gilu
Do-while COnstruct

• do while construct is another #include<stdio.h>


construct that is closely related int main(){
to the while construct.
int x=1;
• The do keyword is placed on a
line of a code at the top of the do{
loop . printf(“%d\n”,x);
• A block of statement follows it x=x+1;
with a test expression after the }while(x<=5);
keyword while ,at the bottom of
the loop. return 0;
}

Gilu
Do while
While

C do-while
Loop

Gilu
While and do-while loop

1. While loop is indeterminate 1. do-while loop is


or unbounded loop. indeterminate or unbounded
loop.
2. While is pre-test loop 2. Do-while loop is considered
to be post-test loop since the
loop is located after the body
3. While does not executes for of the loop.
any false condition 3. Do-while guarantees to
execute at least once even if
4. Flowchart representation is the condition is false
different than do-while 4. Flowchart representation is
different than while loop

Gilu
Jump Control Statements

Control the flow inside loops

It includes:

 break statement

 continue statement

 goto statement
break

Exits loop immediately


continue

Skips current iteration and moves to the next


goto

Jumps to a labeled statement


Thank Yu!!
gilua@[Link]

Gilu

You might also like