Ics 2102 Control Structures
Ics 2102 Control Structures
TO COMPUTER
PROGRAMMING.
CONTROL STRUCTURES
Control structures define the flow of execution in a program based on specified conditions.
They enable decision-making and repetition, crucial for creating efficient and dynamic programs.
Control structures include selection statements and iteration statements.
They are case sensitive. Eg (if) is in lowercase, writing it in uppercase will give an error.
Selection Control Structures:
The if Statement:
if (condition) {
// code block to execute if condition is true
}
Example:
Int x = 9;
if (x > 0) {
printf("x is positive");
}//output is x is positive
The else statement:
Used to specify a block of code to be executed if the condition is false.
If(condition){
//block of code to be executed if condition is true
} else{
//block of code to be executed if the condition is false
}
Example:
Int x = -2;
if (x > 0) {
printf("x is positive");
} else {
printf("x is non-positive");
} //output will be x is non-positive
The else if statement.
Use this when you have to test more than one conditions.
Syntax.
If(condition1){
//code to be executed if condition1 is true
}else if (conditon2){
//Code to be executed if condition1 is false and condition2 is true
}else{
//code to be executed if both condition1 and condition2 are false
}
Example to find out if a number is positive or negative;
int myNum = 10; //is this a positive or negative number?
if(myNum > 0){
printf(“The value is a positive number.”);
} else if (myNum <0){
printf(“The value is a negative number”);
}else{
printf(“The value is 0”);
}//output will be The value is a positive number
Short hand if…else(ternary operator)
Ternary operator consists of three operands. It can be used to replace multiple lines of code with a single line. It is
often used to replace simple if else statements.
Syntax is written like this:
Variable = (condition)? expressionTrue : expressionFalse
int time =20;
(time >18)? Printf(“Good day.”): printf(“Good evening.”);
Instead of :
Int time = 20;
if(time >18){
printf(“Good day.”);
}else{
printf(“Good evening.”);
}// output will be Good day.
Switch statement.
Instead of writing too many if…else statements, you can use switch statement.
The switch statement selects one of many code blocks to be executed.
Switch(expression){
Case x:
//code block
break;
Case y:
//code block
break;
Default:
//code
}
Switch example;
#include<stdio.h>
int main(){
int day = 4;
switch(day){
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
Switch example; continued
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
Switch example; continued
case 7:
printf("Sunday");
break;
default:
printf("Weekend");
}
return 0;
}
//output will be Thurdsday
Example:
The switch expression is evaluated once.
The value of the expression is compared with the values of each case
If there is a match, the associated block of code is executed.
The break statement breaks out of the switch block and stops the execution.
The default statement is optional, and specifies some code to run if there is no case match.
THE BREAK KEYWORD.
A break can save a lot of execution time because it ignores the execution of all the rest of the code in the switch
block.
When a match is found, the job is done. There is no need for more testing.
The default keyword
When you know exactly how many times you want to loop through a block of code, use the for loop
instead of a while loop.
Syntax:
for (initialization; condition; increment/decrement) {
// code block to execute repeatedly until the condition is false
}
Statement 1:is executed one time, before the execution of the code block.
Statement 2:defines the condition for executing the code block
Statement 3: executed every time after the code block has bee executed.
Example:
for (int i = 0; i < 5; i++) {
printf("%d ", i);
Nested loops.
It is possible to place a loop inside another loop. This is called a nested loop.
The inner loop will be executed one time, for each iteration of the outer loop.
Int i,j;
//outer loop
For(i=1;i<=2;i++){
Printf(“outer: %d\n”,i); //executes 2 times
//inner loop
For(j=1;j<=3;j++){
Printf(“Inner: %d\n”,j); //executes 6 times
}
}
Break keywords.
This statement breaks one iteration in the loop, if a specified condition occurs, and continues with the next
iteration in the loop.
#include<stdio.h>
int main(){
int i;
for(i=1; i<10; i++){
if(i == 6){
continue;
}
printf("%d\n",i);
}
}// prints 1 2 3 4 5 7 8 9 in different lines.