0% found this document useful (0 votes)
17 views19 pages

Lecture SP08 Decision Making and Branching 1

This document is a lecture on decision making and branching in C programming, covering various control structures such as selection statements (if, if-else, nested if, else-if ladder, switch) and jump statements (break, continue, goto, return). It explains the syntax and usage of these statements with examples and flowcharts. The content is prepared by Md. Mijanur Rahman from the Dept. of Computer Science and Engineering at Jatiya Kabi Kazi Nazrul Islam University, Bangladesh.

Uploaded by

Kishor Dongare
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)
17 views19 pages

Lecture SP08 Decision Making and Branching 1

This document is a lecture on decision making and branching in C programming, covering various control structures such as selection statements (if, if-else, nested if, else-if ladder, switch) and jump statements (break, continue, goto, return). It explains the syntax and usage of these statements with examples and flowcharts. The content is prepared by Md. Mijanur Rahman from the Dept. of Computer Science and Engineering at Jatiya Kabi Kazi Nazrul Islam University, Bangladesh.

Uploaded by

Kishor Dongare
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/ 19

CSE 06131223  CSE 06131224

Structured Programming
Lecture 8
Decision Making and Branching in C (1)

Prepared by________________________________
Md. Mijanur Rahman, Prof. Dr.
Dept. of Computer Science and Engineering
Jatiya Kabi Kazi Nazrul Islam University, Bangladesh
www.mijanrahman.com
Contents
DECISION MAKING AND BRANCHING IN C
• Conditional Control Structures
• Selection Statements
• if statement
• if..else statements
• nested if statements
• if-else-if ladder
• switch statements
• Jump Statements:
• break
• continue
• goto
• return

Decision Making and Branching in C 2


Conditional Control Structures
• Control Structures are statements that change the flow of a program to a different
code segment based on certain conditions.
• The control structures are categorized into three major Conditional types; they are:
1. Decision making and branching statements
a) Selection statements
b) Jump statements
2. Decision making and looping (Iteration)

Decision Making and Branching in C 3


Conditional Control Structures
• Conditional Control Structures statements in C:
1. Selection Statements: 3. Jump Statements:
• If statement • return
• If Else Statement • goto
• Else If statement • exit()
• Nested If statement • break
• Switch statement • continue
2. Iteration Statements:
• For loop
• While loop
• do while loop

Decision Making and Branching in C 4


Selection Statements in C

Decision Making and Branching in C 5


If statement in C
• if statement is the most simple decision-making statement. It is used to decide whether a
certain statement or block of statements will be executed or not; i.e., if a certain condition is
true then a block of statement is executed otherwise not.

• Here, the condition after evaluation will be either true or false. C if statement accepts boolean
values – if the value is true then it will execute the block of statements below it otherwise not.
If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default if statement
will consider the first immediately below statement to be inside its block.

Decision Making and Branching in C 6


If Statement in C
• Example:

Flowchart of IF statement
Decision Making and Branching in C 7
If Statement in C
• Example:
1. // C program to illustrate If statement
2. #include <stdio.h>
3.
4. int main() {
5. int i = 10;
6.
7. if (i > 15)
8. {
9. printf("10 is less than 15");
10. }
11.
12. printf("I am Not in if");
13. }

Decision Making and Branching in C 8


If-else Statement in C
• The if statement alone tells us that if a condition is true it will execute a block of statements and if the
condition is false it won’t. But what if we want to do something else if the condition is false. Here comes
the C else statement.
• We can use the else statement with if statement to execute a block of code when the condition is false.
Syntax:

Decision Making and Branching in C 9


If-else Statement in C
• Flowchart of IF-ELSE statement:

Decision Making and Branching in C 10


If-else Statement in C
• Example:
1. // C program to illustrate If statement
2. #include <stdio.h>
3.
4. int main() {
5. int i = 20;
6.
7. if (i < 15){
8.
9. printf("i is smaller than 15");
10. }
11. else{
12.
13. printf("i is greater than 15");
14. }
15. return 0;
16. }

Decision Making and Branching in C 11


Nested If-else statement in C
• A nested if in C is an if statement that is the target of another if statement. Nested if
statements mean an if statement inside another if statement. Yes, both C and C++
allow us to nested if statements within if statements, i.e., we can place an if statement
inside another if statement.
• Syntax:

Decision Making and Branching in C 12


Nested If-else statement in C
• Flowchart of Nested IF-ELSE statement:

Decision Making and Branching in C 13


Nested If-else statement in C
• Example:
1. // C program to illustrate nested-if statement
2. #include <stdio.h>
3.
4. int main() {
5. int i = 10;
6.
7. if (i == 10)
8. {
9. // First if statement
10. if (i < 15)
11. printf("i is smaller than 15\n");
12.
13. // Nested - if statement
14. // Will only be executed if statement above
15. // is true
16. if (i < 12)
17. printf("i is smaller than 12 too\n");
18. else
19. printf("i is greater than 15");
20. }
21.
22. return 0;
23. }
Decision Making and Branching in C 14
Nested If-else statement in C
• Example: Find the Largest Number Among Three Numbers

Decision Making and Branching in C 15


Else-If Ladder Statement
• The else if statement is an extension of the "if else" conditional branching statement.
When the expression in the "if" condition is "false" another "if else" construct is used
to execute a set statements based on an expression.
• This control structure statement also known as else if ladder statement.
• Syntax:

Decision Making and Branching in C 16


Else-If Ladder Statement
• Flowchart:

Decision Making and Branching in C 17


1. int main()
2. {
3. int mark;
4. printf("Enter mark: ");
5. scanf(“%d”, &mark);
Else-If Ladder Statement 6. if (mark >=60 )
7. {
• Example: 8. printf("First Division\n");
9. }
10. else if (mark >=45)
11. {
12. printf("Second Division\n");
13. }
14. else if (mark >=33 )
15. {
Output: 16. printf(“Third Division\n");
17. }
Enter mark:65 18. else
First Division 19. {
20. printf(“Fail\n");
21. }
22. return 0;
23. }

Decision Making and Branching in C 18


? THE END
Decision Making and Branching in C 19

You might also like