1.What are different conditional statements in c? or What is selection statement?
Write
it's types?
There are several types of conditional statements that allow you to control the flow of your program based on
certain conditions. Here are the different types of conditional statements:
1.if Statement:
The if statement is used to execute a block of code if a certain condition is true.
2.if-else Statement:
The if-else statement is used to execute a block of code if a certain condition is true, and another block of code if the
condition is false.
3.if-else if Statement:
The if-else if statement is used to check multiple conditions and execute a block of code based on the first true
condition.
4.Nested if Statements:
A nested if statement is a type of conditional statement where an if statement is placed inside another if statement
5.Switch statement:
The switch statement is used to execute different blocks of code based on the value of a variable. It is shortcut of
long if-else statement.
2. What is the use of continue key word?
The continue statement just skips any instructions after it on that iteration of the loop. The current iteration of the
loop is terminated, and the loop statement is executed again as if the last instruction of the loop body has been
reached. Instead, the remaining loop statements are skipped and the computation proceeds directly to the next pass
through the loop. This statement is written as, continue; without any embedded statements or expressions.
continue statement
• Skips the remaining statements in the body of a while, for or do-while loops.
• Loop-continuation test is evaluated immediately after the continue statement is
executed
• In the for loop , increment expression is executed, then the loop-continuation test is
evaluated
Example program:
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
continue; // skip the current iteration when i is 5
}
printf("%d ", i);
}
printf("\n");
return 0;
}
Output:
1 2 3 4 6 7 8 9 10
3.Distinguish between Entry control and Exit control looping statements?
Entry Control Looping Statements:
Entry Control Looping Statements are those where the condition is checked before the loop body is executed. If the
condition is true, the loop body is executed; otherwise, the loop is terminated.
Examples of Entry Control Looping Statements:
for loop
while loop
Exit Control Looping Statements
Exit Control Looping Statements are those where the condition is checked after the loop body is executed. If the
condition is true, the loop body is executed again; otherwise, the loop is terminated.
Examples of Exit Control Looping Statements:
do-while loop
4.What is the use of Break statement?
When a break statement appears in a loop or in a switch control structure, it terminates the execution at that point
and transfers execution control to the statement that immediately follows the loop or switch case construct
statement. If a break statement is not
included in each case statement, whenever the match is found program executes the statements following that case
and also all the subsequent case statements and default statement.
break statement
• Causes immediate exit from a while, for, do/while or switch structure.
• Program execution continues with the first statement after the structure.
Example program:
#include<stdio.h>
void main ( )
{
int n;
printf ( "Enter a value for n : " );
scanf ( "%d", &n);
switch ( n )
{
case 1:
printf("ONE");
break;
case 2:
printf("TWO");
break;
case 3:
printf("THREE");
break;
default:
printf("OUT OF RANGE");
}
}
output:
Enter a value for n : 2
TWO
5.Distinguish between while and do-while?
while Do-while
It is entry controlled loop It is exit controlled loop
Loop executes the statement only Loop executes the statement at least
after testing the condition. once
The loop terminates if condition The condition is tested before
become false execution.
There is no semicolon at the end of There is a semicolon at the end of
while statement. the while statement.
6.Give an example of iteration statements in C.
Here are some examples of iteration statements in C:
For Loop:
#include<stdio.h>
void main(){
int i;
for (i = 0; i < 5; i++) {
printf("%d ", i);
}
}
Output: 0 1 2 3 4
while loop:
#include <stdio.h>
void main(){
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
}
Output: 0 1 2 3 4
do while loop:
#include <stdio.h>
void main(){
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
}
Output: 0 1 2 3 4
7.Write syntax of If-else statements.
if-else Statement :
The if-else statement is used to execute one block of code if a condition is true and another block if the condition is
false.
Syntax of if-else Statement:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
8.What is the use iterative statements?
Iteration statements, commonly known as loops, are statements in programming used to execute part of code
repeatedly based on condition or set of conditions.
Repeat a task: Perform a task multiple times, such as reading input from a user, processing data, or performing
calculations.
Simplify code: Loops can simplify code by reducing the amount of repetitive code and making it more concise.
Improve performance: In some cases, iterative statements can improve performance by reducing the number of
function calls or minimizing memory allocation.
9.Write a while loop that displays numbers 2, 4, 6, 8.......20.
#include <stdio.h>
int main() {
int i = 2;
while (i <= 20) {
printf("%d ", i);
i += 2;
}
return 0;
}
Output:
2 4 6 8 10 12 14 16 18 20