Basic programming course: Lesson #5 Control structures. Part 2. Cycles.
Today I will write @alejos7ven's homework in Week 05 of Teaching Engagement Challenge Session 20. It was very important and instructive. The topic is Basic programming course: Lesson #5 Control structures. Part 2. Cycles. I hope everyone likes my homework.
Design By Canva
The program is repeated a certain number of times according to the performance condition called a cycle or loop. We see Endless, Finite, and Nested loops in programs. A program consists of two or more statements. A statement consists of two parts: the loop body and the test condition.
Cycles are generally of three types.
Namely:
- While
- do...while
- for
While Loop or Statement:
A while loop or statement is used to execute two or more loops or statements according to a condition. In a while loop, the condition is checked first, and if true, it starts executing. A while loop is mainly used as an alternative to a for loop. I have given the format and example of a while loop below.
Do....while loop:
The do...while statement is used to execute one or more statements conditionally. First, the function starts if the condition is true. If the condition is true, then the task resumes. The function continues until the condition is false. The do...while loop terminates when the condition is false. I have given the format and example of the do... while loop below.
For loop:
A for loop is used in computer programming to perform operations through two or more loops. The number of loop iterations is calculated using any variable. I have given the format and example of a for loop below.
For Loop Example:
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=6; i++)
printf("C programming\n");
return 0;
}
In the for loop example, conditionally, the value of i will first perform an action. Then, it will increment by one and output the C programming text six times according to the condition. The image shows the program's output.
While Loop Example:
#include<stdio.h>
main()
{
int a=1;
while(1)
{
printf("\n%d",a);
a++;
}
}
I used the while loop in the program to output an infinite number. First, the condition is checked by one and then incremented one by one, which goes on to infinity. I didn't give the while loop the condition to work up to a certain number.
do...while loop Example:
#include<stdio.h>
int main()
{
int x;
x=0;
do
{
printf("\n%d",x);
x=x+1;
}
while(x<6);
}
I have conditioned the do...while program to display numbers from zero to five. First, it will count zero according to the condition, then the value of x will increase by one according to the condition, and it will stop if it is six.
While loop | do...while loop |
---|---|
In the while loop, the condition will be checked first, then the loop will work. | In the do...while loop, the loop will execute once and then check the condition. |
The loop does not work if the condition is false because the condition is checked first in the while loop. | In the do...while loop, the loop will first run once and then stop if the condition is false. |
Switch case Program:
#include<conio.h>
#include<stdio.h>
void main()
{
int choice;
printf("press 1 for January\n");
printf("press 2 for February\n");
printf("press 3 for March\n");
printf("press 4 for April\n");
printf("press 5 for May\n");
printf("Enter your choice\n");
scanf("%d",& choice);
switch(choice)
{
case 1:
printf("January");
break;
case 2:
printf("February");
break;
case 3:
printf("March");
break;
case 4:
printf("April");
break;
case 5:
printf("May");
break;
defult:
printf("Invalid Input");
}
}
The above computer program is an example of a switch case program. I wrote the program by naming five months out of twelve. I first declared the variable. Inputting one will output January, inputting two will output February, inputting three will output March, inputting four will output April, and inputting five will output May.
Then I used a switch case loop: January for case 1, February for case 2, and May for case 5 will act as a month loop. When I enter six from the keyboard, it shows Invalid Input because I used up to five as a switch case loop.
The code mentioned is a program made with a switch case loop. I am describing the program in several parts.
Variables:
The program has declared integer-type variables op,n, ns, and a. Then, a logical variable called exit is declared.
Initialization:
The initialization part of the program has exit=false: and a = 0:
Repeat Structure:
Here, if I enter one, the sum numbers will work according to the condition. If I enter two, the show result will work. If I enter three, the End program will work. It will execute every time the condition is true, and case 1, case 2, and case 3 will be displayed.
If the condition is not true, then the Invalid option will be displayed by default structure.
#include < iostream >
using namespace std;
int main()
{
int num;
int sum=0;
int counter=1;
do
{
cout<<"Enter a number greater than 10: ";
cin>>num;
}
while(num<=10);
while(counter<=num)
{
sum=sum+counter;
counter=counter+1;
}
cout<< "The sum of numbers from 1 to " << num << "is:" <<sum << endl;
return 0;
}
I have programmed to add numbers from 1 to 15 using a do...while loop. According to the homework condition, entering the big number of ten from the keyboard will work. I first declared the integer type variable. Then, according to the condition, sum=0 will start counting, and the given condition will count the largest number of ten.
Then, the while (num = 10) and while(counter = num) conditions will work to count from the ten-number condition. After I type 9 in the output, the condition is mentioned. But when I typed 15, I got output 120.
SL No. | My Invited Steemit Friends |
---|---|
1 | @memamun |
2 | @shahariar1 |
3 | @max-pro |
This is my Twitter share link :
https://twitter.com/mahadih83660186/status/1844194872556917174?t=R95tUVkJaPo6qC4IQGHyGQ&s=19
Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.
Thank You Dear Sir....