0% found this document useful (0 votes)
2 views10 pages

1 c Programming Unit 3

The document explains decision making in programming, emphasizing its importance for enabling conditional logic and dynamic behavior in programs. It details various branching statements in C, such as if, if-else, nested if, else if ladder, switch, and goto, along with their syntax and use cases. Additionally, it introduces looping concepts, types of loops (while, do-while, for), and control statements (break, continue) to automate repetitive tasks.

Uploaded by

jangraanurudh
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 views10 pages

1 c Programming Unit 3

The document explains decision making in programming, emphasizing its importance for enabling conditional logic and dynamic behavior in programs. It details various branching statements in C, such as if, if-else, nested if, else if ladder, switch, and goto, along with their syntax and use cases. Additionally, it introduces looping concepts, types of loops (while, do-while, for), and control statements (break, continue) to automate repetitive tasks.

Uploaded by

jangraanurudh
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/ 10

UNIT – 3

1. What is Decision Making in Programming?

Definition:
Decision making allows a program to choose different paths of execution based on the
outcome of a condition or expression. It enables dynamic behavior where the program does
not always follow the same path, but instead can make decisions based on input, logic, or
data.

Why is it important?
• Allows conditional logic (e.g., if user is above 18, allow voting)
• Makes programs smarter, interactive, and flexible
• Allows validation, branching, switching options, etc.

2. Branching Statements in C
C provides the following decision-making statements:

Statement Purpose

if Executes a block only if condition is true

if...else Executes one block if true, another if false

else if ladder Tests multiple conditions

nested if Multiple if inside another if

switch Select one case from many

goto Jumps to a labeled part of the code

2.1 IF Statement

Definition:
An if statement is used to check a condition. If the condition is true, the code inside it is
executed.

Syntax:
c
CopyEdit
if (condition) {
// code to execute if condition is true
}

Example:
c
CopyEdit
int age = 20;
if (age >= 18) {
printf("You are eligible to vote.");
}

Use Case:
• Checking age, login status, balance > 0, etc.

2.2 IF-ELSE Statement

Definition:
Used when you want to perform one task if condition is true, and another if false.

Syntax:
c
CopyEdit
if (condition) {
// true block
} else {
// false block
}

Example:
c
CopyEdit
int marks = 70;
if (marks >= 40) {
printf("Pass");
} else {
printf("Fail");
}

Use Case:
• Result, login valid/invalid, yes/no options.

2.3 Nested IF Statement

Definition:
An if-statement inside another if-statement. Useful for hierarchical conditions.

Syntax:
c
CopyEdit
if (condition1) {
if (condition2) {
// code
}
}

Example:
c
CopyEdit
if (marks >= 60) {
if (marks >= 90) {
printf("Grade A");
} else {
printf("Grade B");
}
}

2.4 ELSE IF Ladder

Definition:
Used to test multiple conditions in sequence. The first true condition executes, others are
skipped.

Syntax:
c
CopyEdit
if (condition1) {
// code1
} else if (condition2) {
// code2
} else {
// default code
}

Example:
c
CopyEdit
int marks = 85;
if (marks >= 90) printf("Grade A+");
else if (marks >= 80) printf("Grade A");
else if (marks >= 70) printf("Grade B");
else printf("Fail");

2.5 SWITCH Statement

Definition:
Efficient way to select one option among many based on a single variable.

Syntax:
c
CopyEdit
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// default code
}

Example:
c
CopyEdit
int choice = 2;
switch (choice) {
case 1: printf("Start"); break;
case 2: printf("Pause"); break;
case 3: printf("Stop"); break;
default: printf("Invalid Choice");
}

2.6 GOTO Statement

Definition:
goto is used to jump directly to another part of the program.

Note: Use of goto is discouraged as it makes code hard to follow.

Syntax:
c
CopyEdit
goto label;
// ...code...
label:
// code here

Example:
c
CopyEdit
int a = 1;
if (a < 2)
goto skip;
printf("This will be skipped.");
skip:
printf("You jumped here!");

PART 2: Decision Making and Looping

3. Introduction to Looping in C

Definition:
Looping means repeating a set of instructions until a condition becomes false. It helps in
automating repetitive tasks like printing 1 to 100, calculating sum, generating tables, etc.

4. Types of Loops in C

Loop Type Condition Minimum Execution Best Use

while pre-check 0 or more unknown repeats

do-while post-check 1 or more menus/input

for pre-check 0 or more fixed repeat

4.1 WHILE Loop


Definition:
while loop checks the condition before running. Used when you don't know in advance how
many times loop will run.

Syntax:
c
CopyEdit
while (condition) {
// code
}

Example:
c
CopyEdit
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}

4.2 DO-WHILE Loop

Definition:
do-while executes at least once. Condition is checked after running the loop.

Syntax:
c
CopyEdit
do {
// code
} while (condition);

Example:
c
CopyEdit
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);

4.3 FOR Loop

Definition:
for is used when number of iterations is predefined or known.

Syntax:
c
CopyEdit
for (initialization; condition; update) {
// code
}

Example:
c
CopyEdit
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}

4.4 BREAK Statement

Definition:
Breaks out of a loop/switch prematurely.

Example:
c
CopyEdit
for (int i = 1; i <= 10; i++) {
if (i == 5) break;
printf("%d ", i);
}

4.5 CONTINUE Statement

Definition:
Skips current iteration and jumps to the next.

Example:
c
CopyEdit
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
printf("%d ", i);
}

4.6 NESTED Loops

Definition:
Loop inside another loop, used for patterns, matrices, grids.

Example:
c
CopyEdit
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
printf("(%d,%d) ", i, j);
}
}

Summary
• IF, IF-ELSE: One-time decisions
• SWITCH: Fixed multiple options
• WHILE: Run till unknown condition
• DO-WHILE: At least one time run
• FOR: Known times repetition
• BREAK: Early exit
• CONTINUE: Skip iteration

For more information: Contact us.

You might also like