0% found this document useful (0 votes)
44 views32 pages

4 Statements

Here is a program that uses a compound statement (statements grouped within curly braces {}) in the for loop to display multiples of 6 in a set of N numbers: #include <stdio.h> int main() { int n, num, count = 0; printf("Enter the value of n: "); scanf("%d", &n); for(int i=1; i<=n; i++) { printf("Enter a number: "); scanf("%d", &num); if(num%6 == 0) { printf("%d is a multiple of 6\n", num); count++; } } printf("Total multiples of 6: %d

Uploaded by

sandhya
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)
44 views32 pages

4 Statements

Here is a program that uses a compound statement (statements grouped within curly braces {}) in the for loop to display multiples of 6 in a set of N numbers: #include <stdio.h> int main() { int n, num, count = 0; printf("Enter the value of n: "); scanf("%d", &n); for(int i=1; i<=n; i++) { printf("Enter a number: "); scanf("%d", &num); if(num%6 == 0) { printf("%d is a multiple of 6\n", num); count++; } } printf("Total multiples of 6: %d

Uploaded by

sandhya
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/ 32

PROGRAM STATEMENTS

• A statement performs an action when a program is executed.


• All C program statements are terminated with a semi-colon
(;).
• Declaration :It is a program statement that serves to
communicate to the language translator information about the
name and type of the variables needed during program
execution.
• Expression statement:
• It is the simplest kind of statement which is no more than an expression
followed by a semicolon.
• An expression is a sequence of operators and operands that specifies
computation of a value . Example :x = 4
• Compound statement is a sequence of statements that may be
treated as a single statement in the construction of larger
statements.
The statements are enclosed in { }
• Labelled statements can be used to mark any statement so that
control may be transferred to the statement by switch statement.
• Control statement is a statement whose execution results in a
choice being made as to which of two or more paths should be
followed.
– It determine the ‘flow of control’ in a program.
– Different control statements are
 Selection statements allow a program to select a particular
execution path from a set of one or more alternatives.
 if..else and Switch statements .
 Iteration statements are used to execute a group of one or
more statements repeatedly.
 while
 for
 do..while
 Jump statements cause an unconditional jump to some other
place in the program.
Goto, break, continue statements
• Assignment statement
variable _name = expression
Expression : constants, variables combined with operators
Ex:
a+20/10+b
a
10
First compute the expression value and then assign to left
hand side variable
= is called assignment operator
Output statement : Printf()
available in <stdio.h> header file

Printf(“format string”, expressions separated by


commas)
#include<stdio.h>
#include<conio.h>
void main()
{
int length,breadth,area,peri;
length=10;
breadth=20;
area=length*breadth;
peri=2*(length+breadth);
printf("%d %d %d %d ",length,breadth,area,peri);
return;
}
• Input statement
Scanf(“%d “, &a);
Scanf(“%f”,&b)
• Variable name must be preceded by &
• Only variable names are permitted
IF statement
if (condition) statement1;
else statement2;

Statement1 & statement2 can be


– Assignment
– Input
– Output
– Another IF statement etc
I/O statements
• Generally ,standard input and output devices are the keyboard and
the screen.
• Standard I/O functions
– getchar()
– putchar()
– scanf()
– and printf() are in-built in C.
• The input/output functions are of two kinds:
 non formatted &
 formatted functions.
NON-FORMATTED INPUT AND
OUTPUT
• These can handle one character at a time
• Functions that provide for character-oriented input and output.
 int getchar(void); function for character input
 int putchar(int c); function of character output

• getchar() is an input function that reads a single character from the standard input
device, normally a keyboard.
• For the input functions it does not require <Enter> to be pressed after the entry of
the character.
• putchar() is an output function that writes a single character on the standard
output device, the display screen.

• There are two other functions, gets() and puts(), that are used to read and
write strings from and to the keyboard and the display screen respectively
#include<stdio.h>
#include<conio.h>
main()
{
char c1;
c1='$';
printf("using printf %c\t",c1);
putchar(c1);
return(0);
}
FORMATTED INPUT AND OUTPUT FUNCTIONS
• When input and output is required in a specified format the
standard library functions scanf() and printf() are used.
 The scanf() function allows the user to input data in a specified format.
It can accept data of different data types .
 The printf() function allows the user to output data of different data
types on the console in a specified format.
• The format string in printf(), enclosed in quotation marks, has
three types of objects:
 Ordinary characters: these are copied to output,
 Conversion specifier field:
 denoted by % containing the codes listed in Table 1. and by optional modifiers such as
width, precision, flag, and size
 The conversion specifier field is used to format printed values, often to arrange things
nicely in columns.

 Control code: optional control characters such as \n,\b, and \t.


COMMONLY USED CONTROL CODES
• control codes are also known as
escape sequences.
• If any of these are included in the
format string, the corresponding ASCII
control code is sent to the screen, or
output device, which should produce
the effect listed.
• The control code and conversion
specifier may be embedded within the
character string.
#include<stdio.h>
main()
{
int a=5; float b=1.2;
double c=2.22; char d='$';
printf("a=%d\t b=%e c=%g d=%c %s
%%\n",a,b,c,d,"Hello");

}
The switch statement
The general format of a switch
statement is
switch(expr)
{
case constant1: stmtList1;
break;
case constant2: stmtList2;
break;
case constant3: stmtList3;
break;
………………………….
………………………….
default: stmtListn;
}

The C switch construct


• Expression can be integer or char type
• There can be multiple case labels
• Each case label is value of ‘switch’ expression
• Control goes to first matching case label and executes
until break is encountered or end of ‘switch’ statement
• ‘Default’ label is optional and need not be the last
statement of Switch.
• Break statement is also optional.
• If break is omitted, control proceeds from matching
case label until break is encountered or end of ‘switch’
statement
// Perform arithmatic operations
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
char op;
clrscr();
scanf("%d %d ",&a,&b);
op=getchar();
switch(op)
{
case '+': printf("%d\t",a+b);break;
case '-': printf("%d\t",a-b);break;
case '%': printf("%d\t",a%b);break;
case '/': printf("%d\t",a/b);break;
case '*': printf("%d\t",a*b);break;
default: printf("invalid value for the expression");
}
return(0);
}
// multiply the number by cur value

#include<stdio.h>
#include<conio.h>
main()
{
int n,cur ;
scanf("%d %d",&n,&cur);
switch(cur)
{
case 10:printf("%d\t",n*10);break;
case 20:printf("%d\t",n*20);break;
case 50:printf("%d\t",n*50);break;
case 100:printf("%d\t",n*100);break;
}

return(0);
}
FOR loop
The general form of the for statement

for(initialization; TestExp; updating) statement;

Statement cane be
• any single statement like input/output/assignment/if-
else/any loop/return etc
• compound statement.

Compound statement is set of statements enclosed in { }


#include<stdio.h>
void main()
{
int num,n,i;
printf("enter the number of values");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter the value\n ");
scanf("%d",&num);
if(num>0) printf(" %d is positive\n",num);
else if(num<0) printf(" %d is negative\n",num);
else printf("%d is zero\n",num);

return;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=20;i<=30;i=i+2) {printf("%d\t",i); }
return;

}
For-loop Flow chart
• First initialization is done and then condition is
tested.
• If the condition is true, loop statement is
executed, updation is done and again condition is
tested and so on.
• Testing the condition, executing the statement,
updating are repeated as long as the condition is
true.
• When condition becomes false, control goes the
next statement after the loop.
• Initialization is done only once
#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
for(i=1 ; i<5 ;i++) printf("%d ",i);
return(0);
}
Output : 1 2 3 4
Multiple initializations separated by
comma
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1,j=5 ; i<5 ; i++) printf("%d %d\n",i,j);
return(0);
}
Output : 1 5
25
35
45
Multiple updations
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1,j=5 ; i<5; i++,j-- ) printf("%d %d\n",i,j);
return(0);
}
Output : 1 5
24
33
42
Multiple conditions separated by commas

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=50,j=10,k=100;i>=25,j<50;i=i-5,j=j+5)
{printf("%d %d %d\n",i,j,k); }
return;
}
You come out of loop when last condition is satisfied
Multiple conditions can be combined using logical operator to
form a compound condition

#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
for(i=1,j=5 ; i<5&&j<0 ; i++,j--) printf("%d %d\n",i,j);
return(0);
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=50,j=10,k=100;i>=25||j<50;i=i-5,j=j+5) {printf("%d %d
%d\n",i,j,k); }
return;

}
// display count of number of Even, odd numbers in set of given N numbers

#include<stdio.h>
#include<conio.h>
main()
{
int n,num,even_count=0,odd_count=0,i;
printf("enter value of n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter value\n");
scanf("%d",&num);
if(num%2==0) even_count++;
else odd_count++;

}
printf("number of even numbers=%d #odd=%d\n",even_count,odd_count);
return;
}

Assignment : Display multiples of 6 in given N numbers


Compound statement in the for-loop
#include<stdio.h>
#include<conio.h>
main()
{
int i,num;
clrscr();
for(i=1;i<=10;i++)
{
scanf("%d",&num);
printf("%d %d\n",i,num);
}

return(0);
}

You might also like