WeixiangYan/CodeTransOcean huggingface dataset:
Task: Find the intersection of two lines







Python: from __future__ import division

def setup():
    
    (a, b), (c, d) = (4, 0), (6, 10)  
    (e, f), (g, h) = (0, 3), (10, 7)  
    pt = line_instersect(a, b, c, d, e, f, g, h)
    scale(9)
    line(a, b, c, d)
    line(e, f, g, h)
    if pt:
        x, y = pt
        stroke(255)
        point(x, y)
    println(pt)  

def line_instersect(Ax1, Ay1, Ax2, Ay2, Bx1, By1, Bx2, By2):
    
    d = (By2 - By1) * (Ax2 - Ax1) - (Bx2 - Bx1) * (Ay2 - Ay1)
    if d:
        uA = ((Bx2 - Bx1) * (Ay1 - By1) - (By2 - By1) * (Ax1 - Bx1)) / d
        uB = ((Ax2 - Ax1) * (Ay1 - By1) - (Ay2 - Ay1) * (Ax1 - Bx1)) / d
    else:
        return
    if not(0 <= uA <= 1 and 0 <= uB <= 1):
        return
    x = Ax1 + uA * (Ax2 - Ax1)
    y = Ay1 + uA * (Ay2 - Ay1)
    return x, y
Task: Find the intersection of two lines








VB: Option Explicit

Public Type Point
  x As Double
  y As Double
  invalid As Boolean
End Type

Public Type Line
  s As Point
  e As Point
End Type
  
Public Function GetIntersectionPoint(L1 As Line, L2 As Line) As Point
Dim a1 As Double
Dim b1 As Double
Dim c1 As Double
Dim a2 As Double
Dim b2 As Double
Dim c2 As Double
Dim det As Double

  a1 = L1.e.y - L1.s.y
  b1 = L1.s.x - L1.e.x
  c1 = a1 * L1.s.x + b1 * L1.s.y
  a2 = L2.e.y - L2.s.y
  b2 = L2.s.x - L2.e.x
  c2 = a2 * L2.s.x + b2 * L2.s.y
  det = a1 * b2 - a2 * b1
  
  If det Then
    With GetIntersectionPoint
      .x = (b2 * c1 - b1 * c2) / det
      .y = (a1 * c2 - a2 * c1) / det
    End With
  Else
    GetIntersectionPoint.invalid = True
  End If
End Function

Sub Main()
Dim ln1 As Line
Dim ln2 As Line
Dim ip As Point
  
  ln1.s.x = 4
  ln1.s.y = 0
  ln1.e.x = 6
  ln1.e.y = 10
  ln2.s.x = 0
  ln2.s.y = 3
  ln2.e.x = 10
  ln2.e.y = 7
  ip = GetIntersectionPoint(ln1, ln2)
  Debug.Assert Not ip.invalid
  Debug.Assert ip.x = 5 And ip.y = 5
  
  LSet ln2.s = ln2.e
  ip = GetIntersectionPoint(ln1, ln2)
  Debug.Assert ip.invalid
  
  LSet ln2 = ln1
  ip = GetIntersectionPoint(ln1, ln2)
  Debug.Assert ip.invalid
  
End Sub
Task: Find the intersection of two lines




Go: package main

import (
	"fmt"
	"errors"
)

type Point struct {
	x float64
	y float64
}

type Line struct {
	slope float64
	yint float64
}

func CreateLine (a, b Point) Line {
	slope := (b.y-a.y) / (b.x-a.x)
	yint := a.y - slope*a.x
	return Line{slope, yint}
} 

func EvalX (l Line, x float64) float64 {
	return l.slope*x + l.yint
}

func Intersection (l1, l2 Line) (Point, error) {
	if l1.slope == l2.slope {
		return Point{}, errors.New("The lines do not intersect")
	}
	x := (l2.yint-l1.yint) / (l1.slope-l2.slope)
	y := EvalX(l1, x)
	return Point{x, y}, nil
}

func main() {
	l1 := CreateLine(Point{4, 0}, Point{6, 10})
	l2 := CreateLine(Point{0, 3}, Point{10, 7})
	if result, err := Intersection(l1, l2); err == nil {
		fmt.Println(result)
	} else {
		fmt.Println("The lines do not intersect")
	}
}
Task: Find the intersection of two lines



C#: using System;
using System.Drawing;
public class Program
{
    static PointF FindIntersection(PointF s1, PointF e1, PointF s2, PointF e2) {
        float a1 = e1.Y - s1.Y;
        float b1 = s1.X - e1.X;
        float c1 = a1 * s1.X + b1 * s1.Y;

        float a2 = e2.Y - s2.Y;
        float b2 = s2.X - e2.X;
        float c2 = a2 * s2.X + b2 * s2.Y;

        float delta = a1 * b2 - a2 * b1;
        
        return delta == 0 ? new PointF(float.NaN, float.NaN)
            : new PointF((b2 * c1 - b1 * c2) / delta, (a1 * c2 - a2 * c1) / delta);
    }

    static void Main() {
        Func<float, float, PointF> p = (x, y) => new PointF(x, y);
        Console.WriteLine(FindIntersection(p(4f, 0f), p(6f, 10f), p(0f, 3f), p(10f, 7f)));
        Console.WriteLine(FindIntersection(p(0f, 0f), p(1f, 1f), p(1f, 2f), p(4f, 5f)));
    }
}








Fortran: program intersect_two_lines
  implicit none
  
  type point
    real::x,y
  end type point
  
  integer, parameter :: n = 4
  type(point)        :: p(n)
  
  p(1)%x = 4; p(1)%y = 0; p(2)%x = 6;  p(2)%y = 10 
  p(3)%x = 0; p(3)%y = 3; p(4)%x = 10; p(4)%y = 7  
  
  call intersect(p, n)
  
  contains
  
  subroutine intersect(p,m)
  integer, intent(in)       :: m
  type(point), intent(in)   :: p(m)
  integer   :: i
  real      :: a(2), b(2) 
  real      :: x, y       
  real      :: dx,dy      
  
  do i = 1, 2
    dx = p(2*i-1)%x - p(2*i)%x
    dy = p(2*i-1)%y - p(2*i)%y
    if( dx == 0.) then    
        a(i) = 0.
        b(i) = p(2*i-1)%y
    else
        a(i)= dy / dx
        b(i) = p(2*i-1)%y - a(i)*p(2*i-1)%x
    endif
  enddo
  
  if( a(1) - a(2) == 0. ) then
    write(*,*)"lines are not intersecting"
    return
  endif
  
  x = ( b(2) - b(1) ) / ( a(1) - a(2) )
  y = a(1) * x + b(1)
  write(*,*)x,y
  end subroutine intersect
end program intersect_two_lines
Task: Find the intersection of two lines


C++: #include <iostream>
#include <cmath>
#include <cassert>
using namespace std;


inline double Det(double a, double b, double c, double d)
{
	return a*d - b*c;
}



bool LineLineIntersect(double x1, double y1, 
	double x2, double y2, 
	double x3, double y3, 
	double x4, double y4, 
	double &ixOut, double &iyOut) 
{
	double detL1 = Det(x1, y1, x2, y2);
	double detL2 = Det(x3, y3, x4, y4);
	double x1mx2 = x1 - x2;
	double x3mx4 = x3 - x4;
	double y1my2 = y1 - y2;
	double y3my4 = y3 - y4;

	double denom = Det(x1mx2, y1my2, x3mx4, y3my4);
	if(denom == 0.0) 
	{
		ixOut = NAN;
		iyOut = NAN;
		return false;
	}

	double xnom = Det(detL1, x1mx2, detL2, x3mx4);
	double ynom = Det(detL1, y1my2, detL2, y3my4);
	ixOut = xnom / denom;	
	iyOut = ynom / denom;
	if(!isfinite(ixOut) || !isfinite(iyOut)) 
		return false;

	return true; 
}

int main()
{
	

	
	double x1=4.0, y1=0.0;
	double x2=6.0, y2=10.0;
	
	
	double x3=0.0, y3=3.0;
	double x4=10.0, y4=7.0;

	double ix = -1.0, iy = -1.0;
	bool result = LineLineIntersect(x1, y1, x2, y2, x3, y3, x4, y4, ix, iy);
	cout << "result " <<  result << "," << ix << "," << iy << endl;

	double eps = 1e-6;
	assert(result == true);
	assert(fabs(ix - 5.0) < eps);
	assert(fabs(iy - 5.0) < eps);
        return 0;
}









Fortran: program intersect_two_lines
  implicit none
  
  type point
    real::x,y
  end type point
  
  integer, parameter :: n = 4
  type(point)        :: p(n)
  
  p(1)%x = 4; p(1)%y = 0; p(2)%x = 6;  p(2)%y = 10 
  p(3)%x = 0; p(3)%y = 3; p(4)%x = 10; p(4)%y = 7  
  
  call intersect(p, n)
  
  contains
  
  subroutine intersect(p,m)
  integer, intent(in)       :: m
  type(point), intent(in)   :: p(m)
  integer   :: i
  real      :: a(2), b(2) 
  real      :: x, y       
  real      :: dx,dy      
  
  do i = 1, 2
    dx = p(2*i-1)%x - p(2*i)%x
    dy = p(2*i-1)%y - p(2*i)%y
    if( dx == 0.) then    
        a(i) = 0.
        b(i) = p(2*i-1)%y
    else
        a(i)= dy / dx
        b(i) = p(2*i-1)%y - a(i)*p(2*i-1)%x
    endif
  enddo
  
  if( a(1) - a(2) == 0. ) then
    write(*,*)"lines are not intersecting"
    return
  endif
  
  x = ( b(2) - b(1) ) / ( a(1) - a(2) )
  y = a(1) * x + b(1)
  write(*,*)x,y
  end subroutine intersect
end program intersect_two_lines
Task: Find the intersection of two lines

C: #include<stdlib.h>
#include<stdio.h>
#include<math.h>

typedef struct{
	double x,y;
}point;

double lineSlope(point a,point b){
	
	if(a.x-b.x == 0.0)
		return NAN;
	else
		return (a.y-b.y)/(a.x-b.x);
}

point extractPoint(char* str){
	int i,j,start,end,length;
	char* holder;
	point c;
	
	for(i=0;str[i]!=00;i++){
		if(str[i]=='(')
			start = i;
		if(str[i]==','||str[i]==')')
		{
			end = i;
			
			length = end - start;
			
			holder = (char*)malloc(length*sizeof(char));
			
			for(j=0;j<length-1;j++)
				holder[j] = str[start + j + 1];
			holder[j] = 00;
			
			if(str[i]==','){
				start = i;
				c.x = atof(holder);
			}
			else
				c.y = atof(holder);
		}
	}
	
	return c;
}

point intersectionPoint(point a1,point a2,point b1,point b2){
	point c;
	
	double slopeA = lineSlope(a1,a2), slopeB = lineSlope(b1,b2);
	
	if(slopeA==slopeB){
		c.x = NAN;
		c.y = NAN;
	}
	else if(isnan(slopeA) && !isnan(slopeB)){
		c.x = a1.x;
		c.y = (a1.x-b1.x)*slopeB + b1.y;
	}
	else if(isnan(slopeB) && !isnan(slopeA)){
		c.x = b1.x;
		c.y = (b1.x-a1.x)*slopeA + a1.y;
	}
	else{
		c.x = (slopeA*a1.x - slopeB*b1.x + b1.y - a1.y)/(slopeA - slopeB);
		c.y = slopeB*(c.x - b1.x) + b1.y;
	}
	
	return c;
}

int main(int argC,char* argV[])
{
	point c;
	
	if(argC < 5)
		printf("Usage : %s <four points specified as (x,y) separated by a space>",argV[0]);
	else{
		c = intersectionPoint(extractPoint(argV[1]),extractPoint(argV[2]),extractPoint(argV[3]),extractPoint(argV[4]));
		
		if(isnan(c.x))
			printf("The lines do not intersect, they are either parallel or co-incident.");
		else
			printf("Point of intersection : (%lf,%lf)",c.x,c.y);
	}
	
	return 0;
}










Fortran: program intersect_two_lines
  implicit none
  
  type point
    real::x,y
  end type point
  
  integer, parameter :: n = 4
  type(point)        :: p(n)
  
  p(1)%x = 4; p(1)%y = 0; p(2)%x = 6;  p(2)%y = 10 
  p(3)%x = 0; p(3)%y = 3; p(4)%x = 10; p(4)%y = 7  
  
  call intersect(p, n)
  
  contains
  
  subroutine intersect(p,m)
  integer, intent(in)       :: m
  type(point), intent(in)   :: p(m)
  integer   :: i
  real      :: a(2), b(2) 
  real      :: x, y       
  real      :: dx,dy      
  
  do i = 1, 2
    dx = p(2*i-1)%x - p(2*i)%x
    dy = p(2*i-1)%y - p(2*i)%y
    if( dx == 0.) then    
        a(i) = 0.
        b(i) = p(2*i-1)%y
    else
        a(i)= dy / dx
        b(i) = p(2*i-1)%y - a(i)*p(2*i-1)%x
    endif
  enddo
  
  if( a(1) - a(2) == 0. ) then
    write(*,*)"lines are not intersecting"
    return
  endif
  
  x = ( b(2) - b(1) ) / ( a(1) - a(2) )
  y = a(1) * x + b(1)
  write(*,*)x,y
  end subroutine intersect
end program intersect_two_lines
Task: Find the intersection of two lines




Go: package main

import (
	"fmt"
	"errors"
)

type Point struct {
	x float64
	y float64
}

type Line struct {
	slope float64
	yint float64
}

func CreateLine (a, b Point) Line {
	slope := (b.y-a.y) / (b.x-a.x)
	yint := a.y - slope*a.x
	return Line{slope, yint}
} 

func EvalX (l Line, x float64) float64 {
	return l.slope*x + l.yint
}

func Intersection (l1, l2 Line) (Point, error) {
	if l1.slope == l2.slope {
		return Point{}, errors.New("The lines do not intersect")
	}
	x := (l2.yint-l1.yint) / (l1.slope-l2.slope)
	y := EvalX(l1, x)
	return Point{x, y}, nil
}

func main() {
	l1 := CreateLine(Point{4, 0}, Point{6, 10})
	l2 := CreateLine(Point{0, 3}, Point{10, 7})
	if result, err := Intersection(l1, l2); err == nil {
		fmt.Println(result)
	} else {
		fmt.Println("The lines do not intersect")
	}
}







Fortran: program intersect_two_lines
  implicit none
  
  type point
    real::x,y
  end type point
  
  integer, parameter :: n = 4
  type(point)        :: p(n)
  
  p(1)%x = 4; p(1)%y = 0; p(2)%x = 6;  p(2)%y = 10 
  p(3)%x = 0; p(3)%y = 3; p(4)%x = 10; p(4)%y = 7  
  
  call intersect(p, n)
  
  contains
  
  subroutine intersect(p,m)
  integer, intent(in)       :: m
  type(point), intent(in)   :: p(m)
  integer   :: i
  real      :: a(2), b(2) 
  real      :: x, y       
  real      :: dx,dy      
  
  do i = 1, 2
    dx = p(2*i-1)%x - p(2*i)%x
    dy = p(2*i-1)%y - p(2*i)%y
    if( dx == 0.) then    
        a(i) = 0.
        b(i) = p(2*i-1)%y
    else
        a(i)= dy / dx
        b(i) = p(2*i-1)%y - a(i)*p(2*i-1)%x
    endif
  enddo
  
  if( a(1) - a(2) == 0. ) then
    write(*,*)"lines are not intersecting"
    return
  endif
  
  x = ( b(2) - b(1) ) / ( a(1) - a(2) )
  y = a(1) * x + b(1)
  write(*,*)x,y
  end subroutine intersect
end program intersect_two_lines
Task: Find the intersection of two lines





Java: public class Intersection {
    private static class Point {
        double x, y;

        Point(double x, double y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public String toString() {
            return String.format("{%f, %f}", x, y);
        }
    }

    private static class Line {
        Point s, e;

        Line(Point s, Point e) {
            this.s = s;
            this.e = e;
        }
    }

    private static Point findIntersection(Line l1, Line l2) {
        double a1 = l1.e.y - l1.s.y;
        double b1 = l1.s.x - l1.e.x;
        double c1 = a1 * l1.s.x + b1 * l1.s.y;

        double a2 = l2.e.y - l2.s.y;
        double b2 = l2.s.x - l2.e.x;
        double c2 = a2 * l2.s.x + b2 * l2.s.y;

        double delta = a1 * b2 - a2 * b1;
        return new Point((b2 * c1 - b1 * c2) / delta, (a1 * c2 - a2 * c1) / delta);
    }

    public static void main(String[] args) {
        Line l1 = new Line(new Point(4, 0), new Point(6, 10));
        Line l2 = new Line(new Point(0, 3), new Point(10, 7));
        System.out.println(findIntersection(l1, l2));

        l1 = new Line(new Point(0, 0), new Point(1, 1));
        l2 = new Line(new Point(1, 2), new Point(4, 5));
        System.out.println(findIntersection(l1, l2));
    }
}






Fortran: program intersect_two_lines
  implicit none
  
  type point
    real::x,y
  end type point
  
  integer, parameter :: n = 4
  type(point)        :: p(n)
  
  p(1)%x = 4; p(1)%y = 0; p(2)%x = 6;  p(2)%y = 10 
  p(3)%x = 0; p(3)%y = 3; p(4)%x = 10; p(4)%y = 7  
  
  call intersect(p, n)
  
  contains
  
  subroutine intersect(p,m)
  integer, intent(in)       :: m
  type(point), intent(in)   :: p(m)
  integer   :: i
  real      :: a(2), b(2) 
  real      :: x, y       
  real      :: dx,dy      
  
  do i = 1, 2
    dx = p(2*i-1)%x - p(2*i)%x
    dy = p(2*i-1)%y - p(2*i)%y
    if( dx == 0.) then    
        a(i) = 0.
        b(i) = p(2*i-1)%y
    else
        a(i)= dy / dx
        b(i) = p(2*i-1)%y - a(i)*p(2*i-1)%x
    endif
  enddo
  
  if( a(1) - a(2) == 0. ) then
    write(*,*)"lines are not intersecting"
    return
  endif
  
  x = ( b(2) - b(1) ) / ( a(1) - a(2) )
  y = a(1) * x + b(1)
  write(*,*)x,y
  end subroutine intersect
end program intersect_two_lines
Task: Find the intersection of two lines







Python: from __future__ import division

def setup():
    
    (a, b), (c, d) = (4, 0), (6, 10)  
    (e, f), (g, h) = (0, 3), (10, 7)  
    pt = line_instersect(a, b, c, d, e, f, g, h)
    scale(9)
    line(a, b, c, d)
    line(e, f, g, h)
    if pt:
        x, y = pt
        stroke(255)
        point(x, y)
    println(pt)  

def line_instersect(Ax1, Ay1, Ax2, Ay2, Bx1, By1, Bx2, By2):
    
    d = (By2 - By1) * (Ax2 - Ax1) - (Bx2 - Bx1) * (Ay2 - Ay1)
    if d:
        uA = ((Bx2 - Bx1) * (Ay1 - By1) - (By2 - By1) * (Ax1 - Bx1)) / d
        uB = ((Ax2 - Ax1) * (Ay1 - By1) - (Ay2 - Ay1) * (Ax1 - Bx1)) / d
    else:
        return
    if not(0 <= uA <= 1 and 0 <= uB <= 1):
        return
    x = Ax1 + uA * (Ax2 - Ax1)
    y = Ay1 + uA * (Ay2 - Ay1)
    return x, y




Fortran: program intersect_two_lines
  implicit none
  
  type point
    real::x,y
  end type point
  
  integer, parameter :: n = 4
  type(point)        :: p(n)
  
  p(1)%x = 4; p(1)%y = 0; p(2)%x = 6;  p(2)%y = 10 
  p(3)%x = 0; p(3)%y = 3; p(4)%x = 10; p(4)%y = 7  
  
  call intersect(p, n)
  
  contains
  
  subroutine intersect(p,m)
  integer, intent(in)       :: m
  type(point), intent(in)   :: p(m)
  integer   :: i
  real      :: a(2), b(2) 
  real      :: x, y       
  real      :: dx,dy      
  
  do i = 1, 2
    dx = p(2*i-1)%x - p(2*i)%x
    dy = p(2*i-1)%y - p(2*i)%y
    if( dx == 0.) then    
        a(i) = 0.
        b(i) = p(2*i-1)%y
    else
        a(i)= dy / dx
        b(i) = p(2*i-1)%y - a(i)*p(2*i-1)%x
    endif
  enddo
  
  if( a(1) - a(2) == 0. ) then
    write(*,*)"lines are not intersecting"
    return
  endif
  
  x = ( b(2) - b(1) ) / ( a(1) - a(2) )
  y = a(1) * x + b(1)
  write(*,*)x,y
  end subroutine intersect
end program intersect_two_lines
Task: Find the intersection of two lines








VB: Option Explicit

Public Type Point
  x As Double
  y As Double
  invalid As Boolean
End Type

Public Type Line
  s As Point
  e As Point
End Type
  
Public Function GetIntersectionPoint(L1 As Line, L2 As Line) As Point
Dim a1 As Double
Dim b1 As Double
Dim c1 As Double
Dim a2 As Double
Dim b2 As Double
Dim c2 As Double
Dim det As Double

  a1 = L1.e.y - L1.s.y
  b1 = L1.s.x - L1.e.x
  c1 = a1 * L1.s.x + b1 * L1.s.y
  a2 = L2.e.y - L2.s.y
  b2 = L2.s.x - L2.e.x
  c2 = a2 * L2.s.x + b2 * L2.s.y
  det = a1 * b2 - a2 * b1
  
  If det Then
    With GetIntersectionPoint
      .x = (b2 * c1 - b1 * c2) / det
      .y = (a1 * c2 - a2 * c1) / det
    End With
  Else
    GetIntersectionPoint.invalid = True
  End If
End Function

Sub Main()
Dim ln1 As Line
Dim ln2 As Line
Dim ip As Point
  
  ln1.s.x = 4
  ln1.s.y = 0
  ln1.e.x = 6
  ln1.e.y = 10
  ln2.s.x = 0
  ln2.s.y = 3
  ln2.e.x = 10
  ln2.e.y = 7
  ip = GetIntersectionPoint(ln1, ln2)
  Debug.Assert Not ip.invalid
  Debug.Assert ip.x = 5 And ip.y = 5
  
  LSet ln2.s = ln2.e
  ip = GetIntersectionPoint(ln1, ln2)
  Debug.Assert ip.invalid
  
  LSet ln2 = ln1
  ip = GetIntersectionPoint(ln1, ln2)
  Debug.Assert ip.invalid
  
End Sub



Fortran: program intersect_two_lines
  implicit none
  
  type point
    real::x,y
  end type point
  
  integer, parameter :: n = 4
  type(point)        :: p(n)
  
  p(1)%x = 4; p(1)%y = 0; p(2)%x = 6;  p(2)%y = 10 
  p(3)%x = 0; p(3)%y = 3; p(4)%x = 10; p(4)%y = 7  
  
  call intersect(p, n)
  
  contains
  
  subroutine intersect(p,m)
  integer, intent(in)       :: m
  type(point), intent(in)   :: p(m)
  integer   :: i
  real      :: a(2), b(2) 
  real      :: x, y       
  real      :: dx,dy      
  
  do i = 1, 2
    dx = p(2*i-1)%x - p(2*i)%x
    dy = p(2*i-1)%y - p(2*i)%y
    if( dx == 0.) then    
        a(i) = 0.
        b(i) = p(2*i-1)%y
    else
        a(i)= dy / dx
        b(i) = p(2*i-1)%y - a(i)*p(2*i-1)%x
    endif
  enddo
  
  if( a(1) - a(2) == 0. ) then
    write(*,*)"lines are not intersecting"
    return
  endif
  
  x = ( b(2) - b(1) ) / ( a(1) - a(2) )
  y = a(1) * x + b(1)
  write(*,*)x,y
  end subroutine intersect
end program intersect_two_lines
Task: Conditional structures



C#: if (condition)
{
   
}

if (condition)
{
  
}
else if (condition2)
{
  
}
else
{
  
}









Ada: type Restricted is range 1..10;
My_Var : Restricted;

if My_Var = 5 then
  
elsif My_Var > 5 then
  
else
  
end if;
Task: Conditional structures

C: int a = 3;


if (a == 2) {
    puts ("a is 2"); 
} else if (a == 3) {
    puts ("a is 3");
} else {
    puts("a is 4");
}


unless (a == 2) { 
    puts ("a is 2");  
} else if (a == 3) {
    puts ("a is 3");
} else {
    puts("a is 4");
}   


switch (a) {
    case 2:
        puts ("a is 2"); 
        break;
    case 3:
        puts ("a is 3");
        break;
    case 4:
        puts ("a is 4");
        break;
    default: 
        puts("is neither");
}











Ada: type Restricted is range 1..10;
My_Var : Restricted;

if My_Var = 5 then
  
elsif My_Var > 5 then
  
else
  
end if;
Task: Conditional structures


C++: template<bool Condition, typename ThenType, typename Elsetype> struct ifthenelse;

template<typename ThenType, typename ElseType> struct ifthenelse<true, ThenType, ElseType>
{
  typedef ThenType type;
};

template<typename ThenType, typename ElseType> struct ifthenelse<false, ThenType, ElseType>
{
  typedef ElseType type;
};


ifthenelse<INT_MAX == 32767, 
           long int,         
           int>              
  ::type myvar;              










Ada: type Restricted is range 1..10;
My_Var : Restricted;

if My_Var = 5 then
  
elsif My_Var > 5 then
  
else
  
end if;
Task: Conditional structures




Go: if booleanExpression {
    statements
}








Ada: type Restricted is range 1..10;
My_Var : Restricted;

if My_Var = 5 then
  
elsif My_Var > 5 then
  
else
  
end if;
Task: Conditional structures





Java: if (s == 'Hello World') {
    foo();
} else if (s == 'Bye World') {
    bar();
} else {
    deusEx();
}







Ada: type Restricted is range 1..10;
My_Var : Restricted;

if My_Var = 5 then
  
elsif My_Var > 5 then
  
else
  
end if;
Task: Conditional structures







Python: if x == 0:
    foo()
elif x == 1:
    bar()
elif x == 2:
    baz()
else:
    qux()

match x:
    0 => foo()
    1 => bar()
    2 => baz()
    _ => qux()

(a) ? b : c





Ada: type Restricted is range 1..10;
My_Var : Restricted;

if My_Var = 5 then
  
elsif My_Var > 5 then
  
else
  
end if;
Task: Conditional structures








VB: Sub C_S_If()
Dim A$, B$

    A = "Hello"
    B = "World"
    
    If A = B Then Debug.Print A & " = " & B
    
    If A = B Then
        Debug.Print A & " = " & B
    Else
        Debug.Print A & " and " & B & " are differents."
    End If
    
    If A = B Then
        Debug.Print A & " = " & B
    Else: Debug.Print A & " and " & B & " are differents."
    End If
    
    If A = B Then Debug.Print A & " = " & B _
    Else Debug.Print A & " and " & B & " are differents."    
    
    If A = B Then Debug.Print A & " = " & B Else Debug.Print A & " and " & B & " are differents."
    If A = B Then Debug.Print A & " = " & B Else: Debug.Print A & " and " & B & " are differents."
End Sub




Ada: type Restricted is range 1..10;
My_Var : Restricted;

if My_Var = 5 then
  
elsif My_Var > 5 then
  
else
  
end if;
Task: Conditional structures

C: int a = 3;


if (a == 2) {
    puts ("a is 2"); 
} else if (a == 3) {
    puts ("a is 3");
} else {
    puts("a is 4");
}


unless (a == 2) { 
    puts ("a is 2");  
} else if (a == 3) {
    puts ("a is 3");
} else {
    puts("a is 4");
}   


switch (a) {
    case 2:
        puts ("a is 2"); 
        break;
    case 3:
        puts ("a is 3");
        break;
    case 4:
        puts ("a is 4");
        break;
    default: 
        puts("is neither");
}
Task: Conditional structures



C#: if (condition)
{
   
}

if (condition)
{
  
}
else if (condition2)
{
  
}
else
{
  
}
Task: Conditional structures


C++: template<bool Condition, typename ThenType, typename Elsetype> struct ifthenelse;

template<typename ThenType, typename ElseType> struct ifthenelse<true, ThenType, ElseType>
{
  typedef ThenType type;
};

template<typename ThenType, typename ElseType> struct ifthenelse<false, ThenType, ElseType>
{
  typedef ElseType type;
};


ifthenelse<INT_MAX == 32767, 
           long int,         
           int>              
  ::type myvar;
Task: Conditional structures





Java: if (s == 'Hello World') {
    foo();
} else if (s == 'Bye World') {
    bar();
} else {
    deusEx();
}
Task: Conditional structures







Python: if x == 0:
    foo()
elif x == 1:
    bar()
elif x == 2:
    baz()
else:
    qux()

match x:
    0 => foo()
    1 => bar()
    2 => baz()
    _ => qux()

(a) ? b : c
Task: Conditional structures








VB: Sub C_S_If()
Dim A$, B$

    A = "Hello"
    B = "World"
    
    If A = B Then Debug.Print A & " = " & B
    
    If A = B Then
        Debug.Print A & " = " & B
    Else
        Debug.Print A & " and " & B & " are differents."
    End If
    
    If A = B Then
        Debug.Print A & " = " & B
    Else: Debug.Print A & " and " & B & " are differents."
    End If
    
    If A = B Then Debug.Print A & " = " & B _
    Else Debug.Print A & " and " & B & " are differents."    
    
    If A = B Then Debug.Print A & " = " & B Else Debug.Print A & " and " & B & " are differents."
    If A = B Then Debug.Print A & " = " & B Else: Debug.Print A & " and " & B & " are differents."
End Sub
Task: Conditional structures




Go: if booleanExpression {
    statements
}
Task: Conditional structures



C#: if (condition)
{
   
}

if (condition)
{
  
}
else if (condition2)
{
  
}
else
{
  
}








Fortran: if ( a .gt. 20.0 ) then
   q = q + a**2
else if ( a .ge. 0.0 ) then
   q = q + 2*a**3
else
   q = q - a
end if
Task: Conditional structures


C++: template<bool Condition, typename ThenType, typename Elsetype> struct ifthenelse;

template<typename ThenType, typename ElseType> struct ifthenelse<true, ThenType, ElseType>
{
  typedef ThenType type;
};

template<typename ThenType, typename ElseType> struct ifthenelse<false, ThenType, ElseType>
{
  typedef ElseType type;
};


ifthenelse<INT_MAX == 32767, 
           long int,         
           int>              
  ::type myvar;              









Fortran: if ( a .gt. 20.0 ) then
   q = q + a**2
else if ( a .ge. 0.0 ) then
   q = q + 2*a**3
else
   q = q - a
end if