Latex Algorithm - If-Else - If Shows End Before Begins Else [Duplicate]

As you see in my latex it shows end after If statement which supposes to end after Else. I tried removing the curly braces of the IF but it does not work.

\documentclass[journal]{IEEEtran}
\ifCLASSINFOpdf
\usepackage[pdftex]{graphicx}
\usepackage[dvips]{graphicx}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{amsfonts}
\usepackage{amssymb,mathtools}
\usepackage[linesnumbered,ruled,vlined] {algorithm2e}
\usepackage{nomencl}
\usepackage{algpseudocode}
\usepackage{algorithm}
\hyphenation{op-tical net-works semi-conduc-tor}

\begin{document}


\begin{algorithm}
    \caption{PEC, \texttt{PEC}}
    \SetAlgoLined
    \DontPrintSemicolon
    \textbf{Input:} I, P, C.
    
    
    \textbf{Output:} Set of $PS^*$= $\{{PS_{1},PS_{2}, ...,PS_{n}}\}$ 
    
    //Initialization
    
    \quad \quad \quad $PS^* \gets \emptyset$
    
    
    \For  {each, $i \in I$} {
        // Calculate
        
        $PS^{temp}_{i} \gets P$
        
        $PS^{loss}_{i} \gets C)$
        
        \If { $ C_{i}- P_{i} > 0$}{
            
            $PS_{i} \gets $($PS^{temp}_{i} - PS^{loss}_{i}$)
            
        }
        \Else {
            $PS_{i} \gets 0$
        }
    }

    
    return ($PS^*$)
    
    \label{alg:PoEG}
\end{algorithm}


\end{document}

The output for this problem is as follows, as you see the highlighted end, which I need to eliminate.

0

Your code does not compile due to two major problems:

  • you must not load the graphicx package multiple times with conflicting options. If your tex distribution has been updated at least once since the Stone Age, then best load it without option and let latex determine the necessary driver itself.

  • you must not load conflicting packages for algorithms. Decide which one you want to use and load this. Latex very clearly gives you error messages about commands already defined, DON'T ignore errors!!!!!


... and then there is minor inconvenience of the additional end: simply use the \eIf macro instead of \If if you have an in-else statement

And finally a suggestion: if you you wouldn't manually mess with \quads and use appropriate keywords instead, things would align automatically

\documentclass[journal]{IEEEtran}
\ifCLASSINFOpdf
%\usepackage[pdftex]{graphicx}
%\usepackage[dvips]{graphicx}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{amsfonts}
\usepackage{amssymb,mathtools}
\usepackage{algpseudocode}
%\usepackage{algorithm}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{nomencl}

\hyphenation{op-tical net-works semi-conduc-tor}

\begin{document}

\begin{algorithm}
    \caption{Proof of Energy Generation, \texttt{PoEG}}
    \SetAlgoLined
    \SetKwInOut{Input}{input}
    \SetKwInOut{Output}{output}
    \DontPrintSemicolon
    \Input{Prosumer Set, $\textit{I}=\{{\textit{I}_{1},\textit{I}_{2},...,\textit{I}_{n}}$\},\linebreak
    Predicted energy production, $\textit{P}= \{{\textit{P}_{1},\textit{P}_{2},...,\textit{P}_{n}}$\}, \linebreak
    Predicted energy consumption, $\textit{C}= \{{\textit{C}_{1},\textit{C}_{2},...,\textit{C}_{n}}.$\},\linebreak
    Distance from DS, $\textit{D}= \{{\textit{d}_{1},\textit{d}_{2},...,\textit{d}_{n}}$\}.}
    
    \Output{Set of $PS^*$= $\{{PS_{1},PS_{2}, ...,PS_{n}}\}$ }
    
    //Initialization
    
    \quad \quad \quad $PS^* \gets \emptyset$
    
    
    \For  {each prosumers, $i \in I$} {
        // Calculate proof score for energy balance and loss,
        
        $PS^{temp}_{i} \gets \log(1/e^{C_i-P_i})$
        
        $PS^{loss}_{i} \gets \log(e^{E_{d_i}})$
        
        \eIf { $ C_{i}- P_{i} > 0$}{
            
            $PS_{i} \gets $($PS^{temp}_{i} - PS^{loss}_{i}$)
            
        }{
            $PS_{i} \gets 0$
        }
        $PS^* \gets$ $PS^* \cup   \{PS_{i}\}$
    }
    
    
    return ($PS^*$)
    
    \label{alg:PoEG}
\end{algorithm}


\end{document}
1
Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest