Why do these curly brackets make a difference?

This is in VC++.

First, here is the code, it is to check whether a number is prime or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "stdafx.h"
#include "iostream"
#include "math.h"

using namespace std;

int main() {
	int n;
	int i;
	int is_prime;

	is_prime = true;

	cout << "Input a number and press ENTER: ";
	cin >> n;

	i = 2;
	while (i <= sqrt(static_cast<double>(n))) {
		if (n % i == 0) 
			is_prime = false;
			++i;
		
	}

		if (is_prime) {
			cout << "The number is prime." << endl << endl;
		}
		else {
			cout << "The number is not prime." << endl << endl;
		}

		return 0; 
}


When I add curly brackets to this:

1
2
3
if (n % i == 0) 
	is_prime = false;
	++i;


so it becomes this:
1
2
3
4
if (n % i == 0) {
	is_prime = false;
        ++i;
}

the code compiles normally but when I execute it the program only works for some numbers, like 4 and 6, but not for others, like 10 and 15.

Why is that?

Thanks.
C++ doesn't care about indentation. It has no way of knowing that the ++i is part of the if block without those braces.

if/while/for/etc only have an effect on the next block. That means up until the next semicolon, or the next set of {braces}.

1
2
3
4
5
6
7
// "yes" = part of the if block
// "no" = not part of the if block
// remember:  white space / newlines / indentation does not matter at all

if(blah) yes; no;
if(blah) { yes; yes; yes; } no;
if(blah); no; no;
Looks like someone has been coding in Python. C++ defines code blocks with {} it allows the programmer to format they way they find best and gives a clear representation of what happens where.
Thanks, it makes sense now. But now I have an unrelated question, about a modified version of this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Optimized prime1.cpp code.  Checks a number to see whether it is prime or not.

#include "stdafx.h"
#include "iostream"
#include "math.h"
using namespace std;

int main() {
	int n;
	
	// Prompt user for number.
	cout << "Input a number and press ENTER: ";
	cin >> n;
	
	// Initiating variables.
	int i = 2;
	int is_prime = true;
	double rt = sqrt((static_cast<double>( n )));
	
	if ( n == 0 || 1 ) {
		cout << "The number is not prime." << endl << endl;
		goto end1;
	}

	// Test for prime number.
	while ( i <= rt) {
		if ( n % i == 0 ) {
		is_prime = false;
		break;
		}
		i++;
		}
		
		if ( is_prime ) {
		cout << "The number is prime." << endl << endl;
		}
		
		else {
		cout << "The number is not prime." << endl << endl;
		}
		
		end1:

		return 0;
}


In this section:
1
2
3
4
if ( n == 0 || 1 ) {
		cout << "The number is not prime." << endl << endl;
		goto end1;
	}


I made it work so if the number is a 1 or a zero, the result is not prime. But when i reverse the order of the 1 and zero, like this:
1
2
3
4
	if ( n == 1 || 0 ) {
		cout << "The number is not prime." << endl << endl;
		goto end1;
	} 

it says the number is prime even if i enter a 0. Does the order matter for the || operator?
|| does not work that way. || examines the left and right side, and if either one of them is true (nonzero), then it results as true.

if(n == 0 || 1)

This will always be true, because '1' is nonzero. The || operator will look at 'n==0', then it will look at '1'. Since one of those things (in this case, the '1') is nonzero, the overall expression results in 'true'.


In this:
if ( n == 1 || 0 )

the '|| 0' is basically ignored because '0' will never be nonzero.


What you want is this:

if( n == 1 || n == 0 )
Yeah, that code worked, thanks.
closed account (S6k9GNh0)
This topic should be sticky'd for how well the question was laid out and for the use of codetags. All of the responses were clear cut and resolving. I haven't seen a post like this ever on this forum, I couldn't leave it alone. Good job everyone. :D
Last edited on
Topic archived. No new replies allowed.