0% found this document useful (0 votes)
3 views14 pages

Module 2

Module 2 covers decision-making and branching statements in programming, specifically in C. It details conditional and unconditional branching statements, including if, if-else, nested if, ladder if, switch, break, continue, goto, and return statements. The module provides syntax and examples for each type of statement to illustrate their usage in programming.

Uploaded by

devopsnhc1920
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)
3 views14 pages

Module 2

Module 2 covers decision-making and branching statements in programming, specifically in C. It details conditional and unconditional branching statements, including if, if-else, nested if, ladder if, switch, break, continue, goto, and return statements. The module provides syntax and examples for each type of statement to illustrate their usage in programming.

Uploaded by

devopsnhc1920
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

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

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE


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.

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE


2] Write the c program to check whether the given year is leap year or not.

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE


3] write a C Program to check whether the given number is positive or
negative.

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE


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 2 nd condition.

if(condition2)

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

Statement 1:

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

Statement 2;

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

Statement 3;

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE


Example 1:

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

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE


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

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE


DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE
2] Develop a c program to check for the type of triangle.

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE


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

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE


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

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE


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

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE


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;

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE


Return statement:
If it is a void It return nothing but If it’s a int it will return 0.

DECISION MAKING AND BRANCHING STATEMENTS LAKSHMI S HANNE

You might also like