Skip to content

02 Flow Control

if … else

conditional statement

branching

flowchart TB
db{1==0}

db --- |Yes|nesnt
db --- |No|blah
int x = 4;
if (x == 5)
{
  printf("Yes");
  printf("hi");
}

print("outside everything");
outside everything
if(x>10)
{
 if(y < 10) 
 {
   printf("hi");
 }
}
else
{
  printf("hi");
}
if(x>10 && y<10)
{
   printf("hi");
}
else
{
  printf("hi");
}
elif
hi
outside everything

for, if and else go to the immediate next block

x = 5
if x == 5:
  print("yes")
  print("block 1")
else:
  print("no")
  print("block 2")

Ternary Operator

cooler way of doing if else

(y<10)?(x=10):(x=0)

Switch

switch(s):
{
  case 1: something; break;
  case 2: something; break;
  default: something;
}

switch(s):
{
  case 'a': something; break;
  case 'b': something; break;
  default: something;
}
Last Updated: 2023-01-25 ; Contributors: AhmedThahir

Comments