Using the For Statement to Add
Understanding the For Repetition
statement
Before we use the For Repetition
Statement to add numbers, we need to understand what the For Statement really
mean and does.
The for statement is part of the control
statement in C functions.
The for statement is required for
counter-controlled repetitions. C provides the For statement which specifies
the counter-controlled-repetition.
Counter-Controlled repetition is often
called definite repetition, because the number of repetition terminates when
the counter imputed is complete.
//How to Add with the For Selection statement
#include<stdio.h>
/*function main begins program execution */
int main(void)
{
/*Initialization phase */
int sum=0; /*Initializing total */
int x; /*no of grade to be entered */
/*Processing phase */
for(x=3;x<=1000; x+=3){
sum += x;
}
/*Terminating phase */
printf("sum is: %d", sum); /*displaying result */
return 0;
}
Explanation of the program
The first line (//How to Add with the For Selection Statement) in the solution is called a comment. insert comments to document programs to improve readability. the comment simply describes the purpose of the program.
The second line (#include<stdio.h>) is a directive to the C prepossesor. lines beginning with # are prepossessed by the prepossessor before being complied.
(Stdio.h means Standard input/Output Header) and its always included in every C programs.
(int main (void)) is a part of every C-programs the parentheses after main indicates that main is a program building block called a function
({ }) Begins the body of every function and closes the body of every function
(int sum, int x)- are definitions, the name sum, are names of variables. A variable is a location in memory where a value can be stored for use by a program.
A variable name in C is any valid identifier. every variable has a name, a type and a value.
(printf)-This instructs the computer to perform an action, the entire line, including printf, it's argument within the parentheses and the semicolon (;) is called a statement (;) - Statement terminator (%d)- format control string, indicates the type of data that would be oupted.
(return 0) indicates that program ended successfully.
Comments
Post a Comment
Please feel free to comments if you have any ideas, questions and subjection concerning our post and programs