0
0
Cprogramming~20 mins

Break statement in C - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Break Statement in C
📖 Scenario: You are creating a simple program that searches for a specific number in a list of numbers. Once the number is found, the program should stop searching immediately to save time.
🎯 Goal: Build a C program that uses a break statement inside a for loop to stop searching when the target number is found.
📋 What You'll Learn
Create an array of integers with exact values
Create a target number variable
Use a for loop to search the array
Use a break statement to stop the loop when the target is found
Print the index where the target was found or a message if not found
💡 Why This Matters
🌍 Real World
Searching for a value quickly in a list is common in many programs, like finding a contact in your phone or a product in a store.
💼 Career
Understanding how to stop loops early with break helps write efficient code, which is important for software developers and engineers.
Progress0 / 4 steps
1
Create the array of numbers
Create an integer array called numbers with these exact values: 3, 7, 1, 9, 5.
C
Need a hint?

Use curly braces {} to list the numbers inside the array.

2
Set the target number to find
Create an integer variable called target and set it to 9.
C
Need a hint?

Use int target = 9; to create the variable.

3
Search the array using a for loop and break
Write a for loop using int i = 0 to i < 5 to check each element in numbers. Inside the loop, use an if statement to check if numbers[i] equals target. If yes, use break to stop the loop.
C
Need a hint?

Use break; inside the if block to stop the loop.

4
Print the result
Write a printf statement to print "Found target at index: %d\n" with the variable index if index is not -1. Otherwise, print "Target not found".
C
Need a hint?

Use printf with %d to show the index.