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

Unit 2 Introduction Programming

Uploaded by

shabanasarwani
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 views11 pages

Unit 2 Introduction Programming

Uploaded by

shabanasarwani
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
You are on page 1/ 11

UNIT II Control Structures

Simple sequential programs Conditional Statements (if, if-else, switch), Loops (for, while, dowhile) Break and
Continue
CONTROL STRUCTURES (STATEMENTS)
A statement is a part of program that can be executed. A statement specifies an action.
Statements that are written individually are called Single/Simple statements. Statements that are written
as a block are called Block/Compound statements. A block begins with an open brace {and ends with a
closing brace}.
Control statements allow programmers to control the flow of execution of their programs.
C categorizes control statements into these groups:

SELECTION STATEMENTS:
A selection statement checks the given condition and decides the execution of statements after the
success orfailure of the condition. C supports two selection control statements if and switch.
1. If statement: The if statement is a decision making statement that allows the computer to evaluate an
expression (condition) first and depending on the result of the condition i.e. true or false, it transfers the
control to a particular statement.
The if statement has the following forms
a) Simple if Statement.
b) if...else Statement.
c) Nested if Statement.
d) if..else Ladder Statement.
Simple if Statement
The simple if statement contains only one condition, if the condition is true, it will execute the statements
that are present between opening and closing braces. Otherwise it will not execute those statements.
Syntax:
if(condition)
//Single-statement
OR

if(condition)
{
//statements
}
Example:
/* A program to check whether a number is Less than 100 or not */
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("enter an integer: ");
scanf("%d",&a);
if(a<100)
printf("%d is less than 100",a);
getch();
}
Output:
Enter an integer: 99
99 is less than 100
if...else Statement
This statement is used to define two blocks of statements in order to execute only one block. If the
condition istrue, the block of if is executed; otherwise, the block of else is executed.
Syntax:
if (condition)
{
//statements
}
else
{
//statements;
}
Example:
Write a program to check whether the given number is a positive number or negative number
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter any number ");
scanf("%d",&n);
if(n>=0)
{
printf("\n %d is a positive number",n);
}
else
{
printf("\n %d is a negative number",n);
}
getch();
}
Output:
enter any number -99
-99 is a negative number
Nested if –else Statement
When an if statement is placed in another if statement or in else statement, then it is called nested if statement.
The nested if-else is used when a series of decisions are involved.
Syntax:
if (condition)
{
if(condition)
{
// Statements;
}
}
else
{
//Statements
}
Example:
Write a program to finding greatest among three numbers
#include <stdio.h>
#include <conio.h>
void main()
{
double n1, n2, n3;
clrscr();
printf("Enter three numbers:");
scanf("%lf%lf%lf",&n1,&n2,&n3);
if (n1 >= n2)
{
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
}
else
{
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}
getch();
}
Output:
Enter three numbers: 1 2 3
3.00 is the largest number.

Syntax:
if(condtion1)
{
//statements1;
}
else if(condition2)
{
//statements2;
}
else if(condition3)
{
//statements3;
}
|
|
|
else if(conditionN)
{
//statementsN;
}
Else
{
//Default_Statement ;
}
In the above syntax of if-else-if, if the Condition1 is TRUE then the Statement1 will be
executed and control goes to next statement in the program following if-else-if ladder.
If Condition1 is FALSE then Condition2 will be checked,
if Condition2 is TRUE then Statement2 will be executed and control goes to next statement in the
program following if-else-if ladder.
If Condition2 is FALSE then Condition3 will be checked,
if Condition3 is TRUE then Statement3 will be executed and control goes to next statement in the
program following if-else-if ladder
Similarly, if Condition3 is FALSE then next condition will be checked and the process
continues.
If all the conditions in the if-else-if ladder are evaluated to FALSE, then Default_Statement will be
executed.
Example:
Write a program to test whether the given number is a single digit or a double
digit or a trible digit or more than three digits.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter any number:");
scanf("%d",&n);
if(n>=0 && n<=9)
printf("\n %d is a single digit number",n);
else if(n>=10 && n<=99)
printf("\n %d is a double digit number",n);
else if(n>=100 && n<=999)
printf("\n %d is a trible digit number",n);
Else
printf("\n %d is more than three digits",n);
getch();
}
Output:
Enter any number:10
10 is a double digit number
Switch Statement
C has a built-in multiple-branch selection statement, called switch, which successively tests the value of an
expression against a list of case values. When a match is found, the statements associated with that case are
executed. For example, if the value of the expression is equal to value1, statements after case value1: are
executed until break is encountered. If there is no match, the default statements are executed.
Syntax:
switch(expression)
{
case value1://statements;
break;
case value2://statements;
break;
|
|
|
case valueN://statements;
break;
default://statements;
}
Example:
Write a program to check whether the given character is a vowel or not using switch case
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;clrscr();
printf("enter any single character:");scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': printf("%c is a vowel",ch);
break;
default: printf("\n %c is not a vowel",ch);
break;
}
getch();
}
LOOP/ ITERATION STATEMENTS
In C, and all other modern programming languages, iteration statements (also called loops) allow a
set ofinstructions to be repeatedly executed until a certain condition is reached.
Based on position of loop, loop statements are classified into two types:
1. entry-controlled loop (pre-test loop)- while, for
2. exit-controlled loop (post-test loop)- do while
1. While loop
While is pre tested/ entry controlled loop statement i.e. first condition is checked , if condition is true body of
loop is executed repeatedly until condition is fail.
Syntax:
initialization;
while(condition)
{
statements;
Increment / Decrement statement;
}
Initialization: In this step, we initialize the loop variable to initial value.
Conditional: This is one of the most crucial steps as it decides whether the block in the while loops code
will execute or not. The while loop body will be executed if and only the condition defined is true.
Increment/Decrement: defines how the loop control variable changes each time the loop is repeated.
Example 1:
Write a program to print first 15 natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
n=1;
while(n<=15)
{
printf("%3d",n);
n=n+1;
}
getch();
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Example 2:
//Displaying multiplication table for given integer
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n;
clrscr();
printf("Enter integer to display multiplication table:");
scanf("%d",&n);
i=1;
while(i<=10)
{
printf("%d*%d=%d\n",n,i,n*i);
i=i+1;
}
getch();
}
Output:
Enter integer to display multiplication table: 9
9*1=9
9*2=18
9*3=27
9*4=36
9*5=45
9*6=54
9*7=63
9*8=72
9*9=81
9*10=90
2. do while loop
Do While is post tested/ exit controlled loop statement i.e first body of loop is executed & finally
condition is checked. Even though condition is false, it will execute the body of loop statements at least
once.
Syntax:
initialization;
do
{
statements;
Increment/Decrement statement;
}while(condition) ;
Example:
Write a program to print first 20 natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
n=1;
do
{
printf("%3d",n);
n=n+1;
} while(n<=20);
getch();
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Difference between while/for & do while:
In while and for, if the condition is false, it will never execute the loop statements.
But in do-while, even though condition is false, it will execute the loop statements at least once

While Example: Example:


Write a program to illustrate while Write a program to illustrate do while
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int n; int n;
clrscr( ); clrscr( );
n=1; n=1;
while(n>10) do
{ {
printf(“%d\n”,n); printf("%d",n);
n=n+1; n=n+1;
} } while(n>10);
getch( ); getch();
} }
Output: No output because condition is false Output: it prints 1 even though condition is false
for loop while loop do while loop
Syntax: Syntax: Syntax:
for(initialization ;condition; increment/decrement) Initialization; Initialization;
{ While(condition) do
//Statements { {
} //Statements //Statements
Increment/decrement; Increment/decrement;
} }while(condition);
The control will never enter in a loop if the The control will never enter The control will enter a
condition is not true for the first time. in a loop if the condition is loop even if the condition is
not true for the first time. not true for the first time.
No semicolon after the condition parenthesis No semicolon after the There is a semicolon after
condition parenthesis the condition parenthesis.
Required one keyword for Required one keyword Required two keywords do
while and while
Entry controlled loop Entry controlled loop Exit controlled loop
1. For loop
for is pre tested entry controlled loop statement i.e first condition is checked & body of loop is executed
when condition is true otherwise the body of the will not executed.
Syntax:
for(initialization; condition; increment / decrement )
{
list of statements
}
Example:
Write a program to print first 10 natural numbers and their sum
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
clrscr();
for(i=1; i<=10; i++)
{
printf("%d\t",i);
sum=sum+i;
}
printf("\nThe sum of first 10 natural numbers is:%d",sum);
getch();
}
Output:
1 2 3 4 5 6 7 8 9 10
The sum of first 10 natural numbers is:55
However, if condition section is omitted, the for loop becomes an endless loop, which is called an
infinite loop. When the condition is absent, it is assumed to be true. The for statement may have
an initialization and increment/decrement sections. But C programmers more commonly use for ( ; ; )
for infinite loops.
Nested loops:
A nested loop means a loop statement inside another loop statement. That is why nested loops are also
called “loop inside loops“. We can define any number of loops inside another loop.
Syntax:
for( initialization; condition; increment )
{
for( initialization; condition; increment )
{
// statement of inside loop
}
// statement of outer loop
}
Example:
Construct a pyramid of numbers.
//Half Pyramid of Numbers
#include <stdio.h>
#include<conio.h>
void main()
{
int i, j, rows;
clrscr();
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf("%d ", j);
}
printf("\n");
}
getch();
}
Output:
Enter the number of rows: 10
1
12
123
1234
12345
123456
1234567
12345678
123456789
1 2 3 4 5 6 7 8 9 10
Jump statements:
1. break Statement
The break statement has two uses
a) to terminate a case in the switch statement.
b) to terminate a loop.
The general form of the break statement is break keyword followed by semicolon.
break;
Example:
Write a program to illustrate break
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1; i<=10; i++)
{
if(i==6)
{
break;
}
printf(“%d\t”,i);
}
getch();
}
Output:
12345
2. continue Statement
During the loop operations, it may be necessary to skip a part of the body of the loop under certain
conditions. Like the break statement, C supports another similar statement called the continue
statement. However, unlike the break which causes the loop to be terminated, the continue causes the
loop to be continued with the next iteration after skipping any statements after continue statement.
The general form of the continue statement is continue keyword followed by semicolon
continue;
Example:
Write a program to illustrate continue
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1; i<=10; i++)
{
if(i==6)
{
continue;
}
printf(“%d\t”,i);
}
getch();
}
Output:
1 2 3 4 5 7 8 9 10

You might also like