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

Chapter 1-Module 2

Module 2 covers decision making and branching in programming, detailing conditional and unconditional branching statements. It explains various types of conditional statements such as if, if-else, nested if, ladder if, and switch, along with their syntax and examples. Additionally, it discusses the use of break, continue, goto, and return statements in C programming.

Uploaded by

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

Chapter 1-Module 2

Module 2 covers decision making and branching in programming, detailing conditional and unconditional branching statements. It explains various types of conditional statements such as if, if-else, nested if, ladder if, and switch, along with their syntax and examples. Additionally, it discusses the use of break, continue, goto, and return statements in C programming.

Uploaded by

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

Module 2:

Decision making and branching

Branching statements:
The statement which transfer the control from one place to another place.

Branching can be classified as:

• Conditional branching statement.


• Unconditional branching statement.

Conditional branching statements:

1. Simple if
2. If else
3. Nested if
4. Ladder if
5. Switch

Un – conditional branching statements:

1. Goto
2. Break
3. Continue
4. Return

If statements: The statement should execute when the condition is true.

Syntax: if (Expression)
{
Statements;
}
If -Else statements:
The statement should execute when the condition is true,Or else part will
execute.

Syntax: if (Condition)
Statement 1;
else
Statement 2;

Example:

1]Write the C Program to check the given number is even or odd.


2] Write the c program to check whether the given year is leap year or not.
3] write a C Program to check whether the given number is positive or
negative.
Nested if:

An if or if-else statements in another if or if-else statement is called


nested if statements.

Syntax:

if(condition1)

{ //if 1st condition true , it will enter into 2nd condition.

if(condition2)

{ //if both 1st and 2nd condition true, it will execute the
statement 1.

Statement 1:

else // if 1st condition true but 2nd condition false, it will execute
the else part of 2nd condition.

Statement 2;

else // if 1st condition itself is false, it will execute the else part of 1 st
contion

Statement 3;
}

Example 1:

Write the C Program to find the maximum of three numbers.


Ladder if statements:
If after another if is called ladder if.
Syntax:
if (condition1)
{
//These statements would execute if the condition1 is true
}
else if(condition2)
{
//These statements would execute if the condition2 is true
}
else if (condition3)
{
//These statements would execute if the condition3 is true
}
.
.
else
{
//These statements would execute if all the conditions return false.
}

Example for ladder if :


Write a program to display the grades based on the marks scored by a student.
Marks: Grades:
0 to 39 Fail
40 to 59 First class
60 to 79 Second class
80 to 100 Distinction
2] Develop a c program to check for the type of triangle.
Switch Statement:
The switch case statement is used when we have multiple options and
we need to perform a different task for each option.
Syntax:
switch (variable or an integer expression)
{
case constant:
//C Statements
;
case constant:
//C Statements
;
default:
//C Statements
;
}
Example 1:
To Print the calculator.

C break statement:
The break is a keyword in C which is used to bring the program control out of
the loop. The break statement is used inside loops or switch statement. The break
statement breaks the loop one by one, i.e., in the case of nested loops, it breaks
the inner loop first and then proceeds to outer loops. The break statement in C
can be used in the following two scenarios:
1. With switch case
2. With loop

Syntax:
//loop or switch case
break;
Example:

C continue statement
The continue statement in C language is used to bring the program
control to the beginning of the loop. The continue statement skips some lines of
code inside the loop and continues with the next iteration. It is mainly used for a
condition so that we can skip some code for a particular condition.

Syntax:
//loop statements
continue;
//some lines of the code which is to be skipped

C goto statement :
The goto statement is known as jump statement in C. As the name
suggests, goto is used to transfer the program control to a predefined label.
Syntax:
label:
//some part of the code;
goto label;
Return statement:
If it is a void It return nothing but If it’s a int it will return 0.

You might also like