Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
A.D.A.M

C++ syntax error clarification and some stuff.

Recommended Posts

Posted · Original PosterOP

I got this mcq am wondering if i got the right idea:

3. Analyze the following code:

Code 1:

bool even;
int number;

if (number % 2 == 0)
even = true;
else
even = false;

Code 2:

bool even = (number % 2 == 0);
a. Code 2 has syntax errors.
b. Code 1 has syntax errors.
c. Both Code 1 and Code 2 have syntax errors.
d. Both Code 1 and Code 2 are correct, but Code 2 is better.

 

If i use VS2015 i can't compile without explicitly saying int number=0; (since without it num is undefined.) so answer would be "C" correct? Since 2 definitely can't run without initialisation of num. 


Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to post
Share on other sites
-SNIP-

 

int number = 0;bool even = (number % 2 == 0) ? true : false;

 

You could do something like that. You should always initialize a variable anyways before doing anything else.

Link to post
Share on other sites
Posted · Original PosterOP

Ah ok thanks. Another thing if you don't mind.

say we have something like this 

int x;int y;x=3;switch (x+3){    case 6:  y = 0;       case 7:  y = 1;       default: y += 1;     }

this would cause the it to run each case yeah? Until it reaches default?

so first y would be assigned 0 , then 1 ,and then y= y+1 thus 1+1 so 2?


Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to post
Share on other sites

yep. you cannot do an operation on number without defining it first.

 

its c

You can do this, just matters on the compiler. Xcode gives no errors with this program.

What are you trying to do with this program though? If you were trying to make it random then this is not the way to do it.


Main Gaming PC - i7 5820K @ 4.5GHz 1.25V - EVGA Hybrid 1070ti 2100Mhz Boost 8Ghz Mem.- MSI X99 SLI Krait - Corsair H115i - GSkill Ripjaws V 3000MHz - HX850i - Samsung 840 Pro 120GB SSD - Samsung 850 Evo 512GB - Toshiba 3TB 7200RPM HD - Phanteks P400S

 

Laptop - HP Spectre x360 15t -  i7 8705G - Vega M GL - 16GB DDR4 - 512GB NVME - Windows 10

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 12 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to post
Share on other sites

Ah ok thanks. Another thing if you don't mind.

say we have something like this 

int x;int y;x=3;switch (x+3){    case 6:  y = 0;       case 7:  y = 1;       default: y += 1;     }

this would cause the it to run each case yeah? Until it reaches default?

so first y would be assigned 0 , then 1 ,and then y= y+1 thus 1+1 so 2?

Switch statements are not loops so no. If you were to put it within a do while, while, or for, this would be ok although it would never enter case 7 as x does not ever equal 7.


Main Gaming PC - i7 5820K @ 4.5GHz 1.25V - EVGA Hybrid 1070ti 2100Mhz Boost 8Ghz Mem.- MSI X99 SLI Krait - Corsair H115i - GSkill Ripjaws V 3000MHz - HX850i - Samsung 840 Pro 120GB SSD - Samsung 850 Evo 512GB - Toshiba 3TB 7200RPM HD - Phanteks P400S

 

Laptop - HP Spectre x360 15t -  i7 8705G - Vega M GL - 16GB DDR4 - 512GB NVME - Windows 10

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 12 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to post
Share on other sites
Posted · Original PosterOP

You can do this, just matters on the compiler. Xcode gives no errors with this program.

What are you trying to do with this program though? If you were trying to make it random then this is not the way to do it.

oh it's just a question i have to solve (spotting code errors and all that). Yeah my friends with dev C++ says it compiles fine...


Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to post
Share on other sites

Ah ok thanks. Another thing if you don't mind.

say we have something like this 

int x;int y;x=3;switch (x+3){    case 6:  y = 0;       case 7:  y = 1;       default: y += 1;     }

this would cause the it to run each case yeah? Until it reaches default?

so first y would be assigned 0 , then 1 ,and then y= y+1 thus 1+1 so 2?

 

x+3 = 6, so case 6 would be called. There is no looping in a switch.

Link to post
Share on other sites
Posted · Original PosterOP

Switch statements are not loops so no. If you were to put it within a do while, while, or for, this would be ok although it would never enter case 7 as x does not ever equal 7.

so how does it ever cout<<y; a 2?  because there isn't a break shouldn't it like well break...


Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to post
Share on other sites

Ah ok thanks. Another thing if you don't mind.

say we have something like this 

int x;int y;x=3;switch (x+3){    case 6:  y = 0;       case 7:  y = 1;       default: y += 1;     }

this would cause the it to run each case yeah? Until it reaches default?

so first y would be assigned 0 , then 1 ,and then y= y+1 thus 1+1 so 2?

switch just gives your 1 case. it doesnt do any more than that (loops or anything else you think it might do)

Link to post
Share on other sites

oh it's just a question i have to solve (spotting code errors and all that). Yeah my friends with dev C++ says it compiles fine...

Ah. On Xcode it just defaults ints to 32767. 


Main Gaming PC - i7 5820K @ 4.5GHz 1.25V - EVGA Hybrid 1070ti 2100Mhz Boost 8Ghz Mem.- MSI X99 SLI Krait - Corsair H115i - GSkill Ripjaws V 3000MHz - HX850i - Samsung 840 Pro 120GB SSD - Samsung 850 Evo 512GB - Toshiba 3TB 7200RPM HD - Phanteks P400S

 

Laptop - HP Spectre x360 15t -  i7 8705G - Vega M GL - 16GB DDR4 - 512GB NVME - Windows 10

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 12 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to post
Share on other sites

so how does it ever cout<<y; a 2?  because there isn't a break shouldn't it like well break...

I just ran it and yes Y comes out to 2 but I don't exactly know why. To my knowledge it should only return the value of Case 6 as it is the value that matches X.


Main Gaming PC - i7 5820K @ 4.5GHz 1.25V - EVGA Hybrid 1070ti 2100Mhz Boost 8Ghz Mem.- MSI X99 SLI Krait - Corsair H115i - GSkill Ripjaws V 3000MHz - HX850i - Samsung 840 Pro 120GB SSD - Samsung 850 Evo 512GB - Toshiba 3TB 7200RPM HD - Phanteks P400S

 

Laptop - HP Spectre x360 15t -  i7 8705G - Vega M GL - 16GB DDR4 - 512GB NVME - Windows 10

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 12 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to post
Share on other sites
Posted · Original PosterOP

I just ran it and yes Y comes out to 2 but I don't exactly know why. To my knowledge it should only return the value of Case 6 as it is the value that matches X.

the way i understand is its missing the break; so it runs till the end? Since only when i do that will it give the expected result


Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to post
Share on other sites

the way i understand is its missing the break; so it runs till the end? Since only when i do that will it give the expected result

After reading this http://stackoverflow.com/questions/252489/why-was-the-switch-statement-designed-to-need-a-break and retesting the code it is infact having a fall through occur allowing it to sort of loop through these expressions.


Main Gaming PC - i7 5820K @ 4.5GHz 1.25V - EVGA Hybrid 1070ti 2100Mhz Boost 8Ghz Mem.- MSI X99 SLI Krait - Corsair H115i - GSkill Ripjaws V 3000MHz - HX850i - Samsung 840 Pro 120GB SSD - Samsung 850 Evo 512GB - Toshiba 3TB 7200RPM HD - Phanteks P400S

 

Laptop - HP Spectre x360 15t -  i7 8705G - Vega M GL - 16GB DDR4 - 512GB NVME - Windows 10

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 12 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to post
Share on other sites
Posted · Original PosterOP

After reading this http://stackoverflow.com/questions/252489/why-was-the-switch-statement-designed-to-need-a-break and retesting the code it is infact having a fall through occur allowing it to sort of loop through these expressions.

so the previous assumption was correct?


Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to post
Share on other sites

After reading this http://stackoverflow.com/questions/252489/why-was-the-switch-statement-designed-to-need-a-break and retesting the code it is infact having a fall through occur allowing it to sort of loop through these expressions.

 

Yes, if there's no break, a case will fall through to the next. You'll often see it used like this where more than one case runs the same code.

switch (x){    case 1:    case 2:        // Does something if x == 1 || x == 2        break;    case 3:        // Does something else if x == 3        break;    default:        // Does something else if the above cases aren't called}

Keep in mind that while fall through can be intentional, it can also be a bug because people can forget to add the break.

Link to post
Share on other sites
Posted · Original PosterOP

Yes, if there's no break, a case will fall through to the next. You'll often see it used like this where more than one case runs the same code.

Keep in mind that while fall through can be intentional, it can also be a bug because people can forget to add the break.

So its called a fall? Good thing to know. 

Also, off topic but you proficient in jQuery.If you are I'd appreciate if you would look at my other thread.

http://linustechtips.com/main/topic/456363-need-some-help-with-jquery-and-making-chrome-extensions/#entry6124428


Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Buy VPN

×