0% found this document useful (0 votes)
19 views

Unit II

The document provides an overview of control structures in C programming, detailing various types of statements such as sequential, conditional, and looping statements. It explains the null statement, if statements (including simple, if-else, and nested), switch statements, and looping constructs like do-while, while, and for. Additionally, it discusses the break and continue statements, highlighting their roles in controlling program flow.

Uploaded by

loknath4007
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)
19 views

Unit II

The document provides an overview of control structures in C programming, detailing various types of statements such as sequential, conditional, and looping statements. It explains the null statement, if statements (including simple, if-else, and nested), switch statements, and looping constructs like do-while, while, and for. Additionally, it discusses the break and continue statements, highlighting their roles in controlling program flow.

Uploaded by

loknath4007
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/ 13

22

UNIT - II
Control Structures

Q 1. Define a statement. What are the different types of statements?


Answer:
A Statement is an instruction in a program that can formed using C-Tokens. There are 3 types of statements.
i) Sequential Statements
ii) Conditional Statements (Decision Statements)
iii) Looping Statements.
➢ In Sequential, each and every statement is executed one by one without skipping.
➢ In Conditional, particular block of statements is executed based on condition.
➢ Conditional statements in C are: if and switch
➢ In Looping, a block of statements is executed up to number of times based on condition. Looping statements in C
are: do-while, while and for

Q 2. Define null statement. What is the importance of it?


Answer:

Null statement means it executes nothing in the program that means it performs no operation.

Example:

; → it is a null statement

Importance:

It is useful when the syntax of the language calls for a statement but no expression evaluation.

Q 3. Explain in detail about if statement with example.


Answer:

There are 3 types of if statement. They are:

1. Simple if

2. if-else statement

3. if-else-if statement (else-if ladder)

1. Simple if:

In simple if, if the condition is true, then True block statements are executed. Simple if does not concentrate on the
False condition.
23

Output: Enter any integer number : 20

Given number is divisible by 5.

Explanation: In the above program user need to entered integer value 20 is stored in variable “n” using if condition
modulo operation 20%5 then remainder is “0”. The given condition is True the if block statement is executed...

2. if-else statement:

In if-else-if statement, if the condition is true then True block statements are executed otherwise False block
statements are executed.
24

3. if-else-if statement (else-if ladder):

It is used in the scenario where there are multiple cases to be performed for different conditions.

In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed,
otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last
if none of the condition is true then the statements defined in the else block will be executed.
25
26

Q 4. Write short note on nested-if statement.


Answer:

Nested-if statement:

Writing an if statement inside another if statement is called nested if statement.

Output:
27

Q 5. Explain in detail about switch statement.


Answer:

Switch statement:

The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations
for the different possible values of a single variable called switch variable.

Here, we can define various statements in the multiple cases for the different values of a single variable.

The syntax of switch statement is given below


28

Q 6. Explain in detail about looping statements.


Answer:

The looping statements are used to execute a single statement or block of statements repeatedly until the given
condition is FALSE.

C has 3 looping statements:

1. do-while

2. while

3. for

1. do-while statement:

The do-while statement is used to execute a single statement or block of statements repeatedly until given condition
is False.

The do-while statement is also known as the Exit control looping statement.

In do-while, the block of statements is executed at least once.


29

2. while statement:

The while statement is used to execute a single statement or block of statements repeatedly until given condition is
False.

The while statement is also known as the Exit control looping statement.
30

3. for statement:

The for statement is used to execute a single statement or block of statements repeatedly until given condition is
False.
31

Q 7. Write the difference between do-while and while.

Q 8. Write short note on break statement.


Answer:

In C, the break statement is used to perform the following two things:

1. break statement is used to terminate the switch case statement

2. break statement is also used to terminate looping statements like while, dowhile and for.
32

The break statement execution is as shown in the following figure.

Q 9. Write short note on continue statement.


Answer:

➢ The continue statement is used to move the program execution control to the beginning of the looping
statement.
➢ The continue statement can be used with looping statements like while, do-while and for.
➢ When we use continue statement with while and do-while statements the execution control directly jumps to
the condition.
➢ When we use continue statement with for statement the execution control directly jumps to the modification
portion (increment/decrement/any modification) of the for loop.
➢ The continue statement execution is as shown in the following figure.
33

Program:

Output:
34

Q 10. Write the difference between break and continue.


Answer:

Important Question
1. Define null statement.
2. Write syntax and flow chart for if-else statement.
3. Define conditional statements.
4. Define looping statements.
5. Write syntax for do-while statement.
6. Write differences between while and do-while
7. Write a C program to print 1 to 10 natural number using for loop?
8. Write a C program to print given pattern using while loop?
*
**
***
****
9. Write a C program to print 10 to 1 natural number using do…while loop?
10. Write a C program to find greatest among two numbers using if-else condition?
11. Write a C program to print whether even or odd number using if-else condition?
12. Write a C program to print multiplication table using for loop?
5*1=5
5*2=10..

You might also like