1.Conditional Statements:-
a)If Statements:-
The if statement is used to execute a block of statements if a specified condition is true.
Syntax:-
if(condition)
{
Statement 1;
}
Statement 2;
If the condition is true, then statement 1 is executed; otherwise, statement 2 is executed.
b) If-else Statements:-
It allows you to execute different blocks of code based on whether a specified condition is true or false.
Syntax:-
if(condition)
{
Statement 1;
}
else
{
Statement 2;
}
If the condition is true, then the if statement (Statement 1) is executed; otherwise, the else statement (Statement 2) is executed.
c) Multiple-If Statement:
syntax:-
if(condition)
{
Statement1;
}
else if(condition)
{
Statement2;
}
else
{
Statement3;
}
multipleif
statements to check multiple conditions sequentially. Eachif
statement is evaluated independently, and the corresponding block of code is executed if the condition is true.
d) Nested If Statement:
A nested if statement is an if statement that appears within the body of another if or else statement. This allows for more complex conditions and decision-making structures in a program. Here is the syntax and an example of a nested if statement in C
if (condition1)
{
//--Statement 1 is executed if condition1 is true
Statement1;
if (condition2)
{
//-- Statement2 is executed if both condition1 and condition2 are true
Statement2;
}
//-- More code you can execute outside of if block
}
else
{
//-- Statement3 is executed if condition1 is false
Statement3;
if (condition3)
{
//--Statement 4 is executed if condition3 is true
Statement4;
}
else
{
//-- Statement5 to be executed if both condition1 and Condition 3 are false.
Statement5;
}
// More code you can execute within the outer else block
}
// Code you can execute outside the if-else blocks
e) Conditional/Teenary Expression:-
syntax:-
Expression1?Expression2:Expression3;
In this example, the ternary expression (number >= 0) ? "positive" : "negative" checks if the number is greater than or equal to 0. If true, the expression evaluates to "positive"; otherwise, it evaluates to "negative".
f)Switch Statement:-
The switch statement in C provides a way to handle multiple cases based on the value of an expression.
Syntax:-
switch (expression)
{
case value1:
// Block of code to be executed if expression equals value1;
break;
case value2:
// Block of code to be executed if expression equals value2;
break;
// additional cases as needed
default:
// block of code to be executed if none of the cases match the expression;
}
a) expression: the variable or value that is being tested.
b) case value 1: The first case to check. If the expression equals value 1, the code within this case is executed.
c) break: Terminates the switch statement. If omitted, execution will continue to the next case without checking for further matches.
d) default: If none of the cases match, the default block's code is executed.
Example:-
#include <stdio.h>
#include <conio.h>
void main() {
char grade;
printf("Enter your grade: ");
scanf("%c", &grade);
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good!\n");
break;
case 'C':
printf("Satisfactory.\n");
break;
case 'D':
printf("Needs Improvement.\n");
break;
default:
printf("Invalid grade.\n");
}
getch();
}
a). The user is prompted to enter a grade ('A', 'B', 'C', 'D').
b). To check the value of grade, the program uses a switch statement.
c). Based on the value of grade, it executes the corresponding case block.
Note: The break statement is used to exit the switch statement after a case is executed. If the break is omitted, execution will continue to the next case.
2. Unconditional Statements:
a)Break Statement:-
It is used to terminate the loop.
The break statement is used to terminate the execution of the innermost loop (for, while, or do-while).
syntax:-
for (initialization; condition; alteration) {
// Statement to be executed in each iteration
if (condition_to_out of loop) {
break; // Exit the loop prematurely
}
}
b)Continue Statement:-
It is used to skip the number of terms.
In C programming, the continue statement is used to skip the rest of the code inside a loop for the current iteration and move on to the next iteration of the loop. It is typically used within loops (for, while, or do-while) to jump to the next iteration prematurely.
Syntax:-
for (initialization; condition; alteration)
{
// block of code before the continue statement
if (condition_to_skip)
{
continue; // Skip the rest of the block of code in the current alteration and move to the next alteration
}
// code after the continue statement
}
when (condition_to_skip) is true then it skip current alteration and move to next alteration.
c) goto Statement:-
It is used to jump the control from one statement to another.
Example:-
int i;
for(i=1; i<=10; i++)
{
if(i==5)
{
goto label1;
}
printf("%d\n",i);
}
printf("out of loop");
label1:
printf("Hello");
Result:-
1
2
3
4
Hello
Conclusion:-
label: It is a user-defined identifier followed by a colon (:
). The label is a target for thegoto
statement, indicating the position in the code where control should be transferred.
Here we use label1 in the example instead of label.
3. Repetitive statement:-
Repetitive statements, also known as loops, are used to execute a block of code repeatedly as long as a certain condition is true. There are three main types of loops in the C language: for, while, and do-while. Each type serves different purposes but shares the common objective of repetitive execution.
a). For Loop:
The for loop is typically used when the number of iterations is known beforehand.
Steps to work for loop:
1. Intialization
2.Condition
3. Body of loop
4. Alteration
Syntax:-
for (initialization; condition; Alteration)
{
Body of the loop;
}
Example:-
int i;
for(i=1;i<=5;i++)
{
printf("%d",i);
}
Result:-
1
2
3
4
5
b)While Loop:-
The while loop is used when the number of iterations is not known beforehand, and the loop continues as long as a specified condition is true.
syntax:-
While(condition)
{
Body of loop;
}
Example:-
int i=1;
while(i<=5)
{
printf("%d\n",i);
i++;
}
Result:-
1
2
3
4
5
c)do-while loop:-
The do-while loop is similar to the while loop, but it ensures that the loop body is executed at least once before the condition is checked.
Syntax:-
do
{
Body of loop;
}
while(condition);
Example:-
int i=1;
do
{
printf("%d\n",i);
i++;
}
while(i<=5);
Result:-
1
2
3
4
5