Hello Everyone, Welcome back to Rajasthan Polytechnic BTER. This is Garima Kanwar Welcome you all to Rajasthan Polytechnic BTER at where you will get notes of rajasthan polytechnic 1st semester, acnd cs branch 3rd and 5th semester.
Control statements are the backbone of any programming language, allowing the flow of the program to be directed based on certain conditions and repeated execution of code segments. This chapter covers two primary types of control statements: Conditional Statements and Repeat Statements.
3.1 Conditional Statements
Conditional statements are used to perform different actions based on different conditions. They enable decision-making in a program, allowing certain blocks of code to be executed only if specific conditions are met.
Types of Conditional Statements:
if Statement: Executes a block of code if a specified condition is true.
if (condition) {// code to be executed if the condition is true }
Example:
if (age >= 18) { printf("You are eligible to vote."); }
if-else Statement: Executes one block of code if a condition is true, and another block if it is false.
if (condition) { // code to be executed if the condition is true } else { // code to be executed if the condition is false }
Example:
if (marks >= 40) { printf("You passed the exam."); } else { printf("You failed the exam."); }
if-else if-else Ladder: Allows multiple conditions to be checked sequentially. The first true condition will execute its corresponding block.
if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition2 is true } else { // code to be executed if none of the conditions are true }
Example:
if (marks >= 90) { printf("Grade: A"); } else if (marks >= 75) { printf("Grade: B"); } else if (marks >= 60) { printf("Grade: C"); } else { printf("Grade: D"); }
switch Statement: Evaluates a variable or expression and matches its value against a series of case labels. It is a cleaner alternative to multiple if-else statements.
switch (expression) { case value1: // code to be executed if expression == value1 break; case value2: // code to be executed if expression == value2 break; default: // code to be executed if no cases match }
Example:
switch (day) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; default: printf("Invalid day"); }
3.2 Repeat Statements
Repeat statements, also known as loops, allow a block of code to be executed repeatedly based on a condition. This section covers basic loops and nested loops.
3.2.1 Loops
Loops are used to execute a set of instructions multiple times until a specified condition is met.
Types of Loops:
for Loop: Used when the number of iterations is known.
for (initialization; condition; update) { // code to be executed }
Example:
for (int i = 0; i < 5; i++) { printf("Hello, World!\n"); }
while Loop: Executes a block of code as long as the condition is true. It is useful when the number of iterations is not known in advance.
while (condition) { // code to be executed }
Example:
int i = 0; while (i < 5) { printf("Hello, World!\n"); i++; }
do-while Loop: Similar to the while loop, but the code block is executed at least once before the condition is checked.
do { // code to be executed } while (condition);
Example:
int i = 0; do { printf("Hello, World!\n"); i++; } while (i < 5);
3.2.2 Nested Loops
Nested loops are loops within loops. They are used to handle more complex scenarios, such as multidimensional arrays or pattern printing.
Example of Nested Loops:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
printf("i = %d, j = %d\n", i, j);
}
}
Output:
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
i = 3, j = 1
i = 3, j = 2
In the above example, the inner loop runs completely for every iteration of the outer loop.
Applications of Nested Loops:
- Printing patterns (e.g., stars, numbers).
- Traversing multi-dimensional arrays.
- Handling complex data structures.
This blog post is written by Garima Kanwar, and the blog name is 'Rajasthan Polytechnic BTER.' We hope this post has provided you with a clear understanding of control statements in programming. For more such detailed notes, please visit 'Rajasthan Polytechnic BTER' regularly.
If you want any other subject notes or any other study material please let me know in Comment Section.
0 Comments