C Programming - Control Flow
C Programming - Control Flow
CopyEdit
void greet();
// Function definition
void greet() {
printf("Welcome to C programming!\n");
d) Comments
3. C Data Types
a) Conditional Statements
CopyEdit
printf("Adult\n");
} else {
printf("Minor\n");
}
b) Loops
CopyEdit
// for loop
// while loop
int count = 0;
count++;
5. Functions in C
CopyEdit
return a + b;
int main() {
return 0;
}
6. Input and Output Functions
CopyEdit
// Input
int age;
scanf("%d", &age);
// Output
7. Operators in C
Arithmetic: +, -, *, /, %
Increment/Decrement: ++, --
CopyEdit
CopyEdit
int x = 10;
CopyEdit
fclose(file);
3. main() function
4. User-defined functions
4o
You said:
Control Flow in C programming
ChatGPT said:
ChatGPT
Control flow in C programming refers to the order in which individual statements, instructions, or
function calls are executed or evaluated. C provides several control flow constructs to manage the
execution of code based on conditions, loops, and branching.
Conditional statements allow the program to make decisions based on specific conditions.
a) if Statement
CopyEdit
if (condition) {
Example:
CopyEdit
b) if-else Statement
Executes one block of code if the condition is true, and another block if it's false.
CopyEdit
if (condition) {
// Code to execute if condition is true
} else {
Example:
CopyEdit
} else {
c) else if Ladder
CopyEdit
if (condition1) {
} else if (condition2) {
} else {
Example:
CopyEdit
printf("Grade: A\n");
printf("Grade: B\n");
printf("Grade: C\n");
} else {
printf("Fail\n");
d) switch Statement
CopyEdit
switch (expression) {
case value1:
break;
case value2:
break;
default:
Example:
CopyEdit
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
a) for Loop
CopyEdit
// Code to execute
Example:
CopyEdit
Output: 1 2 3 4 5
b) while Loop
CopyEdit
while (condition) {
// Code to execute
Example:
CopyEdit
int i = 1;
while (i <= 5) {
i++;
Output: 1 2 3 4 5
c) do-while Loop
CopyEdit
do {
// Code to execute
} while (condition);
Example:
CopyEdit
int i = 1;
do {
i++;
Output: 1 2 3 4 5
a) break Statement
Example:
CopyEdit
if (i == 3) {
break;
Output: 1 2
b) continue Statement
Skips the current iteration and continues with the next iteration of the loop.
Example:
CopyEdit
if (i == 3) {
continue;
}
Output: 1 2 4 5
Example:
CopyEdit
int i = 1;
start:
i++;
if (i <= 5) {
goto start;
Output: 1 2 3 4 5
CopyEdit
if (i % 2 == 0) {
} else {
Output:
csharp
CopyEdit
1 is odd
2 is even
3 is odd
if-else Executes one block if true, another if false if (x > 0) else { ... }