Control Flow Statements.
In the C programming language, control flow statements are used to control the order in which statements are executed in a program. There are several control flow statements in C, including:
If-else statement: This statement is used to execute a block of code if a condition is true, and another block of code if the condition is false.
Switch statement: This statement allows the program to select one of several code blocks to execute based on the value of an expression.
While loop: This statement is used to execute a block of code repeatedly as long as a condition is true.
Do-while loop: This statement is similar to the while loop, but the block of code is executed at least once, even if the condition is false.
For loop: This statement is used to execute a block of code a specific number of times, based on a loop counter.
Break statement: This statement is used to terminate a loop or switch statement.
Continue statement: This statement is used to skip the current iteration of a loop and continue with the next iteration.
Goto statement: This statement is used to transfer control to a specific labeled statement in the program.
It's important to use these control flow statements effectively to write efficient and reliable programs.
Now let's Understand each clearly.
IF-ELSE :
The if-else statement is used in C programming to execute a block of code if a condition is true and another block of code if the condition is false.
The syntax of the if-else statement is as follows:
if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is false }
In this example, the program prompts the user to enter a number. Then, the program uses the if-else statement to check whether the number is even or odd. If the number is even, the program prints "num is even." If the number is odd, the program prints "num is odd."
Note that the condition in the if statement is num % 2 == 0, which checks whether the number is divisible by 2. If the condition is true, the program executes the block of code inside the if statement. If the condition is false, the program executes the block of code inside the else statement.
Switch-Case:
The switch statement in C is a control flow statement that allows the program to select one of several code blocks to execute based on the value of an expression. It provides an alternative way to write multiple if-else statements.
The syntax of the switch statement is as follows:
switch (expression) { case constant1: // code to be executed if expression == constant1 break; case constant2: // code to be executed if expression == constant2 break; ... default: // code to be executed if none of the above cases are true break; }
In this example, the program displays a menu of options and prompts the user to select an option. Then, the program uses the switch statement to execute the corresponding code block based on the user's choice. For example, if the user chooses "1", the program executes the code block inside the case 1 statement, which prints "You selected Add." If the user chooses an invalid option, the program executes the code block inside the default statement, which prints "Invalid choice."
Note that each case statement is followed by a break statement, which terminates the switch statement and prevents the program from executing the code in the other cases. If no case matches the value of the expression, the program executes the code in the default case.
While Loop:
while
loop in C is a control flow statement that allows a block of code to be executed repeatedly as long as a certain condition is met. Here's a basic example:1#include <stdio.h> 23int main() { 4 int count = 0; 56 while (count < 5) { 7 printf("Hello, World! %d\n", count); 89 // Increment the counter 10 count++; 11 } 1213 return 0; 14}
while
loop will execute as long as the count
variable is less than 5. Inside the loop, we print "Hello, World!" followed by the current value of count
, and then increment the count
variable by 1. Once count
reaches 5, the loop will exit, and the program will continue execution after the loop.Do While loop:
A do-while loop in C is a type of loop that executes a block of code repeatedly until a certain condition is met. The main difference between a do-while loop and other types of loops is that the block of code is executed at least once, regardless of whether the condition is true or false.
The syntax for a do-while loop in C is as follows:
arduinodo {
/* block of code to be executed */
} while (condition);
Here, the block of code is executed first, and then the condition is checked. If the condition is true, the block of code is executed again, and this continues until the condition becomes false.
Here is an example of a do-while loop that calculates the factorial of a number:
arduino#include <stdio.h>
int main()
{
int num, i = 1, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
do {
fact = fact * i;
i++;
}
while (i <= num);
printf("Factorial of %d is %d.", num, fact);
return 0;
}
In this example, the user enters a number, and the program calculates its factorial using a do-while loop. The loop starts with i = 1 and fact = 1, and calculates the factorial by multiplying fact with i for each iteration until i becomes greater than the entered number. Finally, the result is printed to the screen.
The flow chart for this example is shown below:
As you can see, the loop starts with the "do" block, which contains the code to be executed. After each iteration, the loop checks the condition, and if it is true, the loop continues. If the condition becomes false, the loop ends, and the program proceeds to the output and stop blocks.
For loop::
Here is what each part of the for loop syntax means:
initialization: This is the initial value of the variable used as a loop counter. This part of the loop is executed only once before the loop starts.
condition: This is a boolean expression that is checked at the beginning of each iteration of the loop. If the condition is true, the loop continues; otherwise, the loop exits.
increment/decrement: This part of the loop is executed at the end of each iteration. It is used to modify the value of the loop counter variable.
Now let's take a look at an example that demonstrates how to use a for loop in C to print the first 10 natural numbers:
#include <stdio.h> int main() { int i; for (i = 1; i <= 10; i++) { printf("%d ", i); } return 0; }
In this example, we have initialized a variable i
with the value 1
. The loop will execute as long as i
is less than or equal to 10
. After each iteration of the loop, i
is incremented by 1.
The output of the above program will be:
1 2 3 4 5 6 7 8 9 10
i
to 1
, then checks whether i
is less than or equal to 10
. If the condition is true, the loop executes the block of code inside the loop, which in this case is to print the value of i
. After that, the loop increments the value of i
by 1
and checks the condition again. This process repeats until i
is no longer less than or equal to 10
.
0 Comments