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

PPS Module No.3

The document covers conditional branching and looping statements in C programming, detailing types such as if, if-else, nested if-else, switch, for, while, and do-while loops. It also explains jump statements like break, continue, goto, and return, providing syntax and examples for each. This module serves as a comprehensive guide for implementing control flow in C programs.

Uploaded by

bubble.love1601
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

PPS Module No.3

The document covers conditional branching and looping statements in C programming, detailing types such as if, if-else, nested if-else, switch, for, while, and do-while loops. It also explains jump statements like break, continue, goto, and return, providing syntax and examples for each. This module serves as a comprehensive guide for implementing control flow in C programs.

Uploaded by

bubble.love1601
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Module No.

- 3
Conditional Branching & Loops
1)Branching/Selection Statements –

• Branching statements are mainly categorized as follows.

1) Conditional Branching Statements

2) Unconditional Branching Statements

• Conditional Branching 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

• Nested If-else Statement

• Switch Statement (Menu-Driven Programs)


1)The If 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 :

if (condition) { // Code to execute if the condition is true }

• E.g. : #include<stdio.h>

int main() {

int num=10;

if (num>0) {

printf(“the number is positive”); }

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) {

// Code to execute if the condition is true

else {

// code to execute if condition is false

}
• E.g. : #include<stdio.h>

int mian()

Int num=-5;

If(num>0) {

printf(“The number is positive”); }

else

printf(“The number is Negative”);

|
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) {

// Executes if condition1 is true

if (condition2) {

// Executes if both condition1 and condition2 are true

} else {

// Executes if condition1 is true but condition2 is false

} else {

// Executes if condition1 is false

}
• E.g. : #include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num >= 0) { // First condition

if (num == 0) { printf("The number is zero.\n"); }

else { printf("The number is positive.\n"); }

} else { printf("The number is negative.\n"); }

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.

• Syntax : if (condition1) { // Code if condition1 is true

} else if (condition2) {

// Code if condition2 is true }

else if (condition3) {

// Code if condition3 is true }

else {

// Code if none of the above conditions are true

}
• E.g. : #include <stdio.h>

int main() {

int num = 0;

if (num > 0) {

printf("The number is positive.\n");

} else if (num < 0) {

printf("The number is negative.\n"); }

else {

printf("The number is zero.\n");

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:

// Code to execute if expression == value1

break;

case value1:

// Code to execute if expression == value1

break;

default:

// Code to execute if no case matches }


• E.g. : #include <stdio.h>
int main() {
int day =3;
Switch(day) {
case1 :
printf(“Monday”);
break;
case2 :
printf(“Tuesday”);
break;
default:
printf(“invalid day !”);
}
return 0; }
2) Iterative/Looping Statement :

• Iteration Statement, commonly known as loops, it is used to execute part of code


repeatedly based on condition or set of conditions. These Constructs are important for
performing repetitive tasks efficiently.

• Types of Iteration Statements in Programming :

• There are mainly three types of iteration statements –

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.

• Syntax : while (condition) {

// Code to execute

• The condition is checked first before executing the loop body.

• 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.

• Syntax : for (initialization; condition; increment/decrement) {

// code to execute

• The initialization runs once at the start.

• The condition is checked before each iteration.

• The increment/decrement updates the loop variable.


E.g.

#include <stdio.h>

int main() {

for (int i=1; i<=5; i++) {

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);

• The loop executes once before checking the condition

• If the condition is true, the loop continues; otherwise, it stops.


E.g.

#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.

• Types of Jump Statements in C :

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) {

continue;//skips even numbers

} 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.

• Syntax : goto label;


…..

label;
e.g. #include<stdio.h>

int main() {

int num=1;

start:

printf(“Number : %d”,num);

num++;

if(num<=5)

goto start;

printf(“loop ended using goto”);

return 0;

}
3) return Statement -

• The return statement is used to exit from a function and optionally return a value to the caller.

• Syntax : return value;

e.g. #include<stdio.h>

int add(int a, int b) {

return a+b; }

int main() {

int result = add(5,10);

printf(:Sum:%d”,result);

return 0;

You might also like