Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

control statements and Iterative statements in C

Rating
-
Sold
-
Pages
27
Uploaded on
16-01-2025
Written in
2022/2023

Control Statements and Iterative Statements in C provides a deep dive into decision-making and looping mechanisms essential for structured programming. It begins with an introduction explaining the importance of control flow in C programs, enabling conditional execution and repeated operations. The document first covers control statements, including conditional statements like if, if-else, nested if, and switch-case, illustrating their syntax, use cases, and execution flow with clear examples. It then transitions to iterative statements (loops), explaining the functionality and differences between for, while, and do-while loops, accompanied by flowcharts and real-world scenarios. Key concepts such as loop control mechanisms (break, continue, goto) are explored to demonstrate how they influence loop execution. The document also includes nested loops, highlighting their efficiency and complexity considerations. Advanced topics like infinite loops, loop optimization techniques, and recursion as an alternative to iteration provide deeper insights. A dedicated section on best practices and common pitfalls, such as avoiding infinite loops and optimizing conditions, ensures practical understanding. The document concludes with sample programs, problem-solving exercises, and debugging strategies, making it a comprehensive guide for mastering control and iterative structures in C programming.

Show more Read less
Institution
Course

Content preview

Unit - II

CONTROL STATEMENTS
3.1Control Statements:
In C program, statements are normally executed sequentially in the order in
which they appear. But sometimes, we have to change the order of execution of
statements based on certain conditions, or repeat a group of statements until certain
specified conditions are satisfied.
For this, C language provides Control statements (or decision making
statements) which alter the flow of execution and provide better control to the
programmer on the flow of execution. They are two types. They are
1. Conditional statements
2. Iterative statements


3.2Conditional (Selection) statements:
Conditional statements are used to execute a statement or a group of
statement based on certain conditions.
3.2.1.Simple if statement:

The general form of simple if statement is

if(expression)
{
Statement block;
}
Statement-x;

Here, the expression can be any valid C expression. Statement block may
contain one statement (or) more statements.
First expression will be evaluated. If the expression is true(or
nonzero) then the statements in the statement block gets executed and then
control transfers to the next immediate statement of simple if i.e., statement-
x.
If the expression is false(or zero) then control transfers to execute
the next immediate statement of simple if i.e., statement-x by skipping the
statements in the statement block.This is illustrated in the following
flowchart.
Page 1

, Unit - II




Expression? true


Statement block

False
Statement -x




/*program to find absolute value of an integer using simple if statement*/
#include<stdio.h>
#include<conio.h>
main( )
{
int n;
clrscr( );
printf(“enter a integer\n”);
scanf(“%d”,&n);
if(n<0)
{
n = - n;
}
printf(“the absolute value of number is %d\n”,n);
}
Page 2

, Unit - II

3.2.2if…else statement:
The general form of if …else statement is

if (expression)
{
Statement block1;
}
else
{
Statement block2;
}
Statement-x;
Here, the expression can be any valid C expression. Statement block1 and
statement block2 may contain one statement (or) more statements.
First expression will be evaluated. If the expression is true(or
nonzero) then the statements in the statement block1 gets executed and then
control transfers to the next immediate statement of if..else statement i.e.,
statement-x by skipping the statements in the statement block2.
If the expression is false(or zero) then the statements in the
statement block2 gets executed and then control transfers to the next
immediate statement of if..else statement i.e., statement-x by skipping the
statements in the statement block1.This is illustrated in the following
flowchart.



False true
expression?




Statement block2 Statement block1



Statement -x




Page 3

Written for

Institution
Course

Document information

Uploaded on
January 16, 2025
Number of pages
27
Written in
2022/2023
Type
Class notes
Professor(s)
Vijaya anandaratnam
Contains
All classes

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
srilathalakshmisetty04

Also available in package deal

Get to know the seller

Seller avatar
srilathalakshmisetty04 Vasireddy Venkatadri Institute of Technology
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
5
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions