Control Statement
Control statements control the flow of execution of a program. This statements help to make decisions, repeat tasks or jump to specific part of code.
C Suppots This Types Of Statements
Section titled “C Suppots This Types Of Statements”if Statement
Section titled “if Statement”Conditional statement to execute a block of code if a condition is true.
if (condition) {// code}
if-else if Statement in C
Section titled “if-else if Statement in C”It allows us to execute a block of code among multiple conditions. It evaluates conditions in sequence and executes the code block associated with the first true condition.
if (number > 0) { // block1} else if (number < 0) { // block2} else { // default block}
if-else Statement
Section titled “if-else Statement”Extends if statement to execute one block if condition is true and another if false.
if (condition) {// true block} else {// false block}
Switch Statement
Section titled “Switch Statement”Allows multi-way branching.
switch(expression) {case value1: // code break;case value2: // code break;default: // code}
Code Example
Section titled “Code Example”#include <stdio.h>
int main() { int number = 0;
if (number > 0) { printf("The number is positive.\n"); } else if (number < 0) { printf("The number is negative.\n"); } else { printf("The number is zero.\n"); }
int choice = 2;
switch (choice) { case 1: printf("You selected option 1.\n"); break; case 2: printf("You selected option 2.\n"); break; case 3: printf("You selected option 3.\n"); break; default: printf("Invalid option.\n"); }
return 0;}
The number is zero.You selected option 2.