0% found this document useful (0 votes)
5 views30 pages

Lecture 02 Control

The document is a lecture on C programming focusing on control structures and loops, including decision-making constructs like if-else and switch statements, as well as looping constructs such as for, while, and do-while loops. It provides syntax examples, problem-solving approaches, and exercises for practical understanding. Key points emphasize the use of different conditional operators and the importance of loops in programming for code efficiency and organization.

Uploaded by

Asif Rahat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views30 pages

Lecture 02 Control

The document is a lecture on C programming focusing on control structures and loops, including decision-making constructs like if-else and switch statements, as well as looping constructs such as for, while, and do-while loops. It provides syntax examples, problem-solving approaches, and exercises for practical understanding. Key points emphasize the use of different conditional operators and the importance of loops in programming for code efficiency and organization.

Uploaded by

Asif Rahat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

C Programming: Control Structures and Loops

Prof. Dr. Rafiqul Islam

January 23, 2025

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 1 / 30
Session 3: Decision Making and Management

Problem Statement:
Assume given marks are x for a student.
Determine the grade of the student based on the following conditions:

If x > 95, then the student is brilliant.


If x < 30, then the student is poor.
If x ≤ 95 and x ≥ 30, then the student is average.

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 2 / 30
Decision Making and Management

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 3 / 30
Conditional Operators in C

Common Conditional Operators in C:


if - Executes a block of code if a specified condition is true.
if-else - Executes one block of code if the condition is true,
otherwise another block.
nested if-else - Used for multiple conditions.
switch - Selects one of many code blocks to execute.
ternary operator (? :) - A shorthand for if-else, written as:
condition ? expression1 : expression2;
Example of Ternary Operator: result = (x > 10) ? "Large" :
"Small";

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 4 / 30
if Statement Syntax

The syntax of the if statement in C is as follows:

if (condition) {
// Code to execute if the condition is true
}

Example:

if (x > 0) {
printf("x is positive.");
}

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 5 / 30
if-else Statement Syntax
The syntax of the if-else statement in C is as follows:

if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}

Example:

if (x > 0) {
printf("x is positive.");
} else {
printf("x is not positive.");
}

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 6 / 30
if-else if-else Statement Syntax
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if all conditions are false
}

Example:
if (x > 0) {
printf("x is positive.");
} else if (x < 0) {
printf("x is negative.");
} else {
printf("x is zero.");
}
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 7 / 30
Solution Using if-else
Example program to solve the problem:
1 # include < stdio .h >
2
3 int main () {
4 int x ;
5 printf ( " Enter marks : " );
6 scanf ( " % d " , & x );
7
8 if ( x > 95) {
9 printf ( " Student is brilliant \ n " );
10 } else if ( x < 30) {
11 printf ( " Student is poor \ n " );
12 } else {
13 printf ( " Student is average \ n " );
14 }
15 return 0;
16 }

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 8 / 30
Solution Using Nested if-else
Example program with nested conditions:
1 # include < stdio .h >
2
3 int main () {
4 int x ;
5 printf ( " Enter marks : " );
6 scanf ( " % d " , & x );
7
8 if ( x > 95) {
9 printf ( " Student is brilliant \ n " );
10 } else {
11 if ( x < 30) {
12 printf ( " Student is poor \ n " );
13 } else {
14 printf ( " Student is average \ n " );
15 }
16 }
17 return 0;
18 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 9 / 30
Ternary Operator Syntax

The syntax of the ternary operator in C is as follows:

condition ? expression1 : expression2;

- **Condition**: A logical expression evaluated to true or false. -


**Expression1**: Executed if the condition is true. - **Expression2**:
Executed if the condition is false.
Example:

int max = (a > b) ? a : b;


printf("The maximum is %d", max);

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 10 / 30
Solution Using Ternary Operator

Example program with ? : operator:


1 # include < stdio .h >
2
3 int main () {
4 int x ;
5 printf ( " Enter marks : " );
6 scanf ( " % d " , & x );
7
8 printf ( " % s \ n " , ( x > 95) ? " Student is brilliant " :
9 ( x < 30) ? " Student is poor " :
10 " Student is average " );
11 return 0;
12 }

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 11 / 30
Summary of Approaches

Key Points:
if-else: Clear and simple for straightforward logic.
Nested if-else: Useful for multiple levels of conditions.
Ternary Operator: Concise but less readable for complex conditions.
Choose the Approach:
Use if-else for simplicity.
Use nested if-else for more structured conditions.
Use the ternary operator for concise, single-line conditions.

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 12 / 30
Exercise: if-else

Example of if-else statement to check even or odd:


1 # include < stdio .h >
2
3 int main () {
4 int num ;
5 printf ( " Enter a number : " );
6 scanf ( " % d " , & num );
7
8 if ( num \% 2 == 0) { % Escape the % character to a
9 printf ( " Even number \ n " );
10 } else {
11 printf ( " Odd number \ n " );
12 }
13 return 0;
14 }

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 13 / 30
Exercise: Nested if-else
Example of nested if-else to check grading system:
1 # include < stdio .h >
2
3 int main () {
4 int marks ;
5 printf ( " Enter marks : " );
6 scanf ( " % d " , & marks );
7
8 if ( marks >= 50) {
9 if ( marks >= 75) {
10 printf ( " Grade A \ n " );
11 } else {
12 printf ( " Grade B \ n " );
13 }
14 } else {
15 printf ( " Fail \ n " );
16 }
17 return 0;
18 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 14 / 30
Switch Statement Syntax

The syntax of the switch statement in C is as follows:

switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
...
default:
// Code to execute if no case matches
}

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 15 / 30
Exercise: switch
Example of switch statement to check day of the week:
1 # include < stdio .h >
2 int main () {
3 int day ;
4 printf ( " Enter day number (1 -7): " );
5 scanf ( " % d " , & day );
6 switch ( day ) {
7 case 1: printf ( " Sunday \ n " ); break ;
8 case 2: printf ( " Monday \ n " ); break ;
9 case 3: printf ( " Tuesday \ n " ); break ;
10 case 4: printf ( " Wednesday \ n " ); break ;
11 case 5: printf ( " Thursday \ n " ); break ;
12 case 6: printf ( " Friday \ n " ); break ;
13 case 7: printf ( " Saturday \ n " ); break ;
14 default : printf ( " Invalid day \ n " );
15 }
16 return 0;
17 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 16 / 30
Session 4: Looping Constructs

Loops in programming are used to repeat a block of code until the


specified condition is met. A loop statement allows programmers to
execute a statement or group of statements multiple times without
repetition of code.
Looping constructs: for, while, and do-while.
break and continue statements.

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 17 / 30
Why Looping?
Consider an Example:
1 // C program to illustrate need of loops
2 # include < stdio .h >
3

4 int main ()
5 {
6 printf ( " Hello World \ n " );
7 printf ( " Hello World \ n " );
8 printf ( " Hello World \ n " );
9 printf ( " Hello World \ n " );
10 printf ( " Hello World \ n " );
11 printf ( " Hello World \ n " );
12 printf ( " Hello World \ n " );
13 printf ( " Hello World \ n " );
14 printf ( " Hello World \ n " );
15 printf ( " Hello World \ n " );
16 return 0;
17 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 18 / 30
Types of Loop

Entry Controlled loops: In Entry controlled loops the test condition


is checked before entering the main body of the loop. For Loop and
While Loop is Entry-controlled loops.
Exit Controlled loops: In Exit controlled loops the test condition is
evaluated at the end of the loop body. The loop body will execute at
least once, irrespective of whether the condition is true or false.
do-while Loop is Exit Controlled loop.

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 19 / 30
Syntax of for Loop

The syntax of the for loop in C is as follows:

for (initialization; condition; increment/decrement) {


// Code to execute in each iteration
}

Example:

for (int i = 0; i < 5; i++) {


printf("%d ", i);
}

- **Initialization**: Sets the starting point of the loop. - **Condition**:


Evaluates to true to keep the loop running. - **Increment/Decrement**:
Updates the loop variable.
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 20 / 30
Example: for Loop

Example of a for loop to print 1 to 5:


1 # include < stdio .h >
2
3 int main () {
4 for ( int i = 1; i <= 5; i ++) {
5 printf ( " % d " , i );
6 }
7 return 0;
8 }

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 21 / 30
Syntax of while Loop
The syntax of the while loop in C is as follows:

while (condition) {
// Code to execute while the condition is true
}

Example:

int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}

- The **condition** is checked before entering the loop body. - The loop
runs as long as the condition is true.
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 22 / 30
Example: while Loop

Example of a while loop to print 1 to 5:


1 # include < stdio .h >
2

3 int main () {
4 int i = 1;
5 while ( i <= 5) {
6 printf ( " % d " , i );
7 i ++;
8 }
9 return 0;
10 }

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 23 / 30
Syntax of do-while Loop
The syntax of the do-while loop in C is as follows:

do {
// Code to execute at least once
} while (condition);

Example:

int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);

- The **do-while** loop executes the code block at least once, regardless
of the condition. - The condition is checked after the code block executes.
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 24 / 30
Example: do-while Loop

Example of a do-while loop to print 1 to 5:


1 # include < stdio .h >
2

3 int main () {
4 int i = 1;
5 do {
6 printf ( " % d " , i );
7 i ++;
8 } while ( i <= 5);
9 return 0;
10 }

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 25 / 30
Example using Nested Loops: Multiplication Table

Example of a nested for loop to print a multiplication table:


1 # include < stdio .h >
2

3 int main () {
4 for ( int i = 1; i <= 10; i ++) {
5 for ( int j = 1; j <= 10; j ++) {
6 printf ( " % d \ t " , i * j );
7 }
8 printf ( " \ n " );
9 }
10 return 0;
11 }

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 26 / 30
Break and Continue Statements

Example of using break and continue:


1 # include < stdio .h >
2
3 int main () {
4 for ( int i = 1; i <= 10; i ++) {
5 if ( i == 5) {
6 break ; // Exit the loop
7 }
8 if ( i == 3) {
9 continue ; // Skip the iteration
10 }
11 printf ( " % d " , i );
12 }
13 return 0;
14 }

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 27 / 30
Example and Exercise
// Program to calculate the sum of first n natural numbers:
1 # include < stdio .h >
2 int main ()
3 {
4 int num , count , sum = 0;
5
6 printf ( " Enter a positive integer : " );
7 scanf ( " % d " , & num );
8 // for loop terminates when count exceeds num
9 for ( count = 1; count <= num ; ++ count )
10 {
11 sum += count ;
12 }
13
14 printf ( " Sum = % d " , sum );
15

16 return 0;
17 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 28 / 30
Example and Exercise

// Printing a Triangle Pattern in C:


1
2 # include < stdio .h >
3
4 int main () {
5 int n = 5; // Number of rows
6
7 for ( int i = 1; i <= n ; i ++) {
8 for ( int j = 1; j <= i ; j ++) {
9 printf ( " * " );
10 }
11 printf ( " \ n " ); // Move to the next line
12 }
13
14 return 0;
15 }

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 29 / 30
Exercise: Practice with Conditions and Loops

Solve the following problems using C programming:


1 Write a program to check if a given number is even or odd.
2 Calculate the sum of all numbers from 1 to a user-given number n.
3 Create a program to find the factorial of a number.
4 Display the multiplication table of a user-given number.
5 Write a program to reverse the digits of a number.
6 Find the largest of three user-given numbers.
7 Print all prime numbers between 1 and 100.
8 Calculate the average of n user-given numbers.
9 Generate a right-angled triangle pattern using *.
10 Check if a given number is a palindrome.

Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 30 / 30

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy