PPS Module No.3
PPS Module No.3
- 3
Conditional Branching & Loops
1)Branching/Selection Statements –
• In C, Conditional branching statements are used to execute particular code blocks based on
condition (as needed). The various categories of conditional branching statements in C are
as follows :
• If Statement
• If-else Statement
• The if statement executes a block of code only if the given condition is true. If the
condition is false, the program skips the block.
• Syntax :
• E.g. : #include<stdio.h>
int main() {
int num=10;
if (num>0) {
return 0; }
2)The If-else Statement :
• By offering a different block of code to run when the condition is false, the “if-else”
statement builds on the “if” statement.
• Syntax :
if (condition) {
else {
}
• E.g. : #include<stdio.h>
int mian()
Int num=-5;
If(num>0) {
else
|
3) The nested If-else Statement :
• A Nested if-else statement is an if-else statement inside another if or else block. It allows checking
multiple conditions in a hierarchical manner.
• Syntax : if (condition1) {
if (condition2) {
} else {
} else {
}
• E.g. : #include <stdio.h>
int main() {
int num;
scanf("%d", &num);
return 0;
}
3) The if-else-if Ladder Statement :
• When there are multiple conditions, an if-else-if ladder is used to check each condition
in sequence.
} else if (condition2) {
else if (condition3) {
else {
}
• E.g. : #include <stdio.h>
int main() {
int num = 0;
if (num > 0) {
else {
return 0;
}
3) Switch Case Statement :
• Multiple actions can be taken using “Switch” statement based on the value of a variable or
expression. It is useful for menu-driven programs
• Syntax : Switch(expression) {
case value1:
break;
case value1:
break;
default:
1) For Loop
2) while Loop
3) Do-While Loop
1) While Loop:
• The while loop executes a block of code as long as the condition remains true.
// Code to execute
• If the condition is false initially, the loop will not execute at all.
E.g. :
#include<stdio.h>
int main() {
int i=1;
while(i<=5) {
printf(“%d”,i);
i++;
return 0;
}
1) For Loop:
• The for loop is used when the number of iterations is known in advance. It includes
initialization, condition & increment/decrement in a single line.
// code to execute
#include <stdio.h>
int main() {
printf(“%d”,i);
return 0;
}
1) do-while Loop:
• The do-while loop is similar to while, but it ensures the loop executes at least once,
even if the condition is false initially.
• Syntax : do {
// code to execute
} while (condition);
#include <stdio.h>
int main() {
int I =1;
do {
printf(“%d”,i);
i++;
} while(i<=5) ;
return 0;
}
3) Jump Statements :
• Jump statements in C alter the normal flow of program execution. These statements
allow us to jump from one part of the program to another.
1) break Statement
2) continue Statement
3) goto Statement
4) return statement
1) break Statement -
• The break statement immediately exits the loop or switch case when encountered. It is commonly
used to stop a loop based on a specific condition.
• Syntax : break;
e.g. #include<stdio.h>
int main() {
for(int i=1;i<=10;i++) {
if(i==5) {
break;
printf(“%d”,i);
return 0; }
1) Continue Statement -
• The continue statement skips the current iteration of a loop and moves to the next one
without terminating the loop.
• Syntax : continue ;
e.g. #include<stdio.h>
int main() {
for(int i=1;i<=10;i++) {
if (i%2==0) {
} printf(“%d”,i);
return 0; }
3) goto Statement -
• The goto statement is used to transfer control to a labeled statement in the same
function. It can be useful for breaking out of deeply nested loops but is generally
discouraged because it makes code harder to read and maintain.
label;
e.g. #include<stdio.h>
int main() {
int num=1;
start:
printf(“Number : %d”,num);
num++;
if(num<=5)
goto start;
return 0;
}
3) return Statement -
• The return statement is used to exit from a function and optionally return a value to the caller.
e.g. #include<stdio.h>
return a+b; }
int main() {
printf(:Sum:%d”,result);
return 0;