IP NOTES - Unit - 2 - Student
IP NOTES - Unit - 2 - Student
Control Structures
Simple sequential programs
Conditional Statements
If
if-else
if..else..if
switch
Looping Statements
For
While
do- while
Unconditional Statements
Break and Continue
Control Structures:
A program is nothing but the execution of sequence of one or more instructions.
Sometimes it is desirable to alter the execution of sequence of statements in the program
depending upon certain circumstances. This involves decision making through branching and
looping.
Conditional Statements
Conditional Statements require that the programmer specifies one or more conditions to be
evaluated or tested by the program, along with a statement (s) to be executed if the condition
is determined to be true, and optionally, other statements to be executed if the condition is
determined to be false.
The general form of a typical decision-making structure found in most of the programming
languages:
Decision Making/
Selection/ Branching
statements
One way Selection statement: The statement is executed if the value of the expression
is true. One way Selection is implemented using simple “if statement”.
IF STATEMENT:
The if statement is a powerful decision making statement and is used to control the flow of
execution of statements. C uses the keyword “if” to execute a set of statements or one
statements when the logical condition is true.
Syntax: If (condition or expression)
{
Statements-block;
}
Next statement;
In above syntax, the condition is checked first which allows the computer to evaluate
the expression first and then depending on the value of expression, the control
transfers to the particular statement.
If the expression is true (non- zero value) then the statement-block is executed and
next statement is executed.
If the expression is false (zero), directly next statement is executed without executing
the statement- block.
Note: Statement-block may be one or more statements. If more than one statement, then keep
all those statements in compound block ({ }).
Flow Chart:
Test False
Expression
True
Statement (s) of
Conditional Code
Statement x
#include <stdio.h>
Void main()
{
int a=5;
if(a>4)
{
printf("\nValue of „a‟ is greater than 4!");
}
if(a==4)
{
printf("\n\nValue of „a‟ equal to 4!");
}
}
Output: Value of „a‟ is greater than 4!
Rule2: The semi colon ; must not placed after the test-expression; it will place only end of
statement or block of statements.
Rule3: The open/close braces are placed more than one statement.
TWO WAY SELECTION STATEMENT: Selection statements allow the computer to take
a decision as to which statement is to be executed next based on the answer either true or
false. Two way selection is implemented using
if–else
nested if
IF – ELSE STATEMENT:
It is an extension of simple if. It has two statement blocks.
One is for if and it is executed when the condition is true.
The other block is for else and it is executed when the condition is false.
No multiple else statements are allowed with one if.
Syntax:
if(condition)
{
True statements; //if code
}
else
{
Flow Chart:
Statement x
Example: C program to check whether the given year is leap or not using if..else statement.
#include<stdio.h>
void main()
int year;
scanf("%d",&year);
if(year%4==0)
else
Flowchart:
Test
False Test True
Expression-3 Expression-1
Statement Block 2
Statement Block 1
Statement Block 3
Statement y
Example:
Write a C program to find the biggest of three numbers.
Program:
#include<stdio.h>
main()
{
int a,b,c;
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
Syntax: if (condition1)
{
// Statements block 1 to be executed if condition1 is true
}
else if (condition2)
{
// Statements block 2 to be executed if condition2 is true
}
// Additional else if blocks as needed
else
{
// Statements block 3 to be executed if none of the conditions are true
}
Flow chart:
If condition-1 True
Statement Block 1
False
Elseif True
condition-2 Statement Block 2
False
Elseif True
condition-3 Statement Block3
False
Else Block
Statement x
Example:
//C Program to find whether input year is leap year or not
SWITCH STATEMENT:
A switch statement allows a variable to be tested for equality against a list of values. Each
value is called a case, and the variable being switched on is checked for each switch case.
Syntax
switch(expression}
{
case label – 1: block – 1;
break;
case label – 2: block – 2;
break;
-----------------
-----------------
case label – n: block – n;
break;
default : default block;
break;
}
next statement;
It displays only integer constants, character constants and expression. We cannot give
floating point values as case labels.
First the expression will be evaluated. label – 1 ,label – 2, …. label – n are called case
labels.
The value of the expression is compared with all the case labels one by one. If it is equal
to one of the case label, the block associated with that case will be evaluated and the
break statement will indicate the end of the switch statement. If it is not equal to any of
the case labels the default block will be evaluated and this default is optional.
Break:
The break statement must be used at the end of each case because if it is not used,
then the case that matched and all the following cases will be executed.
For example, if the value of switch statement matched with that of case 2, then all the
statements in case 2 as well as the rest of the cases including default will be executed.
The break statement tells the compiler to jump out of the switch case statement and
execute the statement following the switch–case construct. Thus, the keyword break is
used to break out of the case statements.
Flow Diagram:
Example:
Write a C Program to make a simple Calculator to Add, Subtract, Multiply or Divide Using
switch-case.
scanf("%f%f",&a,&b);
printf("(1) ADDITION\n”);
printf("(2) SUBTRACTION\n");
printf("(3) MULTIPLICATION\n");
printf("(4) DIVISION\n");
printf("(5) REMAINDER\n");
printf("(0) EXIT\n");
scanf("%d",&ch);
switch(ch)
case 1: res=a+b;
printf("Addition: %.2f\n\n",res);
break;
case 2: res=a-b;
break;
case 3: res=a*b;
break;
case 4: res=a/b;
break;
case 5: res=(int)a%(int)b;
printf("Remainder: %d\n\n",res);
break;
break;
break;
Output:
(1) ADDITION
(2) SUBTRACTION
(3) MULTIPLICATION
(5) REMAINDER
(0) EXIT
Types of Loops:
C supports three types of iterative statements. They are
o while loop
o do–while loop
o for loop
In the while loop, the condition is tested before any of the statements in the statement
block is executed. (pre test – entry controlled loop).
If the condition is true, only then the statements will be executed, otherwise if the
condition is false, the control will jump to next statement.
Example program to calculate the sum of n natural numbers using while loop:
#include<stdio.h>
#include<conio.h>
main()
{
int i=1, sum=0, n;
clrscr();
printf(“enter the value of n:”);
scanf(“%d”,&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf(“\n sum is %d”,sum);
getch();
}
Output: enter the value of n:10
Sum is 55
The do–while loop continues to execute while the condition is true and when the
condition becomes false, the control jumps to the statement following the do–while
loop.
The major disadvantage of using a do–while loop is that it always executes at least
once, so even if the user enters some invalid data, the loop will execute.
Example program to calculate the sum of n natural numbers using do-while loop:
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,sum=0;n;
clrscr();
printf(“enter the value of n:”);
scanf(“%d”,&n);
do
{
sum=sum+i;
i++;
}
Y. Srinivasa Rao, Asst.Proressor, AIML & DS Dept., ACET Page 15
INTRODUCTION TO PROGRAMMING
while(i<=n);
printf(“\nsum is %d”,sum);
getch();
}
Output: enter the value of n:10
Sum is 55
FOR LOOP:
Like the while and do–while loops, the for loop provides a mechanism to repeat a task
till a particular condition is true.
For loop is usually known as a determinate or definite loop because the programmer
knows exactly how many times the loop will repeat.
Synax:
Example program to calculate the sum of n natural numbers using for loop:
#include<stdio.h>
#include<conio.h>
main()
{
int i,sum=0;n;
CONTINUE Statement:
When the compiler encounter a continue statement then the rest of the statements in
the loop are skipped and the control is unconditionally transferred to the beginning of
loop.
Syntax/general form:
Continue;
Label:
Statements;
…………….. backward jump
……………..
Goto label;