Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Title: Notes on C Programming- Control Statements
Description: It includes Control Statements such as Decision making and branching(if, if..else and so on), looping statements and Unconditional Jump statements.
Description: It includes Control Statements such as Decision making and branching(if, if..else and so on), looping statements and Unconditional Jump statements.
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
CHAPTER 3
Session Objectives
Explain Types of Control Statements
Explain If, If
...
Case statement
Explain Break,continue Statements
Explain the Goto Statement
•Decision making&
Branching
• Looping Statement
• Jumping
Statement
By default the statements are executed sequentially
...
Decision
Making &Branching
Statement
Jumping
Statement
Looping
Statement
Syntax :
if(condition)
{
True Block Statement(s);
}
Executable statement(s);
Test
Condition
TRUE
FALSE
Executable Statements
True Block Statements
Syntax :
if(condition)
{
True Block Statement(s);
}
else
{False Block statement(s);
}
FALSE
False Block Statements
Test
Condition
TRUE
True Block Statements
Syntax :
Initialization counter;
while(condition)
{
Body of the Loop;
Increment/Decrement counter
;
}
Start
Initialize
FALSE
Test
Condition
TRUE
Body of the loop
Increment or
Decrement
STOP
Syntax :
Initialization counter;
do
{
Body of the Loop;
Increment/Decrement counter
;
} while(condition);
Start
Initialize
Body of the loop
Increment or Decrement
TRUE
Test
Condition
FALSE
Stop
Syntax :
For(initialization;condition;increment/decrement)
{
Body of the Loop;
}
Start
Initialize
FALSE
Test
Condition
TRUE
Body of the loop
Increment or
Decrement
STOP
Multiple Initialization / Increments
The following loop is used for multiple initialization -
The for loop will be termed as a nested for loop when
it is written like this : -
Write a “C” Program swapping two
numbers
#include
h>
void main()
{
int i=2;
printf("\n The value of i is %d",i);
printf("\n The value of i is %d",++i);
printf("\n The value of i is %d",i);
}
Decrement Operator Example
#include
h>
void main()
{
float faren,celsius;
printf("\n Enter the farenheit = ");
scanf(" %f", &faren);
celsius = (faren -32) *5/9;
printf("The Celsius is = %f",celsius);
}
Write a program to accept your name,address & city and print it one by one
#include
h>
#include
h>
void main()
{
char name[20];
int basic,pf,lic,net;
printf("\n Enter the name=");
scanf("%s",name);
printf("\n Enter Basic Bpay =");
scanf("%d",&basic);
printf("\n Enter PF =");
scanf("%d",&pf);
printf("\n Enter LIC =");
scanf("%d",&lic);
net = basic – (pf + lic);
printf("Net Salary is = %d ",net);
}
Write a program to find the greatest among two
numbers a,b
#include
=");
scanf("%d %d",&a,&b);
if (a>b)
{
printf("A is greatest %d“,a);
}
else
{
printf(" B is greatest %d“,b);
}
getch();
}
Enter 2 nos
...
h>
void main()
{
int a,b,c;
printf("\n Enter 3 nos
...
c);
}
getch();
}
Enter 3 nos
...
#include
a);
}
Elseif(a<0)
{
printf("Given number is negative %d“,a);
}
Else
{
printf("Given number is Zero %d“,a);
}
}
Enter the number = 10
The Given Number is positive
Write a progrm input the given
year is LEAP YEAR or Not
#include
h>
void main()
{
int no;
printf("Enter the Number =\n");
scanf("%d",&no);
if ((no%2)==0)
{
printf("\n The Number is Even Number %d“,no);
}
else
{
printf("\n The Number is Odd Number %d“,no);
}
getch();
}
Enter the number = 12
The Number is Even Number 12
Write a program to print the FIBONACCI
SERIES upto N Numbers
#include
h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\n",i);
}
printf("\n");
}
Output
1
22
333
4444
55555
PASCAL TRIANGLE
#include
h>
void main()
{
int i,j,c=1;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\n",c);
c++;
}
printf("\n");
}
Output
Output
1
1
12
23
123
456
1234
7 8 9 10
12345
11 12 13 14 15
PASCAL TRIANGLE to print *
#include
h>
void main()
{
int i,j;
clrscr();
for(i=0;i<5;i++)
{
for(j=i;j>0;j--)
{
printf("%d\n",j);
}
printf("\n");
}
Output
*
**
** *
** * *
** * * *
1
21
321
4321
54321
To print the word in reverse
#include
h>
#define size 10
void main()
Enter Any String : raja
{
char name[size+1];
The Given String is raja
int i=1;
The Reversed String is ajar
clrscr();
printf("\n Enter Any String");
scanf("%s",name);
printf("\n The Given string is %s\n",name);
for(i=0;name[i]!='\0';i++);
printf("\n\n The Reversed String is");
for(i=size-1;i>=0;i--)
{
printf("%c",name[i]);
}
getch();
}
The continue statement causes the next iteration
of the enclosing loop to begin
...
continue;
function
The exit() is used to break out of the program
...
statement
The break statement is used to terminate a case in a
switch statement
...
break;
label
The goto statement transfers control to any other
statement within the same function in a C program
...
goto Labelname:
Statements;
-Labelname:
---
Statements;
Syntax :
switch(Expression)
{
case 1: Statements;
break;
case 2: Statements;
break;
case 3: Statements;
break;
default :Statements;
break;
}
• Default is Optional
• It is useful while writing menu driven programs
• Break statement transfers the control to the end of
switch
...
Write a menu type program to solve arithmetic calculation
using switch case stmts
...
h>
void main()
{
int a,b,choice,tot;
printf("\n 1
...
Subtraction");
printf("\n 3
...
Division");
printf("\n Enter the Values=");
scanf("%d %d",&a,&b);
printf("enter your choice=");
scanf("%d",&choice);
switch(choice)
{
case 1:
tot = a+b;
printf("\n The Addition of 2 nos
...
% d",tot);
break;
case 3:
tot = a*b;
printf("\n The Multiplication of 2 nos
...
h>
void main()
{
int c=0,v=0;
char x;
printf("enter any string \n");
do
{
switch(x=getchar())
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
v++;
break;
case '\n':
break;
default:
c++;
break;
}
}while (x!='\n');
printf("no
...
of consonants is %d", c);
getch();
}
Enter Any String : welcome
No
...
of Consonants : 4
Write a “C” Program to generate Armstrong
No
...
h>
void main()
{
int a,b,s,n,i;
printf("\n ENter the Limit");
scanf("%d",&n);
printf("\n The armstrong Numbers are");
for(i=0;i<=n;i++)
{
a=i;
s=0;
while(a>0)
{
b=a%10;
b=b*b*b;
s=s+b;
a=a/10;
}
if(i==s)
{
printf("\t %d",i);
}
}
}
Write a program print the given number as
reverse format
...
h>
void main()
{
long int a,b,c=0,n;
printf("\n Enter the Number");
scanf("%ld",&a);
while(a>0)
{
n=a%10;
a=a/10;
c=b+c*10;
}
printf("\n The reversed numeral is %ld",c);
}
Enter the Number : 345
The reversed Numeral is 543
Write a program to convert the number from binary to
decimal
#include
h>
void main()
{
int i=0,j=0,sum=0;
long int n,x;
printf("\n Enter Binary Number");
scanf("%ld",&n);
if(n!=0)
{
i=n%10;
if(i==0 || i==1)
{
while(n!= 0)
{
i=n%10;
sum=sum+i*pow(2,j);
n=n/10;
j++;
}}}
if(sum==0)
printf("\n The no is not a binary number");
else
printf("\n The equivalent decimal number is %d",sum);
}
'break' statement Example
'break' stmt is used to terminate loops
or exit from a switch
...
h>
void main()
{
int i;
printf("The values are\n");
for(i=1;i<=100;i++)
{
printf("They are %d \n",i);
if(i==25)
{
break;
}
}
}
Print the Numbers from 1…25
Enter Binary Number : 101
The Equivalent Decimal No : 5
Convert the character from lower to upper and vice
versa
...
h>
void main()
{
char x,a;
Do {
printf("\n Enter any character");
fflush(stdin);
scanf("%c",&x);
if(x>='A' && x<='Z')
printf("\nLower case %c",x+('a' - 'A'));
else if(x>='a' && x<='z')
printf("\n Upper case is %c",x - ('a' - 'A'));
else
printf("\n That's not a Letter");
printf("\n Another Data[Y/N]?");
fflush(stdin);
scanf("%c",&a);
}while(a=='y');
F2 Key
Save File
F3
Open an existing File
F5
Maximize
F6
Move to next Program
Alt+F9
To compile (Check Errors)
Ctrl+F9
Compile and Linking (Execute a Program)
Alt+F5
Display output mode
Alt+F3
Close screen
Quit
Alt+X (come out from Turbo “C”)
Session Summary
The if statement is used to make decisions
The switch statement allows us to make a decision from a number of choices
...
while loops
The loop does does not terminate when a continue statement is encountered
The switch statement can only for test equality
Break used to terminate or exit from a switch statement
The goto statement is used to transfer the control in a loop or function from one point
to any other portion in that program where it encounters a label
EXERCISES
1
...
else statement?
2
...
Write a program to perform arithmetic operations using switch statement?
4
...
Write
a
program
to
find
the
number
of
five
hundreds,Hundreds,
Fifties,Twenties,Tens,Fives,two’s and one’s in a amount given using while loop?
6
...
Write a program to generate N even Numbers and calculate its sum?
8
...
Write a program to calculate the sine series?
EXERCISES
10
...
while loops?
Title: Notes on C Programming- Control Statements
Description: It includes Control Statements such as Decision making and branching(if, if..else and so on), looping statements and Unconditional Jump statements.
Description: It includes Control Statements such as Decision making and branching(if, if..else and so on), looping statements and Unconditional Jump statements.