10/12/2023
Dr. Dileep Kumar Singh
Head, JLU-SOET
dileep.singh@jlu.edu.in
Problem Solving and Program
Design using C
Dr. Dileep Kumar Singh 1
Problem Solving and Program Design using C
UNIT –II
B Tech/B Tech (Hons.) CSE – 1st Sem.
Decision Control Structure
Dr. Dileep Kumar Singh 2
Control Structures - if…else
• if Statements if (Condition) {
statements;
}
• if…else Statement if (Condition) {
statements; //true-case
}
• if…else if…else Statement
else {
statements; //false-case
• Nested if…else?? }
Dr. Dileep Kumar Singh 3
Dr. Dileep Kumar Singh 1
10/12/2023
Control Structures – switch…case
• switch…case Statements
switch (n)
{
case 1: // will execute if n = 1;
break;
case 2: // will execute if n = 2;
break;
default: // will execute if n doesn't match with any cases
}
Dr. Dileep Kumar Singh 4
Control Structures - Loops
• while loop
true
condition statement
false
Dr. Dileep Kumar Singh 5
Control Structures - Loops
• for loop
Initialize variable
Condition
true statement
Test the variable
Increment/Decrement
variable
false
Dr. Dileep Kumar Singh 6
Dr. Dileep Kumar Singh 2
10/12/2023
Control Structures - Loops
• do…while loop
do {
statement
statement
} while ( condition );
true
condition
false
• Nested Loops??
Dr. Dileep Kumar Singh 7
Control Structures - break & continue
• Break
Ex:
– Causes immediate exit from int next = 0;
a while, for, do/while while (true){
or switch statements Scanf(“%d”,&next);
if (next < 0)
• Continue
break;
– Skips the remaining if (next % 2) //odd number, don’t print
statements in the body of a continue;
while, for or do/while printf(“%d\n”,next);
structure and proceeds with }
the next iteration of the printf( “Out of the Loop!”);
loop
Dr. Dileep Kumar Singh 8
Thanks
Dr. Dileep Kumar Singh 9
Dr. Dileep Kumar Singh 3