Control Flow/ Structure: Prepared By: Ms. Sa Sokngim
Control Flow/ Structure: Prepared By: Ms. Sa Sokngim
Control Flow/ Structure: Prepared By: Ms. Sa Sokngim
1. Decision Making
2. Loops
3. Break and Continue Statement
4. Switch… case Statement
5. goto and label Statement
1. Decision Making
if (testExpression)
{
// statements
}
Example: if statement
// Program to display a number if user enters negative number
// If user enters positive number, that number won't be displayed
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
return 0;
}
1.2 if...else statement
The if...else statement executes some code if the test expression
is true (nonzero) and some other code if the test expression is
false (0).
Syntax of if...else
if (testExpression) {
// codes inside the body of if
}else {
// codes inside the body of else
}
Example: if...else statement
// Program to check whether an integer entered by the user is odd or even
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
// True if remainder is 0
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
1.3 if...else if....else Statement
} else if(testExpression2) {
} else if (testExpression 3) {
} else {
}
Example: if...else if....else statement
// Program to relate two integers //checks if number1 is greater than
using =, > or < number2.
#include <stdio.h> else if (number1 > number2) {
int main(){ printf("Result: %d > %d",
int number1, number2; number1, number2);
}
printf("Enter two integers: ");
scanf("%d %d", &number1, // if both test expression is false
&number2); else {
//checks if two integers are printf("Result: %d < %d",
equal. number1, number2);
if(number1 == number2){ }
printf("Result: %d = %d“, return 0;
number1,number2); }
}
1.4 Nested if else statement
if(condition is true){
if(condition is true){
statement;
}else{
statement;
}
}else{
statement;
}
Example of Nested if else Statement
#include <stdio.h>
void main(){
char username;
int password;
printf("Username:");
scanf("%c",&username);
printf("Password:");
scanf("%d",&password);
if(username=='a'){
if(password==12345){
printf("Login successful");
}else{
printf("Password is incorrect, Try again.");
}
}else{
printf("Username is incorrect, Try again.");
}
}
2. Loops
Loops are used in programming to repeat a specific block until some end
condition is met.
o for loop
o while loop
o do...while loop
o Nested loops
2.1 for Loop
// codes
}
for loop Flowchart
Example: for loop
scanf("%d", &n);
2.2 while loop
while (testExpression)
{
//codes
}
Example: while loop
/ Program to find factorial of // loop terminates when number is less
a number than or equal to 0
// For a positive integer n, while (number > 0) {
factorial = 1*2*3...n // factorial = factorial*number;
factorial *= number;
#include <stdio.h> --number;
int main(){ }
int number; printf("Factorial= %lld", factorial);
statement(s);
}
2.4 Nested loops (Con)
while(condition) {
while(condition) {
statement(s);
}
statement(s);
}
2.4 Nested loops (Con)
do {
statement(s);
do {
statement(s);
}while( condition );
}while( condition );
Example of Nested Loops
#include <stdio.h>
int main()
{
int n, c, k;
printf("\n");
}
return 0;
}
3. Break And Continue Statement
break;
Flowchart Of Break Statement
How break statement works?
Example: break statement
// Program to calculate the sum // If user enters negative
of maximum of 10 numbers number, loop is terminated
// Calculates sum until user if(number < 0.0) {
enters positive number break;
# include <stdio.h> }
int main() { // sum = sum + number;
int i; sum += number;
continue;
Flowchart of Continue Statement
How Continue Statement Works?
Example: continue statement
The switch statement is often faster than nested if...else (not always).
Also, the syntax of switch statement is cleaner and easy to understand.
Syntax of switch...case
switch (n){
case constant1:
// code to be executed if n is equal to constant1;
break;
case constant2:
// code to be executed if n is equal to constant2;
break;
.
.
.
default:
// code to be executed if n doesn't match any constant
}
Switch Statement Flowchart
Example: switch Statement
// Program to create a simple calculator
// Performs addition, subtraction, multiplication or division
depending the input from user
# include <stdio.h>
int main() {
char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
switch(operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
break;
// operator is doesn't match any case constant (+, -, *, /)
default:
printf("Error! operator is not correct");
}
return 0; }
5. goto Statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
What is Label?
average=sum/(i-1);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);
return 0;
}