CSE - 109 Lec 67 Flow Control Statement
CSE - 109 Lec 67 Flow Control Statement
PREPARED BY
SHAHRIAR RAHMAN KHAN
Lecturer, Department of CSE, MIST
INTRODUCTION
Take a user input a scanf(“%d”, &a);
Print Hello World in the screen if a < 5? You are stuck now!
Single If in C
The if statement is one of C's selection statements (sometimes called conditional statements).
Its operation is governed by the outcome of a conditional test that evaluates to either true or
false.
In its simplest form, the if statement allows your program in conditionally execute a statement.
Syntax: if(expression) statement;
If the expression evaluates as true, the statement will be executed. If it does not the statement is
bypassed, and the line of code following the if is executed.
if (expression)
{
statement;
…………
statement;
}
Single if Syntax
Understanding If Statement
Sample Code
1 #include <stdio.h>
2 int main(){
3 int a = 1;
4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }
8 printf(“Good Bye!”);
9 }
Sample Code
1 #include <stdio.h>
2 int main(){
3 int a = 1; a = 1 < 5? YES
4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }
8 printf(“Good Bye!”);
9 }
Sample Code
1 #include <stdio.h>
2 int main(){
Hello World
3 int a = 1; a = 1 < 5?
4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }
8 printf(“Good Bye!”);
9 }
Sample Code
1 #include <stdio.h>
2 int main(){
Hello World
3 int a = 1; a = 1
4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }
8 printf(“Good Bye!”);
9 }
Sample Code
1 #include <stdio.h>
2 int main(){
Hello World
3 int a = 1; a = 2 Good Bye!
4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }
8 printf(“Good Bye!”);
9 }
Understanding Invalid If Statement
Sample Code
1 #include <stdio.h>
2 int main(){
3 int a = 6;
4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }
8 printf(“Good Bye!”);
9 }
Sample Code
1 #include <stdio.h>
2 int main(){
3 int a = 6; a = 6 < 5? No
4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }
8 printf(“Good Bye!”);
9 }
Sample Code
1 #include <stdio.h>
2 int main(){
Good Bye!
3 int a = 6; a = 6 Process returned 0 (0x0) execution time : 0.075 s
Press any key to continue.
4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }
8 printf(“Good Bye!”);
9 }
If Example
Write a C program to check whether the summation of two numbers are 24 or
not?
#include <stdio.h>
int main(){
int a, b, answer;
printf(“Is the summation 24?");
scanf("%d %d", &a, &b);
answer = a + b;
if (answer == 24) printf("Right!");
return 0;
}
If-else in C
You can add an else statement to the if. When this is done, the if statement looks like this:
if (expression) statement 1;
else statement2;
If the expression is true, then the target of the if will execute, and the else portion will be skipped.
However, if the expression is false, then the target of the if is bypassed, and the target of the else
will execute.
Under no circumstances will both statements execute. Thus, the addition of the else provides a
two-way decision path.
If-else Statement
if (expression)
{
statement;
…………
statement;
}
else
{
statement;
…………
statement;
}
If and else Syntax
Understanding If-else Statement
Sample Code
1 #include <stdio.h>
2 int main(){
3 int a = 5;
4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }
8 else {
9 printf(“Bye World\n”);
10 a = a + 1;
11 }
12 printf(“Good Bye!”);
13 }
Sample Code
1 #include <stdio.h>
2 int main(){ a = 1 < 5? YES
Hello World
3 int a = 1; Good Bye!
Process returned 0 (0x0) execution time : 0.075 s
4 if ( a < 5 ){ Press any key to continue.
5 printf(“Hello World\n”);
6 a = a + 1;
7 }
8 else {
9 printf(“Bye World\n”);
10 a = a + 1;
11 }
12 printf(“Good Bye!”);
13 }
If-else Example
Write a C program to Divide 2 numbers. Remember, if the divisor if 0, then the
output might become infinity.
#include <stdio.h>
int main()
{
int numl, num2;
printf("Enter first number:");
scanf("%d“, &num1);
printf("Enter second number:" );
scanf("%d", &num2);
if(num2 == 0) printf("Can not divide by zero!");
else printf("Answer is: %d", num1, num2);
return 0;
}
Sequence of If’s in C
In C programming, sequential if statements allow you to evaluate a series of conditions one after
the other. The conditions are checked in the order they appear, and as soon as a true condition is
found, the corresponding block of code is executed.
if (condition1) {
// Code to execute if condition1 is true
}
if (condition2) {
// Code to execute if condition2 is true
}
// Continue with additional if statements as needed...
Unlike if-else statements, where only one block of code is executed, with sequential if statements,
multiple blocks of code can be executed if multiple conditions are true.
Since all conditions are checked, even if an earlier one evaluates to true, there may be a slight performance
overhead
Sequence of If’s Statement
if (expression)
{
statements;
}
if(expression)
{
statements;
}
......................
if(expression)
{
statements;
}
Understanding If Ladder Statement
Sample Code
#include <stdio.h>
int main(){
int a = 1;
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 1
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 1 < 3? YES
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 1
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 2
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 2 < 3? YES
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 2
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
Hello World
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 3
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
Hello World
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 3 < 3? NO
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
Hello World
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 3
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
Hello World
a = a + 1; Good Bye!
} Process returned 0 (0x0) execution time : 0.075 s
Press any key to continue.
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Write a C program to calculate average
of five numbers and count the numbers
that are above average.
Solve using if ladder
#include <stdio.h>
int main()
{ if(d>=avg)
int a,b,c,d,e, sum, count=0; {
float avg; count++;
printf("\nEnter the value of a,b,c,d,e :\n "); }
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e); if(e>=avg)
sum = a+b+c+d+e; {
avg = (float)sum/5; count++;
printf("average: %.2f\n",avg); }
if(a>=avg) printf("numbers above average %d",count);
{
count++; return 0;
} }
if(b>=avg)
{
count++;
}
if(c>=avg)
{
count++;
}
IF-Else if-Else in C
The conditions are evaluated in the order they appear. The first if statement is checked first, then
the second, and so on.
Once a true condition is found, the corresponding block of code is executed, and the rest of the
conditions are skipped.
else Block: The else block is optional. If none of the conditions are true, the code within the else
block (if present) is executed.
Limited to a Single Choice: It's designed for situations where you have one choice among many. If
you need multiple choices to be executed, you might need separate if statements or a switch
statement.
IF-Else if-Else in C
if (condition1) {
// Code to execute if condition1 is true
}
else if (condition2) {
// Code to execute if condition2 is true and condition1 is false
}
else if (condition3) {
// Code to execute if condition3 is true and conditions 1 and 2 are false
}
//...
else {
// Code to execute if none of the conditions are true
}
If-Else if-else Ladder Statement
if (expression)
{
statements;
}
else if(expression)
{
statements;
}
......................
else
{
statements;
}
Understanding If-Else if-else Ladder
Statement
Sample Code
#include <stdio.h>
int main(){
int a = 1;
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else {
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 1
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else {
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 1 < 3? YES
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else {
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 1
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
a = a + 1;
}
else if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else {
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 2
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
a = a + 1;
}
else if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else {
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 2
if ( a < 3 ){
Hello World
printf(“Hello World\n”); Good Bye!
a = a + 1; Process returned 0 (0x0) execution time : 0.075 s
} Press any key to continue.
else if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else {
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
If-Else if-Else Ladder Example
Write a C program to check whether a number is positive, negative or zero
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("The number is positive\n");
}
else if (num < 0) {
printf("The number is negative\n");
}
else {
printf("The number is zero\n");
}
return 0;
}
Understanding Nested If Else
Statement
Nested If Else Statement
Nested If Else Statement
if (expression)
{
if (expression)
{
statement;
…………
statement;
}
}
Sample Code
#include <stdio.h>
int main(){
int a = 5;
if ( a > 3 ){
printf(“a is greater than 3\n”);
if ( a < 7 ){
printf(“ and less than 7\n”);
}
else {
printf(“but not less than 7\n”);
}
}
else {
printf(“a is less than 3\n”);
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){ a = 5 >
< 3?
7? YES
int a = 5;
if ( a > 3 ){
printf(“a is greater than 3\n”); a is greater than 3
and less than 7
if ( a < 7 ){ Good Bye!
printf(“ and less than 7\n”); Process returned 0 (0x0) execution time : 0.075 s
Press any key to continue.
}
else {
printf(“but not less than 7\n”);
}
}
else {
printf(“a is less than 3\n”);
}
printf(“Good Bye!”);
}
When to use which Control Statement
Type When to Use
A single If Just need to check one condition
Sequence of only “If”s Need to check multiple condition one after another
(usually on same vars, might be true for multiple
cases)
If and else Only two conditions, only one of them might be true.
Second condition doesn’t even need to be checked.
If- else-if- else Ladder Only one condition out of multiple may be true.
Check them one after another. The last or default
condition is not checked and written with an “else”
Switch Statement
Multi-branch Decision-Making: When you have a variable with multiple possible values and each
value requires a different set of actions, a switch statement provides a clean and efficient way to
handle this.
In some cases, switch statements can be more efficient than a series of if-else statements,
especially when dealing with a large number of cases.
However, it's worth noting that switch statements have certain limitations. For example, they only
work with integral types (like int, char, etc.) and not with floating-point types. Additionally, case
labels must be constants known at compile time.
Switch Statement
switch (expression)
{
case constant 1:
// statements
break;
case constant 2:
// statements
break;
...
default: // default statements
}
Switch Statement
Switch Statement
Week Day
Day No Week
Write a C program to input day of week number(1-7) and 1 Monday
print day of week name using switch case.
Input: Input day of week number(1-7): 2 2 Tuesday
Output: Tuesday
3 Wednesday
4 Friday
5 Saturday
6 Sunday
7 Saturday
Syntax:
condition ? expression_if_true : expression_if_false;
Conditional/Ternary Operator Statement
Conditional/Ternary Operator Statement
#include <stdio.h>
int main() {
int num = 10;
int result;
return 0;
}
Write a C program to find maximum
between two numbers.
Step by Step Logic
Below is step by step descriptive logic to find maximum.
Input two numbers from user. Store it in some variable say num1 and num2.
NOT!
Value 1 Result
0 1
1 0
Logical Operators - Combine Conditions
An expression containing logical operator returns either 0 or 1 depending upon whether expression
results true or false. Logical operators are commonly used in decision making in C programming.
Write a C program to find maximum
between three numbers.
Step by Step Logic
Below is step by step descriptive logic to find maximum between three numbers:
Input three numbers from user. Store it in some variable say num1, num2 and num3.
Compare num1 with num2 and num3. If the statement is true then num1 is surely the max
value.
If not then it means num1 is smaller than num2 and num3. So, Compare num2 with num3.
If the statement is true then num2 is surely the max value.