Unit II
Unit II
UNIT - II
Control Structures
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.
1. Simple if
2. if-else statement
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
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
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
Nested-if statement:
Output:
27
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 looping statements are used to execute a single statement or block of statements repeatedly until the given
condition is FALSE.
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.
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
2. break statement is also used to terminate looping statements like while, dowhile and for.
32
➢ 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
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..