Decision making
statements:
Decision making statements:
1. if Statement
The if statement allows you to execute a block of code only when a specified condition is
true.
Syntax:
c
Copy code
if (condition) {
// Code to execute if condition is true
}
●
● Explanation:
○ The condition is any expression that returns a Boolean value (true or
false).
○ If the condition evaluates to true (non-zero), the code inside the braces
{} runs.
○ If the condition is false (zero), the program skips this block and continues
with the code below it.
Example:
c
#include <stdio.h>
int main() {
int age;
age=33;
if(age>=33)
{
printf("Your age is: %d", age);
}
else
printf("Your age is below 33");
return(0);
}
Output:
css
Copy code
Your age is: 33
2. if-else Statement
The if-else statement provides two possible paths. It executes one block of code if the
condition is true and another if it is false.
Syntax:
c
Copy code
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
●
● Explanation:
○ When the condition is true, the code inside the if block is executed.
○ If the condition is false, the code inside the else block is executed.
Example:
c
Copy code
int age = 16;
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
Output:
css
Copy code
You are not eligible to vote.
Here, the else block is executed because age is less than 18.
3. if-else-if Ladder
The if-else-if ladder allows multiple conditions to be evaluated in sequence. The
program checks each condition in order; as soon as one condition is true, the corresponding
block is executed, and the rest are skipped.
Syntax:
c
Copy code
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 conditions are true
}
●
● Explanation:
○ Each if or else if block is evaluated in order.
○ When a true condition is found, the corresponding block is executed, and the
ladder exits.
○ If no conditions are true, the else block (optional) runs.
Example:
c
Copy code
int score = 85;
if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
Output:
makefile
Copy code
Grade: B
In this case, the score is 85, so the else if (score >= 80) block runs.
4. switch Statement
The switch statement is used when there are multiple possible values for a single variable.
It provides a cleaner alternative to long if-else-if ladders.
Syntax:
c
Copy code
switch (expression) {
case constant1:
// Code for constant1
break;
case constant2:
// Code for constant2
break;
...
default:
// Code if none of the cases match
}
●
● Explanation:
○ The expression is evaluated once, and its value is compared with each
case constant.
○ When a match is found, the corresponding code block runs.
○ break; is used to exit the switch after the matching case to avoid "fall-
through" (execution of subsequent cases).
○ The default case (optional) runs if no cases match.
Example:
c
Copy code
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
}
Output:
mathematica
Copy code
Wednesday
In this example, day equals 3, so the case 3 is matched, printing "Wednesday."
5. Conditional (Ternary) Operator
The ternary operator ?: is a compact way of writing simple if-else statements. It's used to
evaluate a condition and assign a value based on the result.
Syntax:
c
Copy code
condition ? expression_if_true : expression_if_false;
●
● Explanation:
○ The condition is evaluated; if true, expression_if_true is
executed, otherwise expression_if_false.
○ It’s typically used for short, single-line conditions.
Example:
c
Copy code
int x = 10;
int y = (x > 5) ? 1 : 0;
printf("y = %d\n", y);
Output:
makefile
Copy code
y = 1
Here, x > 5 is true, so y is assigned the value 1.
These decision-making statements make it possible to write flexible, responsive programs that
behave differently depending on their inputs and conditions.
Iterative Statements
Iterative Statements
1. for Loop
○ The for loop is used when you know in advance how many times you want
to execute a statement or a block of statements.
Syntax:
c
Copy code
for (initialization; condition; update) {
// code block to be executed
}
○
○ Components:
■ Initialization: Sets the starting value of the loop control variable.
■ Condition: Evaluated before each iteration; if true, the loop
continues; if false, the loop ends.
■ Update: Modifies the loop control variable after each iteration.
Example:
c
Copy code
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
// Output: 0 1 2 3 4
○
2. while Loop
○ The while loop is used when the number of iterations is unknown and
depends on a condition that is evaluated at the start of each loop.
Syntax:
c
Copy code
while (condition) {
// code block to be executed
}
Example:
c
Copy code
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
// Output: 0 1 2 3 4
○
3. do-while Loop
○ Similar to the while loop, but the condition is evaluated after the code block
executes, guaranteeing that the code runs at least once.
Syntax:
c
Copy code
do {
// code block to be executed
} while (condition);
Example:
c
Copy code
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
// Output: 0 1 2 3 4
Jump Statements
1. break Statement
○ The break statement terminates the loop or switch statement immediately,
and the program control moves to the statement following the terminated
loop/switch.
Example in a for loop:
c
Copy code
for (int i = 0; i < 5; i++) {
if (i == 3) {
break;
}
printf("%d ", i);
}
// Output: 0 1 2
○
2. continue Statement
○ The continue statement skips the current iteration of a loop and proceeds
to the next iteration. It doesn’t terminate the loop but skips to the
update/condition check in the for and while loops.
Example in a for loop:
c
Copy code
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue;
}
printf("%d ", i);
}
// Output: 0 1 2 4
○
3. goto Statement
○ The goto statement transfers control to a labeled statement within the same
function. It's generally discouraged due to potential readability and
maintainability issues, but can be useful in certain cases, such as error
handling.
Syntax:
c
Copy code
goto label;
// code to be skipped
label:
// code where control jumps
Example:
c
Copy code
int i = 0;
if (i == 0) {
goto skip;
}
printf("This won't print.");
skip:
printf("Jumped here directly.");
// Output: Jumped here directly.
Each of these iterative and jump statements helps in managing progra
Tab 3
Iterative Statements
These statements enable the repetition of a set of instructions multiple times until a specified
condition is met.
1. for Loop
● Purpose: Ideal for situations where the number of iterations is known beforehand.
Syntax:
c
Copy code
for (initialization; condition; increment/decrement) {
// statements
}
●
● Components:
○ Initialization: Sets an initial value for the loop control variable (e.g., int i
= 0).
○ Condition: Evaluated before each iteration. If true, the loop body executes;
if false, the loop exits.
○ Increment/Decrement: Alters the loop control variable (e.g., i++) after
each iteration.
Example:
c
Copy code
for (int i = 0; i < 5; i++) {
printf("%d ", i); // Output: 0 1 2 3 4
}
●
● Explanation: In this example, the loop starts with i = 0. As long as i is less than
5, it executes the printf statement, then increments i by 1. When i reaches 5, the
loop terminates.
2. while Loop
● Purpose: Useful when the number of iterations is unknown or depends on user input.
Syntax:
c
Copy code
while (condition) {
// statements
}
●
● Components:
○ Condition: Checked at the beginning of each iteration. If true, the loop
body executes; if false, the loop exits.
Example:
c
Copy code
int i = 0;
while (i < 5) {
printf("%d ", i); // Output: 0 1 2 3 4
i++;
}
●
● Explanation: Here, the loop begins with i = 0. It prints i and increments it by 1 in
each iteration until i is no longer less than 5.
3. do-while Loop
● Purpose: Ensures the loop executes at least once, as the condition is evaluated after
the loop body.
Syntax:
c
Copy code
do {
// statements
} while (condition);
●
● Components:
○ Loop Body: Executes once unconditionally, then repeats as long as the
condition remains true.
Example:
c
Copy code
int i = 0;
do {
printf("%d ", i); // Output: 0 1 2 3 4
i++;
} while (i < 5);
●
● Explanation: In this loop, printf executes at least once because the condition is
evaluated after the loop body. The loop continues until i is no longer less than 5.
Jump Statements
Jump statements control the flow by enabling jumps within code blocks, allowing certain
instructions to be skipped or loops to exit early.
1. break Statement
● Purpose: Used to exit from a loop or switch statement prematurely.
Syntax:
c
Copy code
break;
Example:
c
Copy code
for (int i = 0; i < 10; i++) {
if (i == 5) break;
printf("%d ", i); // Output: 0 1 2 3 4
}
●
● Explanation: Here, the for loop starts with i = 0. When i equals 5, the break
statement is triggered, exiting the loop immediately, so 5 6 7 8 9 are not printed.
2. continue Statement
● Purpose: Skips the current iteration of the loop and moves to the next iteration.
Syntax:
c
Copy code
continue;
Example:
c
Copy code
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
printf("%d ", i); // Output: 0 1 3 4
}
●
● Explanation: In this example, when i equals 2, the continue statement skips the
printf statement for that iteration and moves to the next iteration, so 2 is not
printed.
3. goto Statement
● Purpose: Transfers control to a labeled statement elsewhere in the code, which can
make code flow harder to follow and should be used sparingly.
Syntax:
c
Copy code
goto label;
...
label:
// statements
Example:
c
Copy code
int i = 0;
start:
if (i < 5) {
printf("%d ", i); // Output: 0 1 2 3 4
i++;
goto start;
}
●
● Explanation: Here, the goto statement sends control back to the start label,
causing the loop to repeat until i is no longer less than 5. This example mimics a
loop, but using goto in this way is generally discouraged due to readability
concerns.
Summary of Usage Tips
● for: Best when you know the exact number of iterations.
● while: Preferred for loops with uncertain iterations.
● do-while: Ensures at least one iteration.
● break and continue: Useful for early exit and iteration skipping, respectively.
● goto: Use with caution; generally avoided due to readability issues.