What am I doing wrong? [Nested if statements] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What am I doing wrong? [Nested if statements]

I've typed the code exactly as it told me to in the code playground for that particular lesson, which is as follows: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int age = 18; if (age > 14) { if(age > 18) { Console.WriteLine("Adult"); } else { Console.WriteLine("Teenager"); } } else { if (age > 0) { Console.WriteLine("Child"); } else { Console.WriteLine("Something's wrong"); } } } } } That's the lesson from the code. I've typed it exactly as so and it still gives me several errors, I've even tried it in Visual Studio and I'm stumped. Here is an example of my code. This lesson is confusing me to hell and back. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int age = 18; if (age > 14) { if (age > 18) { Console.WriteLine("Adult"); } else { Console.WriteLine("Teenager"); } } else { if (age = 0) { Console.WriteLine("Child"); } } else { Console.WriteLine("Something's wrong"); } } } } } It gave me the following error: ..\Playground\(34, 1): error CS1022: Type or namespace definition, or end-of-file expected

23rd Sep 2016, 1:29 AM
Jessica Miner
Jessica Miner - avatar
1 Answer
+ 1
you are using two else statements. if you want to split the else given in the playground code then use else if. the syntax is if{} else if {} else {}. Also you have an extra closing curly brace.
26th Sep 2016, 1:44 PM
Kriti Bansal
Kriti Bansal - avatar