repl.it
@kironroy/

control_flow

C#

for , foreach, if/else, ternary, switch, while, do-while

fork
loading
Files
  • main.cs
  • main.exe
  • Notes.txt
  • Season.cs
main.cs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class MainClass {
  public static void Main (string[] args) {

 // ---- if else statement ----  
   
 int hour = 19;

 if (hour > 0 && hour < 12)
   Console.WriteLine("It's morning, Morning!");

 else if (hour >= 12 && hour < 18 )
   Console.WriteLine("It's the afternoon, energy level is low");

 else 
   Console.WriteLine("It's finally the evening");

   Console.WriteLine(); // breakline
  

 // ---- Ternary ? ---- 
 
 bool isVIPmember = true; // false would be 29.00
 //float price;
 //if (isVIPmember)
 //   price = 19.99f;
 //else 
 //     price = 25.00f;

float price = (isVIPmember) ? 19.99f : 29.00f;
Console.WriteLine(price);

Console.WriteLine(); // breakline

 // ---- switch ----  

var season = Season.Spring;

switch (season) 
    {
         case Season.Spring:
         case Season.Summer:
         Console.WriteLine("Time to swim in the bay!");
           break;

         default:
         Console.WriteLine("Monsoon season?  Yikes!");
           break;
    }

  Console.WriteLine(); // breakline

// for loop

for (var i = 10; i >= 1; i--)
{
  if ( i % 2 == 0)
  {
    Console.WriteLine(i);
  }
}

  Console.WriteLine(); // breakline

// foreach loop

var actressName = "Grace Kelly";

foreach (var character in actressName)
{
  Console.Write(character);

}

Console.WriteLine(); // breakline

  // foreach with int array

  var jennysNumber = new int[] {8, 6, 7, 3, 0, 9};

  foreach (var number in jennysNumber)
  {
    Console.Write(number);
    
  }

  Console.WriteLine(); // breakline
  Console.WriteLine(); // breakline

// do while loop

var ii = 0;
while (ii <= 10)
{
  if (ii % 2 == 0)
  Console.WriteLine(ii);
  ii++;
}

 Console.WriteLine(); // breakline

// random class

    var randomPassword = new Random();

    const int passwordLength = 10;
    var buffer = new char[passwordLength];
       for (var i = 0; i < passwordLength; i++)
       buffer[i] = (char)('a' + randomPassword.Next(0, 26));

      var password = new string(buffer);

      Console.WriteLine(password);

 Console.WriteLine(); // breakline
 
// do while loop better example

while(true)
 {
    Console.Write("Type your name");
    var nameInput = Console.ReadLine();

    if (!String.IsNullOrWhiteSpace(nameInput))
   {
     Console.WriteLine("@Echo: " + nameInput);
     continue;
   }

    break;
 }

  Console.WriteLine(); // breakline





  } // main function bracket 
} // main class brakcet