Introduction to condition and switch

  

example of how to use condition in c

  

Different example of condition in c programming

  

example of how to use switch in c programming

  

Introduction to Loop in c programming

  

Different question of using loop in c programming

  

Introduction to array in c programming

  

Different question and answer in array

  

Introduction to conditional structure loop


Conditional structures and loops are essential elements in C programming for controlling the flow of a program. There are mainly three types of loops used in C programming: `for`, `while`, and `do-while` loops. Here's an introduction to each of them:



1. For loop


For Loop:
- The `for` loop is one of the most commonly used loops in C.
- It provides a compact way to iterate over a range of values.
- The basic structure of a `for` loop is as follows:


For loop syntax



for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}



Example of for loop


#include <stdio.h>
int main(){
int num,i;
printf("Enter any nymber");
scanf("%d",&num);
for(i = 1;i<=num;i++){
printf("%d",i);
}
}



2. While loop


While Loop:
- The `while` loop is used to execute a block of code as long as a specified condition is true.
- It may not execute at all if the condition is false from the beginning.
- The basic structure of a `while` loop is as follows:

while loop syntax




while (condition) {
// Code to be executed repeatedly
}



Example of while loop


#include <stdio.h>
int main() {
int n;
printf("Enter a positive integer n: ");
scanf("%d", &n);
int i = 1;
while (i <= n) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}



3. Do While loop


Do-While Loop:
- The `do-while` loop is similar to the `while` loop, but it guarantees that the code block is executed at least once.
- It checks the condition after the execution of the block.
- The basic structure of a `do-while` loop is as follows:

For loop syntax




do {
// Code to be executed repeatedly
} while (condition);





Example of while loop


#include <stdio.h>
int main() {
int x = 1;
do {
printf("Value of x: %d\n", x);
x++;
} while (x <= 5);
printf("\n");
return 0;
}



Contact Us

Email: info@learn.co.rw
Phone: +250723709880
Whatsap: +250725527181

we will try to reply to your message as quickly as we can. please be aware that because of many messages we receive it take us same time to respond