Instrukcja warunkowa if, else if, else

Autor podstrony: Krzysztof Zajączkowski

Stronę tą wyświetlono już: 2393 razy

Instrukcje warunkowe if, else if, else w C# wyglądają tak samo jak w C++. Oto prosty przykład:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { Console.Write("Witaj użytkowniku!nProszę podaj swoje imię: "); string name = Console.ReadLine(); Console.Write("Podaj swój wiek: "); int age = int.Parse(Console.ReadLine()); if (age < 7) { Console.Write("Witaj " + name + ", masz " + age + " lat, jesteś trochę za młody na programowanie, ale nic nie szkodzi!"); }else if(age > 80){ Console.Write("Witaj " + name + ", masz " + age + " lat, masz już swoje lata, ale niec nie szkodzi!"); }else{ Console.Write("Witaj " + name + ", masz " + age + " lat, ale ludzie w każdym wieku mogą się uczyć programowania!"); } Console.ReadLine(); } } }
Propozycje książek