Notes 4
Notes 4
A statement is the smallest logical entity that can independently exist in a C program. No entity
smaller than statement i.e. expressions, variable names, constants etc. can independently exist in a
C program unless and until they are converted into statements.
Non-Executable statements:
• These statements help the compiler to determine how to allocate memory, interpret and
compile other statements in a program.
• These statements are intended mainly for compiler and no machine code is generated for
them. Only executable statements play a role during the execution of a program.
• The order in which non-executable statements appear in a program is important. When a
compiler compiles a program, it scans all the statements from top to bottom. A non-
executable statement can only affect the statements which appear below it. Thus, a non-
executable statement should appear only before executable statements within a block.
• Only non-executable statements can appear outside the body of a function .
• Examples of non-executable statements are: function prototypes, global variable
declarations, constant declarations and preprocessor directive statements.
Executable statements:
• Executable statements represent the instructions which are to be performed when the
program is executed. The following are the important points about executable statements:
• For an executable statement, some machine code is generated by the compiler.
• Executable statement can appear only inside the body of a function.
• The examples of executable statements are: assignment statements, branching statements,
looping statements, function call statements etc.
Simple statements:
• Simple statement consists of a single statement. Simple statement terminates with a
semicolon. Following are the examples of simple statements:
• int variable=10; // definition statement
• variable+5; // expression statement
• variable=variable+10; // assignment statement
Compound statements:
• Compound statement consists of a sequence of simple statements enclosed within a pair of
braces . The following is an example of a compound statement:
Branching Statements:
Branching statements are used to transfer the program control from one point to another. They
are categorized as:
Selection Statements:
• Based upon the outcome of a particular condition, selection statements transfer control
from one point to another. Selection statements select a statement to be executed amongst
a set of various statements.
→The selection statements available in C are:
1. if statement
2. if-else statement
3. switch statement
If Statements
Two – way Decision logic (conditional statement)
- - -- - -} True
Forms of IF statement:
(i) Simple if statement.
(ii) If ….. else statement.
(iii) Nested if …. Else statement.
(iv) If Else if ladder.
start main ()
{
int num;
Enter a printf(“enter your no.”);
num scanf(“%d”,&num);
if(num >15)
No Num> yes {
10 Printf(“you are good ”);
?
}
Print (not Print (good)
good)
else{
Printf(“you are not good ”);
}
stop }
(ii) if-else statement.
if (test expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
::
Other statements in program
::
Program stmt: WAP to calculate gross salary of an employee as-if basic salary is
less than Rs. 1500, then HRA = 10% of basic, DA=90% of basic
If salary is > 1500, then HRA = Rs. 500,
DA = 98% of basic. Input basic salary by keyboard.
main()
{
float bs, gs, da, hra
printf(“Enter basic salary”);
scanf(“%f”,&bs);
if(bs<1500)
{
hra=bs*10/100;
da=bs*90/100;
}
else
{
hra =500;
da=bs*98/100;
}
gs= bs+da+hra;
printf(“Total Salary = %f”,gs);
}
if (test cond.-1)
{
if(test cond.-2)
{
statement-1
}
else
{
statement2
}
}
else
{
statement -3
}
:
other statements in program.
Example: Program to Check Leap Year (using nested if-else)
main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{ // year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else printf("%d is not a leap year.", year);
} // year is divisible by 4, hence the year is a leap year
else printf("%d is a leap year.", year );
}
else printf("%d is not a leap year.", year);
}
Program: One company decides to give bonus as 2% to all employees & 5% to all its
managers
having (salary) > 10,000 & if balance > 5000. Find increased salary after adding bonus.
main( )
{
float balance, bonus, salary;
printf(“Enter Balance, salary”);
scanf(“%f”, & balance);
scanf(“%f”, & salary);
if (salary > 10,000)
{
if (balance>5000)
bonus=.05 * salary;
else
bonus=.02 * salary;}
else
{
bonus=.02* salary;
}
salary=(salary+bonus);printf(“salary now=%f ”,salary);
main()
{
int marks;
printf("Enter the marks");
scanf("%d", &marks);
printf("\nresult is:");
if(marks > 79 && marks<=100)
{
printf("1st div. with Honors");
}
else if (marks > 59 && marks<=79)
{
printf("1st div.");
}
else if (marks > 49 && marks<=59)
{
printf("2nd Div.");
}
else if(marks > 39 && marks<=49)
{
printf("3rd Div.");
}
else
{
printf("Fail");
}
}
Example: A company insures its employees in the following cases:
• If the employee is married
• If the employee is unmarried, male & above 30 yrs.
• If the employee is unmarried, female & above 25 yrs.
Otherwise the employee is not insured.
Marital status, gender and age of employee are inputs. Then WAP to determine if the
driver is to be insured or not.
Now we will solve above program by 2 ways: using logical operators & without
using logical operators.
Here we can select one option among many alternatives. We use switch statement here.
switch statement:
• The switch statement is used to control complex
branching operations. When there are many
conditions, it becomes too difficult and complicated
to use if and if-else constructs.
• In such cases, switch statement provides an easy and
organized way to select among multiple options,
depending upon the outcome of a particular
condition.
The general form of switch statement is:
• switch(expression) //switch header
• statement //switch body
Example :
void main()
{
int i=2;
switch(i)
{
case 1:
printf(“I am in case 1\n”);break
case 2:
printf(“I am in case 2\n”);
case 3:
printf(“I am in case 3\n”);
default:
printf(“I am in default \n”);
}
} o/p → I am in case 2
I am in case 3
I am in default
But if we will insert break in each case then only one o/p → I am in case 2.
Statement-x
KEY POINTS: SWITCH STATEMENT
• Switch header :consists of keyword switch followed by
switch selection expression enclosed within parentheses.
• Switch selection expression must be of integral type (i.e.
integer type or character type).
• Switch body consists of a statement. The statement
constituting switch body can be a null statement,
expression statement, labeled statement, flow control
statement, compound statement etc.
• Generally, switch body consists of a compound statement,
whose constituent statements are case labeled statements,
expression statements, flow control statements and an
optional default labeled statement.
o
o Order of cases is not important
We can write as:
Case 10:
Statement …..
Case 599:
Statement …..
Case 305:
Statement …
o Char values are also permitted in case & switch. because char values are replaced by the
ASCII values of these characters.
As :-
Char c;
switch (c)
{
case ‘v’:
----
case ‘a’:
-----
}
}
main()
{
int a,c;
int b;
int opt;
printf(“Select an option (1….4):”);
scanf(“%d”, &opt);
switch (opt)
{ case 1:scanf(“%d%d”,&a,&b); c = a +b; printf(“%d”,c);break;
case 2: scanf(“%d%d”,&a,&b);c = a -b; printf(“%d”,c);break;
case 3: scanf(“%d%d”,&a,&b);c = a *b; printf(“%d”,c);break;
case 4: scanf(“%d%d”,&a,&b);c = a /b; printf(“%d”,c);break;
default:printf(“Select a value between 1 & 4, try again”);}return 0;}