Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Control Statements

Control Statements has the ability to change the sequential order of execution in
a programme. In other words, the control statements determine the 'flow of
Control' in a program. Control statements are of following types:

Decision Control Statement


LoopControl Statement
JumpControl Statement
Function
call Statement

Control statement has a Block, consists of either one or multiple sttements.


Multiple statements are to be written inside { }. For single statement block { }is
optional.
if statement - one way decision

syntax:
if (expression)
{
if (expression) S1; if (expression)
Block S2; S1;
next statements; $3; next statements

}
next statements

The expression can be any type of statement returning integer value not a

floating point value.

Ifthe result of expression is non zero (treated as true), then execution continues
from first statement of the Block.
If the result is zero (treated as false) then control of execution is transfered to
next statements to the Block. Statement(s) in the block are not executed.
Relational & Equality Operator

this expression is true if

X X IS equal to y

x!=y X is not equal toy


xy x is less than y

xy X 1S greater than y
xy x is less than equal to y
or

xy x is greater than or equal to y

An expression, involving the relational & equality operator, ifit is true, it


yields a value of 1 and if it is false, it gives a value 0. Similarly, if a numeric
expression is used as a test expression, any non-zero value (including
negative) will be treated as true, while a zero value will be considered as
false.
a -= b and a = b are not similar, as == is a test for equality, a =b is an
to be used
assignment operator. Therefore, the equality operator has
n refully.
WAP to give 60 mark grace if age is below 50 else give 40 marks grace
i.e. all will get atleast 40mark grace additional 20 mark will be given if the age is
below 50.
0

#include <stdio.h>
int main0

variable declaration

assign value to variables using printf) and scanf)

Computation:
Give grace mark 40 to everyone 40 mark grace to all
Test the age
if (age <
50) 20 mark more grace if age is below 50
marks =marks + 20;

Display Result
WAP to give 60 mark grace if age is below 50 else give 40 marks grace
i.e. all will get atleast 40mark grace additional 20 mark will be given if the age is
below 50.
14

#include <stdio.h>
int main0

int marks, age; //variable declaration


printf("Enter marks:");
scanf("%d", &marks); //value assigning
printf(" nEnter age:");
scanf("%d", &age); //value assigningg
marks = marks + 40; 40 mark grace to all

if (age< 50) 20 mark more grace if age is below 50


marks marks + 20;

Display Result
WAP to print KIIT student if you are in class 1 to 12 else print
KIIT University student.
23

#include <stdio.h>
int main0
{
int clasS //variable declaration
printf("Enter Class:");
scanf("%d', &class); /value assigning

printfC"nKIIT"); Display KlIT

if(class> 12) Dispaly University if class>12


printf University");
printf( student"); Display student
return(0);
WAP to display whether the wheatstone bridge is balanced or not,
if not find out value of the variable resistance to balance the bridge.
32

#include <stdio.h> b

int main0
O
float P,Q R, S; //variable declaration
assign the values to PQR,Susing scanfO0: O
if(P*S==R *Q) R

printf("\nThe wheatstone bridge is balanced");


return(0); //Exit from main function E
Wheatstone Bridge

printf("\nThe wheatstone bridge is NOT balanced");


S = R*Q/P;

printf("\nChoose S-%f to balance the bridge");: P/Q-- R/S


OR
return(0); //Exit from main function
P'S-- R*Q
Write a program to print the largest among three numbers (a, b, c)

a=30

max holds the largest number starting


max= b-50
from 1st number upto ith number

c-20

Step-1: Store the 1st number (a) in the variable max

Step-2: Compare value of max with 2nd (b) number and store largest among them
in max

Step-3: Compare value of max with 3rd (c) number and store largest among them
in max
Write a program to print the largest among three numbers (a, b, c)
42

#include <stdio.h>
int main(0 a-30

Declare the variables


max= b-50

Read the data from K/B using printf) and scanfO


Assign max to value of a c-20
Test
if(b>max)
max b;
Test
if(c>max)
max c;

Display the largest


}
Write a program to print the largest among three numbers (a, b, c)

#include <stdio.h>
int main0 a-30

int a, b, C, max:
printf\nEnter 3 numbers"); max-50 b-50
scanf("%d %d %d", &a, &b, &c);
max-a;
c-20

iffb>max)
max-b;

if(c>max)
max-c;
printf("\nThe largest No. is %d'", max);
return(0);
Find the output

int a-0;

if (a<1) if (a>1)
L1: printf(" XXX"); //Block L1: printf(" 000"); //Block
L2: printf(" YYY"); L2: printf(" 1111");

if (1) if (a)
L1: printf(" KiIT"); L1: printf(" KIIT"):
L2: printf(" University"); L2: printf(" University");

if (a=1) if (a=a++)
L1: printf" Comp. Sc."): L1: printf(" Comp. Sc.");
L2: printf(" Engineering"); L2: printf" Engineering');

You might also like