break statement doesn't work in a loop [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I saw a tutorial that explaining how to use the "break" statement in a loop
but every time i'm trying to use it i'm getting a compilation error saying:
"break statement not within loop or switch
break;"
This is my code:
if (finalFirstChar > 6 || finalFirstChar < 1)
{
printf("You didn't entered a proper number! \n");
break;
}

FWIW, if is a condition (selection statement, to be exact), and break works in a loop (iteration statement)/switch-case statement.
As per C11, chapter ยง6.8.6.3
A break statement shall appear only in or as a switch body or loop body.
In any case, you don't need a break in a if statement body.

OK:
while (cond1) {
if (cond2) break; /* or continue; */
if (cond3) return [something];
}
for (init; cond4; after) {
if (cond5) break; /* or continue; */
if (cond6) return [something];
}
do {
if (cond7) break; /* or continue; */
if (cond8) return [something];
} while (cond9);
if (cond10) return [something];
NOT OK:
if (cond11) { /* not in any while/do/for loop, or case switch */
break; /* or continue; */
}

Related

How to compare enum values in switch-case? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Why does the default case fire for the following:
typedef enum {VALUE_ONE, VALUE_TWO} someValue;
typedef struct {
someValue value;
} myStruct;
---main() BELOW---
myStruct* myPtr = malloc(sizeof(myStruct));
myPtr->value = VALUE_ONE;
switch (myPtr->value) {
case VALUE_ONE:
...;
case VALUE_TWO:
...;
default:
...;
}
If I use if-statements, the code will work properly. Currently, I would like to use the switch-case to print out value specific phrases.
UPDATE: Forgot break statement. Thanks everyone!
Use break statement in switch case so that when a break statement is reached, the switch terminates and the flow of control jumps to the next line following the switch statement.
switch (myPtr->value) {
case VALUE_ONE:
...;
break;
case VALUE_TWO:
...;
break;
default:
...;
}
While using the switch case statement, you have to do something like this to get the correct result
switch ( <variable> ) {
case 1:
Code to execute if <variable> == this-value
break;
case 2:
Code to execute if <variable> == that-value
break;
...
default:
Code to execute if <variable> does not equal the value following any of the cases
break;
}
suppose you missed the break statement of case 2, so whenever the switch-case 2 will be true, it will enter in all the other cases(which comes after case 2) utill the code encounters a break statement.
Break statement is used to come out the case after performing the desired action.
Hope so it's clear.

Easy C Program While loop Not Working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
hey im lost on why this loop doesnt work it all seems right but nothing inside the while works please help the rest of the code is in other files if you need them i can post them
#include <stdio.h>
#include "weatherstation.h"
int dunits = METRIC;
void main(void)
{
char test;
InitializeWeatherStation();
while(1);
{
UpdateWeatherStation();
printf("Enter m for Metric units, b for British units, or q to quit");
scanf_s("%c",&test);
if(test == 'm')
{
dunits = METRIC;
}
else if(test == 'b')
{
dunits = BRITISH;
}
else if(test == 'q')
{
return;
}
DisplayWeatherData(dunits);
}
}
while(1);
{
something;
}
is exactly the same as:
while(1)
{
}
{
something;
}
In other words, what you have there is an infinite loop followed by a scoped block of code (which will never be reached).
Get rid of the semicolon and it should fix that particular problem.
You must not end the while(1) with a semi-colon dude. Because that's a null statement you wrote in there.

Case/Break and '{' in my function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm working on my latest new function. Any explanations would be great since I'm new to programming. I couldn't figure out how to fix this because the errors I got are:
18 syntax error before '{' token
20 case label not within a switch statement
21 (same as above)
22 (same as above)
23 `default' label not within a switch statement
29 [Warning] `return' with a value, in function returning void
32 [Warning] assignment makes pointer from integer without a cast
34 [Warning] `return' with a value, in function returning void
void moveCar(char board[], char vehicle, char direction, int distance)
{
int i;
int position, newPosition;
int offset;
for (i = 0; i < size; i++) //the main loop for the vehicles and user's input
{
if(isalpha(vehicle))//vehicles for all letters but 'x'
{
if(board[i] == vehicle)//vehicles on board
{
swtich(direction)
{
case 'r': offset = 1; break;
case 'l': offset = -1; break;
case 'u': offset = -8; break;
case 'd': offset = 8; break;
default: printf("invalid direction"); break;
newPosition = position + offset;
if(newPosition != '.')
{
printf("invalid move.");
return 0;
}
board = '.';
board[newPosition] = vehicle;
return 1;
}
}
}
}
It looks like you have a typo of Switch as Swtich, the compiler won't recognize it, and the case statements will fail to compile.
Additionally you close your switch statement too early. If you want those commands to execute only in default move them before the break. If you want them to execute for every case move them outside of the switch statement entirely

so break statement just break out the inner for loop or both in c [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
1.so break statement just break out the inner for loop or both?
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
if(grid[j][k]=='p')
{
x=j;
y=k;
break;
}
}
}
break will only break out the inner loop. If you need to break out as soon as you found the element, use goto. Though you can avoid goto in this case using other techniques such as setting a flag. In my opinion, goto is the clean choice in this usage, especially when the loop is deeply nested.
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
if(grid[j][k]=='p')
{
x=j;
y=k;
goto found;
}
}
}
found:
//other processing
You can only break out of the current scope in C as C works in a "Last In, First Out" system.
Within 2 loops, you can only break out of the scope of the loop you are currently in. So you would need to set a flag, then break, and then in the outer loop, check for that flag and then if that flag exists, reset the flag and break.
break only breaks from the immediate loop in which it is used. For breaking out of remaining loops use goto or flags. If you cannot use goto then set flags. Here is how your code looks when using flags:
int flag = 0; //set a flag and initialize it to zero
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
if(grid[j][k]=='p')
{
x=j;
y=k;
flag = 1; // set flag to 1
break;
}
if (flag == 1) break;
}
if (flag == 1) break;
}

How to use kbhit and getch (C programming) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to create a function that will printf a certain string if the user presses any button on the keyboard EXCEPT for capital P, if the user presses P then it will break the loop.
However I don't think I'm using _kbhit and _getch properly. I use the number 80 because that is the ASCII symbol for 80....sorry for any confusion
void activateAlarm(int channelID) {
int key = 0;
while(temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit
||temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit) {
beep(350,100);
if (_kbhit()) {
key = _getch();
if(key == 'P');
break;
}
}
}
No need to explain, the code talks better :
#include <conio.h>
// ...
printf("please press P key to pause \n ");
int key = 0;
while(1)
{
if (_kbhit())
{
key =_getch();
if (key == 'P')
break;
}
}

Resources