0% found this document useful (0 votes)
18 views19 pages

UNIT 5control Statement Structure by Sir

This document serves as a reference note for C programming, focusing on control statements, including decision-making and looping constructs. It provides examples of various control statements such as if, else, switch, for, while, and do-while loops, along with sample code snippets and outputs. Additionally, it includes important questions and programming tasks related to these concepts.

Uploaded by

kamal
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)
18 views19 pages

UNIT 5control Statement Structure by Sir

This document serves as a reference note for C programming, focusing on control statements, including decision-making and looping constructs. It provides examples of various control statements such as if, else, switch, for, while, and do-while loops, along with sample code snippets and outputs. Additionally, it includes important questions and programming tasks related to these concepts.

Uploaded by

kamal
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/ 19

C Programming Reference Note

Control Statement
The statements which alter flow of execution of a program are known as control
statements.

There are two types of control statements: Decision Making & Repeating construct

Decision Making Statements Loop Statements or Repeating construct

1. if statements for loop

2. if.. else statements while loop

3. if.. else if statements do.. while loop

4. Nested if.. else statement

5. switch statement

Decision Making Statements


i. if Statements
An if statements is used to control the flow of execution of statements. The if statement
first evaluates an expression and then, it executes the statements within its block if the
value of the evaluated expression is true.

if(test_ expression)
{
statements-block;
}

College Note Prepared By: Lokendra Bk


C Programming Reference Note

Example 1

Write a program to read a number from user and test whether the number is negative
[Show message “The number is negative ” if it is negative number otherwise show
nothing].

#include<stdio.h>
#include<conio.h>
int main()
{
int num;
printf("Enter a number to be tested:");
scanf("%d", &num);
if(num<0)
printf("The number is negative");
getch();
return 0;
}

Output
Enter a number to be tested:-6
The number is negative

Nested if Statement
When one if statement is written within body of another if statement, it is called nested
if statement if statement. The several if statements shall be declared as
nested if statements.

Syntax:

if(expression)
{
if(expression)
{
//body
}
}

College Note Prepared By: Lokendra Bk


C Programming Reference Note

Example
Write a program to read percentage of marks obtained by a student in SLC and +2
level, Display message “Congratulation!! You have first division in both SLC and
+2” if both levels have percentage greater than or equal to 60.

#include<Stdio.h>

#include<conio.h>

int main()

float SLC_ per, plus2_ per; ;

print f("Enter percentage of SLC:");

scan f("%f", &SLC_ per)

print f("Enter percentage in +2 in science:");

scan f("%f", &plus2_per);

if(SLC_ per>=60)

if(plus2_per>=60)

print f("Congratulation!!");

print f("\n You have first division in both SLC and +2.");

getch () ;

return 0;

Output

Enter percentage of SLC: 70

College Note Prepared By: Lokendra Bk


C Programming Reference Note

Enter percentage in +2 in science: 60

Congratulation!!!!!

You have first division in both SLC and +2.

if…. else Statement


The if.. else statement is an extension of the simple if statement . It is used when there
are only two possible actions one happens when a test condition is true, and the other
when it is false.

if(test_ expression)
{
true-block statement (s);
}
else
{
false-block statement (s);
}

Example

Write a program to read a number from user and determine whether the number
is even or odd.

#include<stdio.h>

#include<conio.h>

int main()

int num, remainder;

College Note Prepared By: Lokendra Bk


C Programming Reference Note

print f("Enter a number:");

scan f("%d", &num);

remainder = num % 2;

// Modulo division

if (remainder == 0)

print f("The number is even");

else

print f("The number is odd");

getch();

return 0;

Output

Enter a number : 90

The number is : even

Enter a number: 17

The number is odd

Nested if.. else Statement


Similar to nested if statement if.. else statements shall also be written inside the body of
another if.. else body called nested if.. else statement.

if(condition)

College Note Prepared By: Lokendra Bk


C Programming Reference Note

{
if(condition2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
if(condition3)
{
statement-3;
}
else
{
statement-4;
}
}

Example

Write a program to read three number from user and determine the largest
number among them.

int main()

int n1, n2, n3;

print f("Enter 3 numbers:");

scan f ("%d%d%d", &n1 , &n2 , &n3);

if (n1>n2)

if(n1>n3)

print f("Largest = %d , n1);

else

print f("Largest = %d", n3);

College Note Prepared By: Lokendra Bk


C Programming Reference Note

else

if (n2>n3)

print f("Largest = %d , n2);

else

print f("Largest = %d, n3);

getch();

return 0;

Output

Enter 3 number : 100 50 10

Largest = 100

Enter 3 number : 50 100 10100

Largest = 100

if…. else if Statement


An if… else if statement is used when there are more than two possible actions
depending upon the outcome of test expression. When an action is taken, no others can
be executed or taken. In such situation, if…. else if… else if.. else structure is used.

if(condition)
{
statement-1;
}
else if(condition2)
{
statement-2;
}
else if(condition3)
{

College Note Prepared By: Lokendra Bk


C Programming Reference Note

statement-3;
}
else if(condition n)
{
statement-n;
}
else
{
default-statement;
}
statement-x;

Example

Write a program that reads total marks of a student in seven subjects. Then
calculate the percentage and determine the division. Use following conditions:

i. Percentage greater than or equal to 80- distinction,


ii. Percentage between 60 and 79- First Division
iii. Percentage between 45 and 59-second division
iv. Percentage between 32 and 44 – third division
v. Percentage less than or equal to 31 -fail.

#include<stdio.h>

#include<conio.h>

int main()

float nep, eng, math, phy, chem, bio, comp, percent;

print f("Enter the marks in 7 subjects:");

scan f("%f%f%f%f%f%f%f", &nep, &eng, &math, &phy, &chem, &bio, &comp);


percent =(nep+ eng + math +phy + chem + bio + comp) ;

if(percent>= 80)

print f("Distinction");

else if(percent >=60 && percent < 80)

College Note Prepared By: Lokendra Bk


C Programming Reference Note

print f ("First Division");

else if (percent>=45 && percent <60)

print f("Second Division")

else if (percent >=32 && percent <45)

print f("Third Division");

else

print f("Fail");

print f("\n Your percentage is: %f", percent);

getch();

return 0;

Output:

i. Enter the marks in 7 subjects: 45 78 89 35 76 80 35

First Division

Your percentage is :62.5714286

ii. Enter the marks in 7 subjects: 20 10 32 25 24 35 40

Fail

Your percentage is: 26.5714286

Loop or Iteration or Repeating Construct

Types of Loop
i. for loop
ii. while loop
iii. do-while loop

College Note Prepared By: Lokendra Bk


C Programming Reference Note

i. for Loop
for(counter_ initialization; test_ condition; increment or decrement)
{
statements; or body of loop
}

Example

Write a program to calculate factorial of a number.

#include<stdio.h>

#include<conio.h>

int main()

int num, i;

long fact=1;

print f("\n Enter a number whose factorial is to be calculated:");

scan f("%d", &num);

for(i=1;i<=num;i++)

fact*= i;

print f("\n The factorial is :%d", fact);

getch();

return 0;

Output

Enter a number whose factorial is to be calculated : 5

The factorial is : 120

College Note Prepared By: Lokendra Bk


C Programming Reference Note

While Loop
While(test_ condition)
{
body of loop
}

Example

Write a program to calculate factorial of a number using while loop.

#include<stdio.h>

#include<conio.h>

int main()

int num, i=1;

long fact = 1;

print f("Enter a number whose factorial is to be calculated:");

scan f("%d", &num);

while(i<=num)

fact*=i;

i++;

print f("The factorial is:%d", fact);

getch();

return 0;

College Note Prepared By: Lokendra Bk


C Programming Reference Note

do-while Loop
Do
{
//statements or body of loop;
}while(test_condition);

Example

Write a program to read a number from keyword until a zero or a negative number
is keyed in. Finally, calculate sum and average of entered numbers.

#include<stdio.h>

#include<conio.h>

int main()

int num, count=0;

float sum=0, avg;

do

print f("\n Enter number:\t");

scan f("%d", &num);

sum+=num;

count++;

}while(num>0);

sum=sum-num;

avg=(sum)/(count-1)

print f("\n The sum is :\t%d", sum);

print f("\n The average is: \t%f", avg);

College Note Prepared By: Lokendra Bk


C Programming Reference Note

getch();

return 0;

Output

Enter number: 10

Enter number: 20

Enter number: 15

Enter number: 5

Enter number: 0

The sum is: 50.000000

The average is: 12.500000

break Statement
The break statement terminates the execution of the loop and the control is transferred
to the statement immediately following the loop.

break;

Example

What is the output of following program?

#include<stdio.h>

#include<conio.h>

int main()

int i;

for(i=1; i<10; i++)

College Note Prepared By: Lokendra Bk


C Programming Reference Note

print f("\t%d, i);

if (i==5)

break;

getch();

Output

1 2 3 4 5

continue Statement
The continue statement is used when we want to continue running the loop.

continue;

Example

Write a program that asks for a number n from user and then display only even
numbers from 2 to n .

#include<stdio.h>

#include<conio.h>

int main()

int i, num;

print f("\n Enter a number:");

scan f("%d", &num);

College Note Prepared By: Lokendra Bk


C Programming Reference Note

print f("\n The even numbers from 2 to %d are: \n", num);

for (i=1; i<= num; i++)

if(i%2!=0)

continue;

print f("\t%d", i);

getch();

return 0;

Output

Enter a number: 20

The even numbers from 2 to 20 are:

2 4 6 8 10 12 14 16 18 20

goto STATEMENT
The goto statement is used to after the normal sequence of program execution by
unconditionally transferring control to some part of the program.

goto label;

label: statements;

College Note Prepared By: Lokendra Bk


C Programming Reference Note

Example

Write a program to ask two numbers. Display message” Either number is


negative ” if either number is negative ; otherwise display message ” Both
numbers are positive”.

int main()

int i, num1, num2;

print f("Enter first number:");

scan f("%d", &num1);

if(num<0)

goto negative;

print f("Enter second number:");

scan f("%d", &num2);

if(num2<0)

goto negative;

print f("The both numbers are positive");

getch();

return;

negative :

print f("Enter number is negative ");

getch();

return 0;

Output

a) Enter first number: 20

Enter second number: 10

The both numbers are positive

College Note Prepared By: Lokendra Bk


C Programming Reference Note

b) Enter first number: -5

Either number is negative

c) Enter first number : 6

Enter second number: -4

Either number is negative

switch STATEMENT
When there are a number of options available and one of them is to be selected on the
basis of some criteria, switch statement is used.

switch(variable or expression)
{
case caseConstant1:
statements;
break;
case caseConstant2:
statements;
break;

default:
statements;
}

Important Questions

Write short notes on

a. Local, Global, and Static variables

b. Conditional Operator

Asked on 2079 Exam

College Note Prepared By: Lokendra Bk


C Programming Reference Note

How do you swap the values of two integers without using the third temporary variable?
Justify with the example.
Asked on 2079 Exam

Write a program to demonstrate the following menu-driven program. The user will
provide an integer and alphabet for making choice and the corresponding task has to be
performed according as follow:

A. Find Odd or Even

B. Find Positive or Negative

C. Find the Factorial value

D. Exit

The choice will be displayed until the user will give “D” as a choice.
Asked on 2079 Exam

Why do we need a break and continue statement? Define formal argument and actual
argument in function with examples. Identify and list the errors in the following code.
int main(){
int a,b,c
scanf("%d%d%d, &a, &b, &c);
sum(a, b, c);
return -1;
}

void sum(int x, int y, int z){


int sum;
sum = a + b + c;
return sum;
}
Asked on 2079 Exam

Write a program to print largest among three numbers entered by the user.

What do you mean by jump statement? Explain each jump statement with example.
Write a program to check whether a number entered is prime or not.

Write a program that computes the sum of digits of a given integer number

College Note Prepared By: Lokendra Bk


C Programming Reference Note

Discuss different types of if statements with example of each. Differentiate if statement


with switch statement.

Write a program to calculate sum of first 10 odd numbers.

Write a program to check whether a number entered is even or odd.

What is break statement? Discuss with example. How the break statement is different
from continue statement?

Discuss different logical operation in detail.

What is looping statement? Discuss different looping statements with suitable example
of each.

Write a program to display first n prime numbers.

What do you mean by looping? Explain while loop with suitable example. Compare
while loop with do-while loop. Write a program to find sum and average of first n natural
numbers.

College Note Prepared By: Lokendra Bk

You might also like