0% found this document useful (0 votes)
27 views9 pages

Itp Unit-2

Introduction to programming in C unit 2
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)
27 views9 pages

Itp Unit-2

Introduction to programming in C unit 2
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/ 9

SEM :- 1-1 (R23) introduction to programming UNIT-2

SYLLABUS

1) Control Structures

2) Simple sequential programs

3) Conditional Statements (if, if-else, switch),

4) Loops (for, while, dowhile)

5.) Break and Continue.

web-D Page 1
SEM :- 1-1 (R23) introduction to programming UNIT-2

CONTROL STRUCTURES

Certainly! Let’s explore control structures in C programming. Control structures allow


you to specify the flow of execution in your program based on certain conditions or
loops. Here are some common control structures:

1. Sequential Logic (Sequential Flow):

 In sequential logic, the program executes instructions in the order they appear.
 It follows a linear path without any branching or repetition.
 Example:

#include <stdio.h>

int main() {
printf("Hello, World!\n");
printf("Welcome to C programming.\n");

web-D Page 2
SEM :- 1-1 (R23) introduction to programming UNIT-2

return 0;
}

2. Selection Logic (Conditional Flow):

a. if Statement:

 Executes a block of code if a specified condition is true.


 Example:

#include <stdio.h>

int main() {
int num = 10;
if (num > 5) {
printf("Number is greater than 5.\n");
}
return 0;
}

b. if-else Statement:

 Executes one block of code if the condition is true and another block if it’s false.
 Example:

#include <stdio.h>

int main() {
int num = 7;
if (num % 2 == 0) {
printf("Number is even.\n");
} else {
printf("Number is odd.\n");
}
return 0;

web-D Page 3
SEM :- 1-1 (R23) introduction to programming UNIT-2

c. switch Statement:

 Allows multiple conditions to be tested against a single value.


 Example:

#include <stdio.h>

int main() {
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good!\n");
break;
default:
printf("Needs improvement.\n");
}
return 0;
}

3. Iteration Logic (Repetitive Flow):

a. while Loop:

 Repeats a block of code while a specified condition is true.


 Example:

#include <stdio.h>

int main() {
int i = 1;

web-D Page 4
SEM :- 1-1 (R23) introduction to programming UNIT-2

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

b. for Loop:

 Executes a block of code a specified number of times.


 Example:

#include <stdio.h>

int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}

while Loop:

 The while loop is an entry-controlled loop in C.


 It repeatedly executes a block of code as long as a specified condition remains true.
 The condition is checked before executing the loop body.
 Syntax:
 while (condition) {
 // Code to be executed
 }
 Example:

#include <stdio.h>

web-D Page 5
SEM :- 1-1 (R23) introduction to programming UNIT-2

int main() {
int i = 0;
while (i < 5) {
printf("VINAY PREM\n");
i++;
}
return 0;
}

 Output:

VINAY PREM
VINAY PREM
VINAY PREM
VINAY PREM
VINAY PREM

2. do…while Loop:

 The do...while loop is an exit-controlled loop in C.


 It executes the loop body at least once, regardless of the condition.
 The condition is checked after executing the loop body.
 Syntax:

do {
// Code to be executed
} while (condition);

Example:

#include <stdio.h>

int main() {

web-D Page 6
SEM :- 1-1 (R23) introduction to programming UNIT-2

int i = 0;
do {
printf("VINAY PREM\n");
i++;
} while (i < 3);
return 0;
}

 Output:

VINAY PREM
VINAY PREM
VINAY PREM

The while loop checks the condition before executing the loop body, while the
do...while loop executes the body at least once and then checks the condition. Choose

the appropriate loop based on your requirements!

CONTROL STATEMENTS

Certainly! Let’s explore the break and continue statements in C programming.

1. break Statement:

 The break statement is used to terminate the execution of a loop (such as for,
while, or do...while) prematurely.

 It is commonly used within loops to exit the loop when a specific condition is met.
 Example (using break in a while loop):

#include <stdio.h>

int main() {
int i = 1;

web-D Page 7
SEM :- 1-1 (R23) introduction to programming UNIT-2

while (i <= 10) {


if (i == 5) {
break; // Exit the loop when i equals 5
}
printf("%d\n", i);
i++;
}
return 0;
}

Output:

1
2
3
4

2. continue Statement:

 The continue statement is used to skip the current iteration of a loop and move to
the next iteration.
 It allows you to bypass specific iterations based on a condition.
 Example (using continue in a for loop):

#include <stdio.h>

int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip iteration when i equals 3
}
printf("%d\n", i);
}
return 0;

web-D Page 8
SEM :- 1-1 (R23) introduction to programming UNIT-2

Output:

1
2
4
5

Both break and continue are powerful tools for controlling the flow of execution within
loops. Use them wisely based on your specific requirements!

web-D Page 9

You might also like