C for loop

Created By: chatGPT

In C, the for loop is used for iterating over a sequence (like an array) or for repeating a block of code a certain number of times. Its syntax consists of three parts: initialization, condition, and increment/decrement. Here’s the basic structure:

for (initialization; condition; increment) {
    // code to be executed
}

Let's say we want to print numbers from 1 to 10. We can use a for loop like this:

#include <stdio.h>

int main() {
    for (int i = 1; i <= 10; i++) {
        printf("%d\n", i);
    }
    return 0;
}

In the above example, the loop initializes i to 1, checks the condition i <= 10, and increments i by 1 after each iteration. This continues until the condition is no longer true.

// Output:
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10

You can also use a for loop to iterate through an array. Here’s how:

#include <stdio.h>

int main() {
    int arr[] = {5, 10, 15, 20, 25};
    int length = sizeof(arr) / sizeof(arr[0]);

    for (int i = 0; i < length; i++) {
        printf("%d\n", arr[i]);
    }
    return 0;
}

In this example, we define an array called arr, calculate its length, and use the loop to access each element. The sizeof operator helps in determining how many elements are present in the array.

// Output:
// 5
// 10
// 15
// 20
// 25
Introduction And SetupVariablesData TypesIntFloatDoubleCharVoidUnsignedSignedConstantsEnumerationsArraysStringsStructsUnionsTypedefsPointersDynamic Memory AllocationMallocCallocReallocFreeFunctionsFunction DeclarationsFunction DefinitionsFunction CallsReturn StatementInline FunctionsRecursionHeader FilesPreprocessor DirectivesControl FlowIf StatementElse StatementElse If StatementSwitch StatementCase StatementDefault CaseLoopsFor LoopWhile LoopDo While LoopBreak StatementContinue StatementGoto StatementLabelsOperatorsArithmetic OperatorsRelational OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsConditional (ternary) OperatorComma OperatorSizeof OperatorData StructuresLinked ListsStacksQueuesTreesGraphsFunction PointersCallbacksMacrosCommentsSingle Line CommentsMulti Line CommentsSyntaxSyntax ErrorsCompilation ErrorsDebuggingStandard Input OutputPrintfScanfFile HandlingFopenFcloseFreadFwriteFprintfFgetsFputsError HandlingErrnoAssertionsExit FunctionExit CodesEvent HandlingSignal HandlingInterrupts